Skip to content

Commit

Permalink
feat(init): ***breaking change*** pass webdriver instance into `init(…
Browse files Browse the repository at this point in the history
…)` instead of using `require()`

This removes the dependency on `selenium-webdriver` and protects jasminewd from having a different
webdriver instance than Protractor, which could be a huge problem if they had different control flow
settings.

This is a breaking change because it changes the API for the `init` function.
  • Loading branch information
sjelin committed Jan 21, 2017
1 parent 369a249 commit 3876a16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ npm install jasminewd2
Usage
-----

Assumes selenium-webdriver as a peer dependency.

```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(webdriver.promise.controlFlow(), webdriver);

jrunner.projectBaseDir = '';
jrunner.execute(['**/*_spec.js']);
Expand Down Expand Up @@ -76,7 +71,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
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
],
"author": "Julie Ralph <ju.ralph@gmail.com>",
"dependencies": {
"jasmine": "2.4.1",
"selenium-webdriver": "3.0.1"
"jasmine": "2.4.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

0 comments on commit 3876a16

Please sign in to comment.