简体   繁体   中英

How do I handle an array of promises using Protractor w/ chai-as-promised

I am using Protractor with CucumberJS and chai-as-promised (given that CucumberJS does not have a built-in assertions library) to build an automated test suite.

Everything works fine for single assertions (using the expect feature of chai-as-promised). I run into trouble, however, when attempting to handle multiple promises within the same test (step). In the following example, verifyUserFirstName returns a promise mapped to a certain row's td.getText().

this.Then(/^I should see my user entry with proper values in the list$/, function (callback) {
    expect(usersPage.verifyUserFirstName('tony@gmail.com')).to.eventually.equal('Tony');
    expect(usersPage.verifyUserLastName('tony@gmail.com')).to.eventually.equal('Bui');
    expect(usersPage.verifyUserPhone('tony@gmail.com')).to.eventually.equal('8764309111');
    callback();

Currently, when any of the expect() lines fail, Protractor will exit and leave the browser window hanging without running the rest of the tests.

When a step featuring just a single expect() fails (see example below), everything works perfectly. It is logged as a failed step, and Protractor continues to run the rest of the tests to completion. Has anyone experienced this?

this.Then(/^I should be directed to the user list page$/, function (callback) {
    expect(browser.getCurrentUrl()).to.eventually.equal('http://localhost:9001/#/nav/').and.notify(callback);
});

I had same challenge, this is how I solved:

this.Then(/^I should see my user entry with proper values in the list$/, function (callback) {
    var verifyUser = Q.all([
        usersPage.verifyUserFirstName('tony@gmail.com'),
        usersPage.verifyUserLastName('tony@gmail.com'),
        usersPage.verifyUserPhone('tony@gmail.com')
    ]);
    expect(verifyUser).to.eventually.deep.equal(['Tony', 'Bui', '8764309111').and.notify(callback);
}

I hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM