Skip to content

Commit

Permalink
Merge pull request DevExpress#165 from inikulin/gh125
Browse files Browse the repository at this point in the history
Runner.browser() now accepts browser path (fixes DevExpress#128)
  • Loading branch information
AndreyBelym committed Nov 9, 2015
2 parents 9e88c8c + 62347b5 commit b5bb7a4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 105 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"parse5": "^1.5.0",
"qrcode-terminal": "^0.10.0",
"read-file-relative": "^1.2.0",
"testcafe-browser-natives": "^0.9.1",
"testcafe-browser-natives": "^0.10.0",
"testcafe-hammerhead": "^0.2.1",
"uglify-js": "1.2.6",
"useragent": "^2.1.7"
Expand Down
2 changes: 1 addition & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const MESSAGE = {
browserDisconnected: 'The {userAgent} browser disconnected. This problem may appear when a browser hangs or is closed, or due to network issues.',
cantRunAgainstDisconnectedBrowsers: 'The following browsers disconnected: {userAgents}. Tests will not be run.',
cantEstablishBrowserConnection: 'Unable to establish one or more of the specified browser connections. This can be caused by network issues or remote device failure.',
cantFindBrowserForAlias: 'Cannot find a corresponding browser for the following alias: {alias}.',
cantFindBrowser: 'Unable to find the browser. "{browser}" is not a browser alias or path to an executable file.',
browserNotSet: 'No browser selected to test against.',
testSourcesNotSet: 'No test file specified.',
noTestsToRun: 'No tests to run. Either the test files contain no tests or the filter function is too restrictive.',
Expand Down
20 changes: 10 additions & 10 deletions src/runner/bootstrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Promise } from 'es6-promise';
import { getInstallations as getBrowserInstallations } from 'testcafe-browser-natives';
import { getBrowserInfo } from 'testcafe-browser-natives';
import reporters from '../reporters';
import BrowserConnection from '../browser-connection';
import LocalBrowserConnection from '../browser-connection/local';
Expand Down Expand Up @@ -27,17 +27,17 @@ export default class Bootstrapper {
return Promise.all(ready);
}

static async _convertBrowserAliasToBrowserInfo (alias) {
if (typeof alias !== 'string')
return alias;
static async _convertAliasOrPathToBrowserInfo (browser) {
if (typeof browser === 'string') {
var browserInfo = await getBrowserInfo(browser);

var installations = await getBrowserInstallations();
var browserInfo = installations[alias.toLowerCase()];
if (!browserInfo)
throw new Error(getText(MESSAGE.cantFindBrowser, browser));

if (!browserInfo)
throw new Error(getText(MESSAGE.cantFindBrowserForAlias, alias));
return browserInfo;
}

return browserInfo;
return browser;
}

_createConnectionFromBrowserInfo (browserInfo) {
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class Bootstrapper {

this._checkForDisconnectedBrowsers();

var browsers = await Promise.all(this.browsers.map(Bootstrapper._convertBrowserAliasToBrowserInfo));
var browsers = await Promise.all(this.browsers.map(Bootstrapper._convertAliasOrPathToBrowserInfo));
var browserConnections = browsers.map(browser => this._createConnectionFromBrowserInfo(browser));

try {
Expand Down
187 changes: 94 additions & 93 deletions test/server/runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ describe('Runner', function () {
throw new Error('Promise rejection expected');
})
.catch(function (err) {
expect(err.message).eql('Cannot find a corresponding browser for the following alias: browser42.');
expect(err.message).eql('Unable to find the browser. "browser42" is not a ' +
'browser alias or path to an executable file.');
});

run
Expand Down Expand Up @@ -446,112 +447,112 @@ describe('Runner', function () {
})
.catch(done);
});
});

describe('Should not stop the task until local connection browsers are not closed', function () {
const BROWSER_CLOSING_DELAY = 50;
const TASK_DONE_DELAY = 50;
describe('Task termination', function () {
const BROWSER_CLOSING_DELAY = 50;
const TASK_DONE_DELAY = 50;

var origCreateBrowserJobs = Task.prototype._createBrowserJobs;
var origClose = LocalBrowserConnection.prototype.close;
var origRunBrowser = LocalBrowserConnection.prototype._runBrowser;
var origAbort = Task.prototype.abort;
var origConvertBrowserAlias = Bootstrapper._convertBrowserAliasToBrowserInfo;
var origCreateBrowserJobs = Task.prototype._createBrowserJobs;
var origClose = LocalBrowserConnection.prototype.close;
var origRunBrowser = LocalBrowserConnection.prototype._runBrowser;
var origAbort = Task.prototype.abort;
var origConvertAliasOrPathToBrowserInfo = Bootstrapper._convertBrowserAliasToBrowserInfo;

var closeCalled = 0;
var taskShouldBeDone = true;
var closeCalled = 0;
var taskShouldBeDone = true;

beforeEach(function () {
closeCalled = 0;
taskShouldBeDone = true;
});
beforeEach(function () {
closeCalled = 0;
taskShouldBeDone = true;
});

before(function () {
Bootstrapper._convertAliasOrPathToBrowserInfo = function (alias) {
return typeof alias === 'string' ? {} : alias;
};

LocalBrowserConnection.prototype.close = function () {
var bc = this;

before(function () {
Bootstrapper._convertBrowserAliasToBrowserInfo = function (alias) {
return typeof alias === 'string' ? {} : alias;
};
setTimeout(function () {
BrowserConnection.prototype.close.call(bc);
closeCalled++;
bc.emit('closed');
}, BROWSER_CLOSING_DELAY);
};

LocalBrowserConnection.prototype._runBrowser = function () {
this.establish('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/42.0.2227.1 Safari/537.36');
};

LocalBrowserConnection.prototype.close = function () {
var bc = this;
Task.prototype._createBrowserJobs = function () {
if (taskShouldBeDone) {
var task = this;

setTimeout(function () {
BrowserConnection.prototype.close.call(bc);
closeCalled++;
bc.emit('closed');
}, BROWSER_CLOSING_DELAY);
};

LocalBrowserConnection.prototype._runBrowser = function () {
this.establish('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/42.0.2227.1 Safari/537.36');
};

Task.prototype._createBrowserJobs = function () {
if (taskShouldBeDone) {
var task = this;

setTimeout(function () {
task.pendingBrowserJobs.forEach(function (job) {
task.emit('browser-job-done', job);
});

task.emit('done');
}, TASK_DONE_DELAY);
}

return this.browserConnections.map(function (bc) {
return { browserConnection: bc };
});
};

Task.prototype.abort = function () {
};
});
task.pendingBrowserJobs.forEach(function (job) {
task.emit('browser-job-done', job);
});

after(function () {
LocalBrowserConnection.prototype.close = origClose;
LocalBrowserConnection.prototype._runBrowser = origRunBrowser;
Task.prototype._createBrowserJobs = origCreateBrowserJobs;
Task.prototype.abort = origAbort;
Bootstrapper._convertBrowserAliasToBrowserInfo = origConvertBrowserAlias;
});
task.emit('done');
}, TASK_DONE_DELAY);
}

it('when task done', function () {
return runner
.browsers('chrome', 'ff')
.reporter(function () {
})
.src('test/server/data/test-suite/top.test.js')
.run()
.then(function () {
expect(closeCalled).eql(2);
});
});
return this.browserConnections.map(function (bc) {
return { browserConnection: bc };
});
};

it('when connection failed', function () {
var brokenConnection = testCafe.createBrowserConnection();
Task.prototype.abort = function () {
};
});

taskShouldBeDone = false;
brokenConnection.establish('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36');
after(function () {
LocalBrowserConnection.prototype.close = origClose;
LocalBrowserConnection.prototype._runBrowser = origRunBrowser;
Task.prototype._createBrowserJobs = origCreateBrowserJobs;
Task.prototype.abort = origAbort;
Bootstrapper._convertAliasOrPathToBrowserInfo = origConvertAliasOrPathToBrowserInfo;
});

setTimeout(function () {
brokenConnection.emit('error', 'I have failed :(');
}, 25);
it('Should not stop the task until local connection browsers are not closed when task done', function () {
return runner
.browsers('browser-alias1', 'browser-alias2')
.reporter(function () {
})
.src('test/server/data/test-suite/top.test.js')
.run()
.then(function () {
expect(closeCalled).eql(2);
});
});

return runner
.browsers(brokenConnection, 'chrome')
.reporter(function () {
})
.src('test/server/data/test-suite/top.test.js')
.run()
.then(function () {
throw new Error('Promise rejection expected');
})
.catch(function (err) {
expect(err.message).eql('I have failed :(');
expect(closeCalled).eql(1);
});
});
it('Should not stop the task until local connection browsers are not closed when connection failed', function () {
var brokenConnection = testCafe.createBrowserConnection();

taskShouldBeDone = false;
brokenConnection.establish('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36');

setTimeout(function () {
brokenConnection.emit('error', 'I have failed :(');
}, 25);

return runner
.browsers(brokenConnection, 'browser-alias')
.reporter(function () {
})
.src('test/server/data/test-suite/top.test.js')
.run()
.then(function () {
throw new Error('Promise rejection expected');
})
.catch(function (err) {
expect(err.message).eql('I have failed :(');
expect(closeCalled).eql(1);
});
});
});
});

0 comments on commit b5bb7a4

Please sign in to comment.