Skip to content
This repository has been archived by the owner on Jan 26, 2018. It is now read-only.

Commit

Permalink
Merge PR: karma-runner#22 to use WebDriver to run tests (preferred so…
Browse files Browse the repository at this point in the history
…lution).

Merge branch 'safari-webdriver' into develop

* safari-webdriver:
  Use the Safari 10 WebDriver to run tests

# Conflicts:
#	index.js
#	package.json
  • Loading branch information
Mischa1610 committed Oct 2, 2017
2 parents 4e6b2f4 + c8ce913 commit 7657c75
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
56 changes: 45 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,56 @@ var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var Promise = require('bluebird');
var wd = require('wd');
var http = require('http');

var SafariBrowser = function(baseBrowserDecorator) {
/**
* 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, args, logger) {
baseBrowserDecorator(this);

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

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

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 @@ -43,13 +78,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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"author": "Vojta Jina <[email protected]>",
"dependencies": {
"bluebird": "^3.4.0"
"bluebird": "^3.4.0",
"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 7657c75

Please sign in to comment.