I've been playing around with protractor and promises, and I'm puzzled by the different results I'm getting. I have three tests that basically load www.angularjs.org and wait for the "Home" link.
describe('Sample tests', function() {
it("test1", function(){
browser.get("angularjs.org");
browser.wait(protractor.ExpectedConditions.visibilityOf(element(by.linkText('Home'))))
.then(function() {
console.log("element visible");
})
})
it("test2", function() {
browser.get("angularjs.org");
fn1().then(function(){
return browser.wait(protractor.ExpectedConditions.visibilityOf(element(by.linkText('Home'))))
.then(function() {
console.log("element visible");
})
});
function fn1() {
return new Promise(function (fulfill, reject){
fulfill();
});
}
})
it("test3", function() {
browser.get("angularjs.org");
fn1().then(function(){
browser.wait(protractor.ExpectedConditions.visibilityOf(element(by.linkText('Home'))))
.then(function() {
console.log("element visible");
}, function(err) {
console.log("error: " + err);
})
});
function fn1() {
return new Promise(function (fulfill, reject){
browser.manage().addCookie("abc", "123")
.then(function() {
console.log("set cookie");
fulfill();
}, function(err){
console.log("error in fn1: " + err);
})
});
}
})
})
test1 passes and outputs element visible
.
test2 fails with exception Error while waiting for Protractor to sync with the page: "[ng:test] http://errors.angularjs.org/1.5.8/ng/test"
or Error: Error while waiting for Protractor to sync with the page: "window.angular is undefined.
test3 does not print element visible
, and intermittently outputs error: WebDriverError: no such session (Driver info: chromedriver=2.22.397929 (fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.10.5 x86_64)
Funny thing is if I move browser.get("angularjs.org");
to a beforeEach
block, test2 does not throw an error and outputs element visible
, while test1 and test3 behaves the same regardless of the location of browser.get("angularjs.org");
.
So my questions are
- Why does placing
browser.get("angularjs.org");
in abeforeEach
block make test2 work, and why doesn't the location ofbrowser.get("angularjs.org");
affect test1 and test3? - Why doesn't test3 output
element visible
?
UPDATE I fixed test3 by
- returning the browser-promise from fn1
- returning the browser-promise in
fn1().then( ... )
- chained an additional
then
clause to the end (fn1().then().then()
) and calleddone()
.
it("test3", function(done) {
browser.get('https://www.angularjs.org')
fn1()
.then(function(){
return browser.wait(protractor.ExpectedConditions.visibilityOf(element(by.linkText('Home'))))
.then(function() {
console.log("element visible");
}, function(err) {
console.log("error: " + err);
})
})
.then(function(){
done();
});
function fn1() {
return new Promise(function (fulfill, reject){
return browser.manage().addCookie("abc", "123")
.then(function() {
console.log("set cookie");
fulfill();
}, function(err){
console.log("error in fn1: " + err);
})
});
}
})