How do I handle an array of promises using Protrac

2019-08-09 18:32发布

问题:

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);
});

回答1:

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!