Skip to content

Commit

Permalink
Use the Safari 10 WebDriver to run tests
Browse files Browse the repository at this point in the history
For all the reasons addressed in a WebKit blog post,
https://webkit.org/blog/6901/webdriver-support-in-safari-10/,
this method should be preferred over the previous method.

This should address the concerns of both #6 and #20.
  • Loading branch information
RLovelett committed Mar 3, 2017
1 parent d4dd9d0 commit 690fe97
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
58 changes: 45 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
var fs = require('fs');
var path = require('path');
var wd = require('wd');
var http = require('http');

/**
* The `safaridriver` does not always start accepting incoming connections immediately. As a result we have to "ping"
* the driver to figure out when it is safe to start sending commands. This method pings the server, in a callback loop,
* to determine when the server starts accepting incoming connections.
*
* @param {*} options The options to send to `http.get`.
* @param {number} limit The maximum number of attempts to make before quitting.
* @param {Function} done The callback performed when the Safari WebDriver is accepting connections.
*/
function ping(options, limit, done) {
var attempts = 0;
function error(e) {
if (attempts <= limit) {
http.get(options, done).on('error', error);
}
}
http.get(options, done).on('error', error);
}

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

var log = logger.create('Safari via Remote WebDriver');

var config = args.config || {
hostname: '127.0.0.1',
port: 4444,
pathname: '/'
};

this.name = 'Safari via Remote WebDriver';

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

this._start = function(url) {
var HTML_TPL = path.normalize(__dirname + '/safari.html');
var self = this;

fs.readFile(HTML_TPL, function(err, data) {
var content = data.toString().replace('%URL%', url);
var staticHtmlPath = self._tempDir + '/redirect.html';
log.debug(url);
self._execCommand(self._getCommand(), self._getOptions(url));
ping(config, 100, function() {
self.driver = wd.remote(config, 'promiseChain');
self.browser = self.driver.init({});

fs.writeFile(staticHtmlPath, content, function(err) {
self._execCommand(self._getCommand(), [staticHtmlPath]);
});
self.browser.get(url).done();
});
};
};
Expand All @@ -24,13 +57,12 @@ SafariBrowser.prototype = {
name: 'Safari',

DEFAULT_CMD: {
darwin: '/Applications/Safari.app/Contents/MacOS/Safari',
win32: process.env['ProgramFiles(x86)'] + '\\Safari\\Safari.exe'
darwin: '/usr/bin/safaridriver'
},
ENV_CMD: 'SAFARI_BIN'
};

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


// PUBLISH DI MODULE
Expand Down
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": "^1.1.3"
},
"peerDependencies": {
"karma": ">=0.9"
},
Expand Down
13 changes: 0 additions & 13 deletions safari.html

This file was deleted.

0 comments on commit 690fe97

Please sign in to comment.