Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): ***breaking change*** pass webdriver instance into init() #83

Merged
merged 1 commit into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,27 @@ npm install jasminewd2
Usage
-----

Assumes selenium-webdriver as a peer dependency.
In your setup:

```js
// In your setup.
var JasmineRunner = require('jasmine');
var jrunner = new JasmineRunner();
require('jasminewd2');
var webdriver = require('selenium-webdriver');

global.driver = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({browserName: 'chrome'}).
build();

require('jasminewd2').init(driver.controlFlow(), webdriver);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably still show this webdriver step, since we use the global driver below.

jrunner.projectBaseDir = '';
jrunner.execute(['**/*_spec.js']);
```

// In your tests
In your tests:

```js
describe('tests with webdriver', function() {
it('will wait until webdriver is done', function() {
// This will be an asynchronous test. It will finish once webdriver has
Expand Down Expand Up @@ -76,7 +79,7 @@ publish typings for this function. If you call this function directly (e.g. you
are a Protractor dev), you should simply do:

```ts
require('jasminewd2').init(controlFlow);
require('jasminewd2').init(controlFlow, webdriver);
```

`async` functions / `await`
Expand Down
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js
*/

var webdriver = require('selenium-webdriver');
var WebElement; // Equal to webdriver.WebElement
var idleEventName = 'idle'; // Equal to webdriver.promise.ControlFlow.EventType.IDLE
var maybePromise = require('./maybePromise');

/**
Expand Down Expand Up @@ -49,11 +50,6 @@ function validateString(stringtoValidate) {
}
}

var idleEventName = 'idle';
try {
idleEventName = webdriver.promise.ControlFlow.EventType.IDLE;
} catch(e) {}

/**
* Calls a function once the scheduler is idle. If the scheduler does not support the idle API,
* calls the function immediately. See scheduler.md#idle-api for details.
Expand Down Expand Up @@ -171,14 +167,30 @@ function wrapInScheduler(scheduler, newPromise, globalFn, fnName) {
*
* @param {Object=} scheduler The scheduler to wrap tests in. See scheduler.md for details.
* Defaults to a mock scheduler that calls functions immediately.
* @param {Object=} webdriver The result of `require('selenium-webdriver')`. Passed in here rather
* than required by jasminewd directly so that jasminewd can't end up up with a different version
* of `selenium-webdriver` than your tests use. If not specified, jasminewd will still work, but
* it won't check for `WebElement` instances in expect() statements and could cause control flow
* problems if your tests are using an old version of `selenium-webdriver` (e.g. version 2.53.0).
*/
function initJasmineWd(scheduler) {
function initJasmineWd(scheduler, webdriver) {
if (jasmine.JasmineWdInitialized) {
throw Error('JasmineWd already initialized when init() was called');
}
jasmine.JasmineWdInitialized = true;


// Pull information from webdriver instance
if (webdriver) {
WebElement = webdriver.WebElement || WebElement;
idleEventName = (
webdriver.promise &&
webdriver.promise.ControlFlow &&
webdriver.promise.ControlFlow.EventType &&
webdriver.promise.ControlFlow.EventType.IDLE
) || idleEventname;
}

// Default to mock scheduler
if (!scheduler) {
scheduler = { execute: function(fn) {
Expand All @@ -190,7 +202,7 @@ function initJasmineWd(scheduler) {
var newPromise;
if (typeof scheduler.promise == 'function') {
newPromise = scheduler.promise.bind(scheduler);
} else if (webdriver.promise && webdriver.promise.ControlFlow &&
} else if (webdriver && webdriver.promise && webdriver.promise.ControlFlow &&
(scheduler instanceof webdriver.promise.ControlFlow) &&
(webdriver.promise.USE_PROMISE_MANAGER !== false)) {
newPromise = function(resolver) {
Expand Down Expand Up @@ -226,7 +238,7 @@ function initJasmineWd(scheduler) {

var originalExpect = global.expect;
global.expect = function(actual) {
if (actual instanceof webdriver.WebElement) {
if (WebElement && (actual instanceof WebElement)) {
throw Error('expect called with WebElement argument, expected a Promise. ' +
'Did you mean to use .getText()?');
}
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
"jasmine"
],
"author": "Julie Ralph <ju.ralph@gmail.com>",
"dependencies": {
"jasmine": "2.4.1",
"selenium-webdriver": "3.0.1"
},
"devDependencies": {
"@types/jasmine": "^2.5.40",
"@types/node": "^6.0.56",
"@types/selenium-webdriver": "^2.53.38",
"jasmine": "2.4.1",
"jshint": "^2.9.4",
"selenium-webdriver": "2.53.3",
"selenium-webdriver": "3.0.1",
"tslint": "^4.2.0",
"tslint-eslint-rules": "^3.2.3",
"typescript": "^2.0.10",
Expand Down
3 changes: 2 additions & 1 deletion spec/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {promise as wdpromise, WebElement} from 'selenium-webdriver';

const flow = wdpromise.controlFlow();
require('../index.js').init(process.env['JASMINEWD_TESTS_NO_SCHEDULER'] ? null : flow);
require('../index.js').init(process.env['JASMINEWD_TESTS_NO_SCHEDULER'] ? null : flow,
require('selenium-webdriver'));

export function getFakeDriver() {
return {
Expand Down