This repository was archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
allow testing in Selenium locally using --selenium #271
Open
nolanlawson
wants to merge
1
commit into
defunctzombie:master
Choose a base branch
from
nolanlawson:local-selenium
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| var path = require('path'); | ||
| var EventEmitter = require('events').EventEmitter; | ||
| var debug = require('debug')('zuul:selenium'); | ||
| var wd = require('wd'); | ||
|
|
||
| var SELENIUM_VERSION = '2.52.0'; | ||
|
|
||
| var setup_test_instance = require('./setup'); | ||
| require('colors'); | ||
|
|
||
| function Selenium(opt) { | ||
| if (!(this instanceof Selenium)) { | ||
| return new Selenium(opt); | ||
| } | ||
|
|
||
| var self = this; | ||
| self._opt = opt; | ||
| self._browserName = (opt.browsers && opt.browsers[0] && opt.browsers[0].name) || null; | ||
| self._browserVersion = (opt.browsers && opt.browsers[0] && opt.browsers[0].version) || null; | ||
| self.status = { | ||
| passed: 0, | ||
| failed: 0 | ||
| }; | ||
| } | ||
|
|
||
| Selenium.prototype.__proto__ = EventEmitter.prototype; | ||
|
|
||
| Selenium.prototype.start = function() { | ||
| var self = this; | ||
|
|
||
| var seleniumClient; | ||
| var selenium = require('selenium-standalone'); | ||
|
|
||
| function finish() { | ||
| if (self._finished) { | ||
| return; | ||
| } | ||
| self._finished = true; | ||
| reporter.removeAllListeners(); | ||
| if (seleniumClient) { | ||
| seleniumClient.quit(); | ||
| } | ||
| } | ||
|
|
||
| self.controller = setup_test_instance(self._opt, function(err, url) { | ||
| if (err) { | ||
| console.log('Error: %s'.red, err); | ||
| self.emit('done', { | ||
| passed: false | ||
| }); | ||
| finish(); | ||
| } | ||
|
|
||
| debug('url %s', url); | ||
|
|
||
| var reporter = new EventEmitter(); | ||
|
|
||
| reporter.on('console', function(msg) { | ||
| console.log.apply(console, msg.args); | ||
| }); | ||
|
|
||
| reporter.on('test', function(test) { | ||
| console.log('starting', test.name.white); | ||
| }); | ||
|
|
||
| reporter.on('test_end', function(test) { | ||
| if (!test.passed) { | ||
| console.log('failed', test.name.red); | ||
| return self.status.failed++; | ||
| } | ||
|
|
||
| console.log('passed:', test.name.green); | ||
| self.status.passed++; | ||
| }); | ||
|
|
||
| reporter.on('assertion', function(assertion) { | ||
| console.log('Error: %s'.red, assertion.message); | ||
| assertion.frames.forEach(function(frame) { | ||
| console.log(' %s %s:%d'.grey, frame.func, frame.filename, frame.line); | ||
| }); | ||
| console.log(); | ||
| }); | ||
|
|
||
| reporter.on('done', function() { | ||
| finish(); | ||
| }); | ||
|
|
||
| self.emit('init', url); | ||
| self.emit('start', reporter); | ||
|
|
||
| var opts = {version: SELENIUM_VERSION}; | ||
| selenium.install(opts, function(err) { | ||
| if (err) { | ||
| console.log('Error: %s'.red, new Error( | ||
| 'Failed to install selenium')); | ||
| self.emit('done', { | ||
| passed: false | ||
| }); | ||
| finish(); | ||
| return; | ||
| } | ||
| selenium.start(opts, function() { | ||
| seleniumClient = wd.promiseChainRemote(); | ||
| onSeleniumReady(); | ||
| }); | ||
| }); | ||
|
|
||
| function onSeleniumClientReady() { | ||
| var lastMessage = Date.now(); | ||
| var script = 'window.zuul_msg_bus ? ' + | ||
| 'window.zuul_msg_bus.splice(0, window.zuul_msg_bus.length) : ' + | ||
| '[]'; | ||
|
|
||
| var interval = setInterval(poll, 2000); | ||
|
|
||
| function onDoneMessage() { | ||
| clearInterval(interval); | ||
| seleniumClient.quit(function () { | ||
| seleniumClient = null; | ||
| self.emit('done', { | ||
| passed: self.status.passed, | ||
| failed: self.status.failed | ||
| }); | ||
| finish(); | ||
| }); | ||
| } | ||
|
|
||
| function onGetMessages(err, messages) { | ||
| if (err) { | ||
| self.emit('error', err); | ||
| } else if (messages.length) { | ||
| lastMessage = Date.now(); | ||
| messages.forEach(function (msg) { | ||
| debug('msg: %j', msg); | ||
| if (msg.type === 'done') { | ||
| onDoneMessage(); | ||
| } else { | ||
| reporter.emit(msg.type, msg); | ||
| } | ||
| }); | ||
| } else if ((Date.now() - lastMessage) > testTimeout) { | ||
| clearInterval(interval); | ||
| console.log('Error: %s'.red, new Error( | ||
| 'selenium timeout after ' + testTimeout + ' ms')); | ||
| self.emit('done', { | ||
| passed: false | ||
| }); | ||
| finish(); | ||
| } | ||
| } | ||
|
|
||
| function poll() { | ||
| seleniumClient.eval(script, onGetMessages); | ||
| } | ||
| } | ||
|
|
||
| function onSeleniumReady() { | ||
| // TODO: maybe these should be configurable | ||
| var testTimeout = 120000; | ||
| var tunnelId = process.env.TRAVIS_JOB_NUMBER || 'tunnel-' + Date.now(); | ||
| var opts = { | ||
| tunnelTimeout: testTimeout, | ||
| name: self._browserName + ' - ' + tunnelId, | ||
| 'max-duration': 60 * 45, | ||
| 'command-timeout': 599, | ||
| 'idle-timeout': 599, | ||
| 'tunnel-identifier': tunnelId | ||
| }; | ||
| if (self._browserName) { | ||
| opts.browserName = self._browserName; | ||
| } | ||
| if (self._browserVersion) { | ||
| opts.version = self._browserVersion; | ||
| } | ||
|
|
||
| seleniumClient.init(opts).get(url, onSeleniumClientReady); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| Selenium.prototype.shutdown = function() { | ||
| if (self.controller) { | ||
| self.controller.shutdown(); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = Selenium; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| var Zuul = require('../../'); | ||
|
|
||
| var after = require('after'); | ||
| var assert = require('assert'); | ||
|
|
||
| test('jasmine - phantom', function(done) { | ||
| done = after(3, done); | ||
|
|
||
| var config = { | ||
| ui: 'jasmine', | ||
| prj_dir: __dirname + '/../fixtures/jasmine', | ||
| selenium: true, | ||
| concurrency: 1, | ||
| files: [__dirname + '/../fixtures/jasmine/test.js'] | ||
| }; | ||
|
|
||
| var zuul = Zuul(config); | ||
|
|
||
| // each browser we test will emit as a browser | ||
| zuul.on('browser', function(browser) { | ||
| browser.on('init', function() { | ||
| done(); | ||
| }); | ||
|
|
||
| browser.on('done', function(results) { | ||
| assert.equal(results.passed, 1); | ||
| assert.equal(results.failed, 1); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| zuul.run(function(passed) { | ||
| assert.ok(!passed); | ||
| done(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| var Zuul = require('../../'); | ||
|
|
||
| var after = require('after'); | ||
| var assert = require('assert'); | ||
|
|
||
| test('mocha-qunit - phantom', function(done) { | ||
| done = after(3, done); | ||
|
|
||
| var config = { | ||
| ui: 'mocha-qunit', | ||
| prj_dir: __dirname + '/../fixtures/mocha-qunit', | ||
| selenium: true, | ||
| concurrency: 1, | ||
| files: [__dirname + '/../fixtures/mocha-qunit/test.js'] | ||
| }; | ||
| var zuul = Zuul(config); | ||
|
|
||
| zuul.on('browser', function(browser) { | ||
| browser.once('start', function(reporter) { | ||
| reporter.once('done', function(results) { | ||
| assert.equal(results.passed, false); | ||
| assert.equal(results.stats.passed, 1); | ||
| assert.equal(results.stats.failed, 1); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| browser.on('done', function(results) { | ||
| assert.equal(results.passed, 1); | ||
| assert.equal(results.failed, 1); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| zuul.run(function(passed) { | ||
| assert.ok(!passed); | ||
| done(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
localbrowser tests? Because it's always automated right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "automated local"? I wanted to highlight that this isn't designed for debugging, and to distinguish it from
--local.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just "Running through phantomJS" "Running through local Selenium server"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really bike shedding here, this is a great PR :)