Skip to content

Instantly share code, notes, and snippets.

@kumarharsh
Last active January 2, 2016 08:58
Show Gist options
  • Save kumarharsh/8279594 to your computer and use it in GitHub Desktop.
Save kumarharsh/8279594 to your computer and use it in GitHub Desktop.
A gist to track down an error in selenium-webdriver tests using chai-as-promised + mocha
describe("test the UI", function() {
it('when the log is shown', function(next) {
var _this = this;
var promiseA, promiseB;
promiseA = Q.promise() // or whatever, to create a promise for this test
promiseB = Q.promise() // or whatever, to create a promise for this test
promiseA.then(function() { // promiseA and promiseB are Q promises
var fn;
fn = function(limit) {
return promiseB.then(function(data) {
return expect(data.rows.length).to.equal(1);
}).fail(function(err) {
if (limit > 0) {
return fn(limit - 1);
} else {
return Q.reject(err);
}
});
};
return fn(20);
}).then(function() {
this.client.findElement(By.css("li.log-content")).then(function(log) {
return Q.all([
expect(log.getText()) // this is a webdriver.promise, value = HUNDRED.
.to.eventually.equal("THOUSAND"), // This expect clause SHOULD throw an error
expect(log.findElement(By.css(".image")).getAttribute('src')) // this is a webdriver.promise too
.to.eventually.equal('user.png')
]).should.notify(next); // next is always executed, no matter if the test fails or succeeds
}, function(err) {
return console.log("ERROR", JSON.stringify(err)); // Control never reaches here when any of the above two promises fail
})
.then(function() {
console.log("Promise Succeeds", arguments); // Here I get a nice fat error which is
// thrown from the expect() clause,
// for the error, refer to [stack-trace.json]
}).fail(function(err) {
console.log("Promise Fails", err) // Control never reaches here.
return next(err);
});
});
});
{"__flags":{
"object":{
"stack":"at defer (.../node_modules/q/q.js:552:19)
at Promise.then (.../node_modules/q/q.js:792:20)
at when (.../node_modules/q/q.js:879:21)
at Function.all (.../node_modules/q/q.js:1508:12)
at .../test/common/directives/activity_log.test.coffee:83:24
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:402:7)
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:402:7)
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at then (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:377:7)
at Object.webdriver.promise.asap (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:663:11)
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1449:25)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at then (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:377:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:394:19)
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at then (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:377:7)
at Object.webdriver.promise.asap (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:663:11)
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1449:25)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:402:7)
at .../node_modules/selenium-webdriver/lib/webdriver/promise.js:1305:10
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:402:7)
at .../node_modules/selenium-webdriver/lib/goog/base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
at notify (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:328:12)
at notifyAll (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:297:7)
at fulfill (.../node_modules/selenium-webdriver/lib/webdriver/promise.js:402:7)
at .../node_modules/selenium-webdriver/lib/webdriver/promise.js:607:51
at .../node_modules/selenium-webdriver/lib/webdriver/http/http.js:96:5
at IncomingMessage.<anonymous> (.../node_modules/selenium-webdriver/http/index.js:113:7)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)",
"source":{
"stack":"at defer (.../node_modules/q/q.js:552:19)
at .../node_modules/q/q.js:1510:24
at _fulfilled (.../node_modules/q/q.js:798:54)
at self.promiseDispatch.done (.../node_modules/q/q.js:827:30)
at Promise.promise.promiseDispatch (.../node_modules/q/q.js:760:13)
at .../node_modules/q/q.js:821:14
at flush (.../node_modules/q/q.js:108:17)
at process._tickCallback (node.js:415:13)",
"source":{
"exception":{
"name":"AssertionError",
"message":"expected 'HUNDRED' to equal 'THOUSAND'",
"showDiff":true,
"actual":"HUNDRED",
"expected":"THOUSAND",
"stack":"AssertionError: expected 'HUNDRED' to equal 'THOUSAND'
From previous event:
at Array.reduce (native)"
}
}
}
}
}}
// As a workaround to the non-functional .should.notify(next) construct, I'm using this for now
// This way, if the test passes, err will be null, and next would be called without args = PASS
// ... and if the Q.all() fails (either expect() clauses fail) then there will be an err object
// ... which I log to console and FAIL
Q.all([
expect(log.getText())
.to.eventually.equal("HUNDRED"),
expect(log.findElement(By.css(".image > img")).getAttribute('src'))
.to.eventually.equal('http://games.playlyfe.com/test/api/assets/players/RedDevilz')
]).should.notify(function(err) {
if (err) {
console.log(err);
}
return next(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment