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

Use the Safari 10 WebDriver to run tests #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
127 changes: 120 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,121 @@
var fs = require('fs');
var path = require('path');
var wd = require('wd');
var url = require('url');

var Safari = function(baseBrowserDecorator, args, logger) {
baseBrowserDecorator(this);

var config = Object.assign({
protocol: 'http:',
hostname: '127.0.0.1',
port: 4444,
pathname: '/'
}, args.config);

var webDriver = url.format(config);
this.name = 'Safari via WebDriver at ' + webDriver;
var log = logger.create(this.name);

log.debug(JSON.stringify(args));
log.debug(JSON.stringify(config));

this.driver = wd.remote(config);

this.driver.on('status', (info) => {
log.debug('Status: ' + info);
});

this.driver.on('command', (eventType, command, response) => {
log.debug('[command] ' + eventType + ' ' + command + ' ' + (response || ''));
});

this.driver.on('http', (meth, path, data) => {
log.debug('[http] ' + meth + ' ' + path + ' ' + (data || ''));
});

this._getOptions = function() {
return [
"-p", config.port.toString()
];
}

const superStart = this._start;
// Stop the default start from occuring
this._start = () => {};

/**
* This launcher works by checking to see if there is a `/usr/bin/safaridriver` instance running.
* It is determined to be running if the web driver API can be reached on the configured host and port.
* If it is then it it launches the Karma test runner in a new session. If it is not, it then attempts
* to start its own new instance of `/usr/bin/safaridriver` and then connect the Karma test runner in
* a new session.
*
* @param {string} url The URL that the Karma server is listening on.
*/
this.on('start', function(url) {
var self = this;

var attempts = 0;
// TODO: It would be nice if this was configurable
const MAX_ATTEMPTS = 10;
// TODO: It would be nice if this was configurable
const SLEEP_DURATION = 500;

const webDriverConfig = {
browserName: 'safari',
allowW3C: true
};

var SafariBrowser = function(baseBrowserDecorator) {
function attachKarma(error) {
attempts += 1;
if (error && error.code === 'ECONNREFUSED' && attempts === 1) {
log.debug('attachKarma ' + attempts + ' of ' + MAX_ATTEMPTS);
log.debug(self._getCommand() + ' is not running.');
log.debug('Attempting to start ' + self._getCommand() + ' ' + self._getOptions(url).join(' '));
superStart(url);
self.driver.init(webDriverConfig, attachKarma);
} else if (error && error.code === 'ECONNREFUSED' && attempts <= MAX_ATTEMPTS) {
log.debug('attachKarma ' + attempts + ' of ' + MAX_ATTEMPTS);
log.debug('Going to give the driver time to start-up. Sleeping for ' + SLEEP_DURATION + 'ms.');
setTimeout(function() {
log.debug('Awoke to retry.');
self.driver.init(webDriverConfig, attachKarma);
}, SLEEP_DURATION);
} else if (error) {
log.error('Could not connect to Safari.');
log.error(error);
} else {
log.debug('Connected to Safari WebDriver');
log.debug('Connecting to ' + url);
self.driver.get(url);
}
}

self.driver.init(webDriverConfig, attachKarma);
});

this.on('kill', (done) => {
if (this.driver) {
this.driver.quit(function() {
done();
});
} else {
done();
}
});
};

Safari.prototype = {
name: 'Safari',

DEFAULT_CMD: {
darwin: '/usr/bin/safaridriver'
},
ENV_CMD: 'SAFARI_BIN'
};

Safari.$inject = ['baseBrowserDecorator', 'args', 'logger'];

var SafariLegacy = function(baseBrowserDecorator) {
baseBrowserDecorator(this);

this._start = function(url) {
Expand All @@ -20,7 +133,7 @@ var SafariBrowser = function(baseBrowserDecorator) {
};
};

SafariBrowser.prototype = {
SafariLegacy.prototype = {
name: 'Safari',

DEFAULT_CMD: {
Expand All @@ -30,10 +143,10 @@ SafariBrowser.prototype = {
ENV_CMD: 'SAFARI_BIN'
};

SafariBrowser.$inject = ['baseBrowserDecorator'];

SafariLegacy.$inject = ['baseBrowserDecorator'];

// PUBLISH DI MODULE
module.exports = {
'launcher:Safari': ['type', SafariBrowser]
'launcher:Safari': ['type', Safari],
'launcher:SafariLegacy': ['type', SafariLegacy]
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"safari"
],
"author": "Vojta Jina <[email protected]>",
"dependencies": {},
"dependencies": {
"wd": "https://github.com/admc/wd.git#master"
RLovelett marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"karma": ">=0.9"
},
Expand Down
13 changes: 0 additions & 13 deletions safari.html

This file was deleted.