This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Mocha/Chai Promise Adapter -- Proof of Concept #47
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
java -jar selenium/selenium-server-standalone-2.34.0.jar -Dwebdriver.chrome.driver=./selenium/chromedriver | ||
|
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,8 @@ | ||
global.chai = require('chai'); | ||
global.expect = chai.expect; | ||
|
||
var promiseTesting = require('promise-testing'); | ||
global.promiseEngine = new promiseTesting(); | ||
promiseEngine.scanChai(chai); | ||
|
||
require('./index.js'); |
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,102 @@ | ||
/** | ||
* Adapts Mocha tests to work better with WebDriverJS. Borrows | ||
* heavily from the mocha WebDriverJS adapter at | ||
* https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js | ||
*/ | ||
|
||
var webdriver = require('selenium-webdriver'); | ||
|
||
var flow = webdriver.promise.controlFlow(); | ||
|
||
/** | ||
* Wraps a function so that all passed arguments are ignored. | ||
* @param {!Function} fn The function to wrap. | ||
* @return {!Function} The wrapped function. | ||
*/ | ||
function seal(fn) { | ||
return function() { | ||
fn(); | ||
}; | ||
}; | ||
|
||
|
||
/** | ||
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and | ||
* waits for the flow to complete before continuing. | ||
* @param {!Function} globalFn The function to wrap. | ||
* @return {!Function} The new function. | ||
*/ | ||
function wrapInControlFlow(globalFn) { | ||
return function() { | ||
switch (arguments.length) { | ||
case 1: | ||
globalFn(asyncTestFn(arguments[0])); | ||
break; | ||
|
||
case 2: | ||
// The first variable is a description. | ||
globalFn(arguments[0], asyncTestFn(arguments[1])); | ||
break; | ||
|
||
default: | ||
throw Error('Invalid # arguments: ' + arguments.length); | ||
} | ||
}; | ||
|
||
function asyncTestFn(fn) { | ||
return function(done) { | ||
var that = this; | ||
flow.execute(function() { | ||
fn.call(that); | ||
}).then(seal(done), done); | ||
}; | ||
}; | ||
}; | ||
|
||
global.it = wrapInControlFlow(global.it); | ||
global.beforeEach = wrapInControlFlow(global.beforeEach); | ||
global.afterEach = wrapInControlFlow(global.afterEach); | ||
global.after = wrapInControlFlow(global.after); | ||
global.before = wrapInControlFlow(global.before); | ||
|
||
|
||
|
||
/** | ||
* Wrap a Jasmine matcher function so that it can take webdriverJS promises. | ||
* @param {!Function} matcher The matcher function to wrap. | ||
* @param {webdriver.promise.Promise} actualPromise The promise which will | ||
* resolve to the actual value being tested. | ||
*/ | ||
function wrapMatcher(matcher, actualPromise) { | ||
return function(expected) { | ||
actualPromise.then(function(actual) { | ||
if (expected instanceof webdriver.promise.Promise) { | ||
expected.then(function(exp) { | ||
expect(actual)[matcher](exp); | ||
}); | ||
} else { | ||
expect(actual)[matcher](expected); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
/** | ||
* Return a chained set of matcher functions which will be evaluated | ||
* after actualPromise is resolved. | ||
* @param {webdriver.promise.Promise} actualPromise The promise which will | ||
* resolve to the acutal value being tested. | ||
*/ | ||
function promiseMatchers(actualPromise) { | ||
return promiseEngine.wrap(actualPromise).then.expect.result; | ||
}; | ||
|
||
originalExpect = global.expect; | ||
|
||
global.expect = function(actual) { | ||
if (actual instanceof webdriver.promise.Promise) { | ||
return promiseMatchers(actual); | ||
} else { | ||
return originalExpect(actual); | ||
} | ||
}; |
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,63 @@ | ||
/** | ||
* This example shows how to use the protractor library in a Mocha test. | ||
* It assumes that a selenium server is running at localhost:4444. | ||
* Run this test with: | ||
* mocha onMocha.js | ||
*/ | ||
|
||
var util = require('util'); | ||
var webdriver = require('selenium-webdriver'); | ||
var protractor = require('../lib/protractor.js'); | ||
|
||
describe('angularjs.org homepage', function() { | ||
this.timeout(80000); | ||
|
||
var driver, ptor; | ||
|
||
before(function() { | ||
driver = new webdriver.Builder(). | ||
usingServer('http://localhost:4444/wd/hub'). | ||
withCapabilities(webdriver.Capabilities.chrome()).build(); | ||
|
||
driver.manage().timeouts().setScriptTimeout(10000); | ||
ptor = protractor.wrapDriver(driver); | ||
}); | ||
|
||
after(function(done) { | ||
driver.quit().then(function() {done()}); | ||
}) | ||
|
||
it('should greet using binding', function() { | ||
ptor.get('http://www.angularjs.org'); | ||
|
||
ptor.findElement(protractor.By.input('yourName')).sendKeys('Julie'); | ||
|
||
var hello = ptor.findElement(protractor.By.binding('{{yourName}}')); | ||
|
||
expect(hello.getText()).to.eql('Hello Julie!'); | ||
}); | ||
|
||
it('should list todos', function() { | ||
ptor.get('http://www.angularjs.org'); | ||
|
||
var todo = ptor.findElement( | ||
protractor.By.repeater('todo in todos').row(2)); | ||
|
||
expect(todo.getText()).to.eql('build an angular app'); | ||
}); | ||
|
||
// Uncomment to see failures. | ||
|
||
// it('should greet using binding - this one fails', function(done) { | ||
// ptor.get('http://www.angularjs.org'); | ||
|
||
// ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie"); | ||
|
||
// ptor.findElement(protractor.By.binding("Hello {{yourName}}!")). | ||
// getText().then(function(text) { | ||
// expect(text).to.eql('Hello Jack'); | ||
// 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
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.
You probably also want to wrap the 'before' and 'after' functions.