Skip to content

Commit

Permalink
Improved support for running tests using Safari driver
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Dec 11, 2018
1 parent 38136cb commit 40e75b5
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 189 deletions.
38 changes: 22 additions & 16 deletions examples/globalsModule.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
module.exports = {
// this controls whether to abort the test execution when an assertion failed and skip the rest
// it's being used in waitFor commands and expect assertions
abortOnAssertionFailure : true,
abortOnAssertionFailure: true,

// this will overwrite the default polling interval (currently 500ms) for waitFor commands
// and expect assertions that use retry
waitForConditionPollInterval : 300,
waitForConditionPollInterval: 500,

// default timeout value in milliseconds for waitFor commands and implicit waitFor value for
// expect assertions
waitForConditionTimeout : 5000,

// this will cause waitFor commands on elements to throw an error if multiple
// elements are found using the given locate strategy and selector
throwOnMultipleElementsReturned : false,
throwOnMultipleElementsReturned: false,

// controls the timeout time for async hooks. Expects the done() callback to be invoked within this time
// controls the timeout value for async hooks. Expects the done() callback to be invoked within this time
// or an error is thrown
asyncHookTimeout : 10000,

// controls the timeout value for when running async unit tests. Expects the done() callback to be invoked within this time
// or an error is thrown
unitTestsTimeout : 2000,

// controls the timeout value for when executing the global async reporter. Expects the done() callback to be invoked within this time
// or an error is thrown
customReporterCallbackTimeout: 20000,

// Automatically retrying failed assertions - You can tell Nightwatch to automatically retry failed assertions until a given timeout is reached, before the test runner gives up and fails the test.
retryAssertionTimeout: 1000,

'default' : {
myGlobal : function() {
return 'I\'m a method';
Expand All @@ -32,34 +43,29 @@ module.exports = {
}
},

before : function(cb) {
before(cb) {
//console.log('GLOBAL BEFORE')
cb();
},

beforeEach : function(browser, cb) {
beforeEach(browser, cb) {
//console.log('GLOBAL beforeEach')
cb();
},

after : function(cb) {
after(cb) {
//console.log('GLOBAL AFTER')
cb();
},

afterEach : function(browser, cb) {
afterEach(browser, cb) {
browser.perform(function() {
console.log('GLOBAL afterEach')


//console.log('GLOBAL afterEach')
cb();


})

});
},

reporter : function(results, cb) {
reporter(results, cb) {
cb();
}
};
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ class NightwatchClient extends EventEmitter {
.setWebdriverHttpOption('username')
.setWebdriverHttpOption('access_key', ['accessKey', 'access_key', 'password']);

let webdriverOpts = this.settings.webdriver || {};
let timeoutOptions = webdriverOpts.timeout_options || {};
const webdriverOpts = this.settings.webdriver;
const timeoutOptions = webdriverOpts.timeout_options || {};

if (webdriverOpts.port) {
this.httpOpts.setPort(webdriverOpts.port);
Expand Down
1 change: 1 addition & 0 deletions lib/runner/wd-instances/base-wd-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class BaseWDServer {
}

let err = this.createError(null, code);
err.detailedErr = this.error_out || this.output;
this.promiseStarted.reject(err);
}
}
Expand Down
23 changes: 21 additions & 2 deletions lib/runner/wd-instances/safaridriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ class SafariDriver extends BaseWDServer {

static get acceptedCLIArgs() {
return [
'port', 'p', 'enable'
'port', 'p', 'enable', 'legacy', 'w3c'
];
}

static get useLegacyDriver() {
return true;
}

static get serviceName() {
return 'SafariDriver';
}

static get safariDriverPath() {
return '/usr/bin/safaridriver';
}

get initialCheckProcessDelay() {
return 500;
}
Expand All @@ -29,14 +37,16 @@ class SafariDriver extends BaseWDServer {
}

get serviceDownloadUrl() {
return 'https://developer.apple.com/documentation/webkit/macos_webdriver_commands_for_safari';
return 'https://developer.apple.com/documentation/webkit/macos_webdriver_commands_for_safari_12_and_later';
}

get serviceName() {
return SafariDriver.serviceName;
}

constructor(settings) {
settings.server_path = settings.server_path || SafariDriver.safariDriverPath;

super(settings);

const cliArgs = ['--port', this.settings.port];
Expand All @@ -58,6 +68,15 @@ class SafariDriver extends BaseWDServer {
cliArgs.push(...argsReduced);
}

const use_legacy_jsonwire = this.settings.use_legacy_jsonwire === undefined ?
SafariDriver.useLegacyDriver : this.settings.use_legacy_jsonwire;

if (use_legacy_jsonwire) {
cliArgs.push('--legacy');
} else {
cliArgs.push('--w3c');
}

this.settings.cli_args = cliArgs;

// safaridriver doesn't output anything to stdout, so we need to start the process check
Expand Down
8 changes: 5 additions & 3 deletions lib/runner/webdriver-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class WDServer {
return __concurrencyEnabled__;
}

static get instances() {
return __wd_instances__;
}

/**
* @param {Object} baseConfig
* @param {Object} argv
Expand Down Expand Up @@ -128,9 +132,7 @@ class WDServer {

return true;
})
.then(() => {
return this.instance.stop();
});
.then(() => this.instance.stop());
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/settings/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module.exports = {
cli_args: {},
server_path: null,
log_path: '',
use_legacy_jsonwire: undefined,

host: undefined,
port: undefined,
Expand Down Expand Up @@ -147,7 +148,7 @@ module.exports = {

sync_test_names: true,

// Skip a tests by tag name; can be a list of comma-separated values (no space)
// Skip tests by tag name; can be a list of comma-separated values (no space)
skiptags: '',

// Use xpath as the default locator strategy
Expand Down
78 changes: 59 additions & 19 deletions lib/transport/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,18 @@ class Transport extends EventEmitter {
}

createSession() {
let request = this.createProtocolAction({
const body = {};
if (this.settings.capabilities) {
body.capabilities = this.settings.capabilities;
}

if (this.desiredCapabilities) {
body.desiredCapabilities = this.desiredCapabilities;
}

const request = this.createProtocolAction({
path : '/session',
data : {
desiredCapabilities : this.desiredCapabilities
}
data : body
});

request
Expand Down Expand Up @@ -456,37 +463,70 @@ class Transport extends EventEmitter {
});
}

static adaptWebdriverSettings(settings, usingSeleniumServer = false) {
const browserName = settings.desiredCapabilities.browserName.toLowerCase();

if (usingSeleniumServer && [BrowserName.CHROME, BrowserName.EDGE].includes(browserName)) {
settings.selenium.version2 = true;

return;
}

switch (browserName) {
case BrowserName.SAFARI:
if (settings.webdriver.start_process && settings.webdriver.use_legacy_jsonwire === undefined) {
const SafariDriver = require('../runner/wd-instances/safaridriver.js');
settings.webdriver.use_legacy_jsonwire = SafariDriver.useLegacyDriver;
}
break;

case BrowserName.FIREFOX:
settings.webdriver.use_legacy_jsonwire = false;
break;
}

if (settings.webdriver.use_legacy_jsonwire === undefined) {
settings.webdriver.use_legacy_jsonwire = true;
}

if (!settings.webdriver.use_legacy_jsonwire && !usingSeleniumServer) {
settings.capabilities = settings.desiredCapabilities;
}
}

static create(nightwatchInstance) {
let settings = nightwatchInstance.settings;
let browserName = settings.desiredCapabilities.browserName.toLowerCase();
let usingSeleniumServer = settings.selenium && (settings.selenium.start_process || !settings.webdriver.start_process);
let seleniumVersion2 = usingSeleniumServer && (settings.selenium.version2 || [BrowserName.CHROME, BrowserName.EDGE].includes(browserName));
const settings = nightwatchInstance.settings;
const usingSeleniumServer = settings.selenium && (
settings.selenium.start_process ||
!settings.webdriver.start_process && (settings.selenium_host || settings.selenium.host || settings.seleniumHost)
);

if (seleniumVersion2) {
Transport.adaptWebdriverSettings(settings, usingSeleniumServer);

// Legacy Selenium Server 2
if (usingSeleniumServer && settings.selenium.version2) {
const Selenium2 = require('./selenium2.js');

return new Selenium2(nightwatchInstance);
}

// Selenium Server 3
if (usingSeleniumServer) {
const SeleniumProtocol = require('./selenium3.js');

return new SeleniumProtocol(nightwatchInstance);
}

switch (browserName) {
case BrowserName.FIREFOX: {
const WebdriverProtocol = require('./webdriver.js');
// drivers using the legacy JSONWire protocol
if (settings.webdriver.use_legacy_jsonwire) {
const JsonWireProtocol = require('./jsonwire.js');

return new WebdriverProtocol(nightwatchInstance);
}
return new JsonWireProtocol(nightwatchInstance);
}

default: {
const JsonWireProtocol = require('./jsonwire.js');
const WebdriverProtocol = require('./webdriver.js');

return new JsonWireProtocol(nightwatchInstance);
}
}
return new WebdriverProtocol(nightwatchInstance);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/src/api/commands/testClearValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ describe('clearValue', function() {
MockServer.addMock({
url: '/session',
postdata: JSON.stringify({
capabilities: {
browserName: 'firefox',
acceptSslCerts: true
},
desiredCapabilities: {
browserName: 'firefox',
acceptSslCerts: true,
Expand Down
Loading

0 comments on commit 40e75b5

Please sign in to comment.