简体   繁体   中英

Using asynchronous commands in nightwatch e2e tests

I have a following e2e scenario written using Nightwatch:

var Q = require('q');

module.exports = {

    afterEach: function (browser, done) {
        browser.end(function() {
            done();
        });
    },

    'should display same data on second page as on first page': function (browser) {
        //Given
        var firstPage = bowser.pages.first()
        //When
        Q.all([
            firstPage.getTextPromise('@element1'),
            firstPage.getTextPromise('@element2'),
            firstPage.getTextPromise('@element3')]
        ).then( function(values) {
            users.click('@linkToSecondPage');
            //Then
            var secondPage = browser.page.secondPage();
            secondPage.expect.element('@dataElement1').text.to.equal(values[0]).before(5000);
            secondPage.expect.element('@dataElemnet2').contains.text(values[1]);
            secondPage.expect.element('@dataElement3').contains.text(values[2]);
        });
    } }

The getTextPromise command is defined by me in following way:

commands: [{
    getTextPromise: function(selector) {
        var self = this;
        return Q.Promise(function (resolve, reject) {
            self.getText(selector, function(result) {resolve(result.value); });
        });
    } }]

The rationale behind this scenarion is to remember some values on one page before clicking on link to second page and then checking that on second page the same content is displayed (for example, you click on one item in a table and go to page displaying details of this particular item).

Unfortunately, I observed that this test sometimes does not check things inside the then callback. I think this is caused by the test finishing (calling done in afterEach() ) before he callback returns. I thought there was a done() callback passed to the test (much like in nightwatch async unit tests) that I could use but apparently there is not.

Is there a proper way to do this in Nightwatch? Perhaps I am using commands in wrong way?

Nightwatch will keep track of command run order itself if the command runs a method on 'this', and returns 'this'.

Try a command like this, adapted as a page command if you prefer:

exports.command = function() {

    var self = this;
    var globals = self.globals;
    if (!globals.values) { globals.values = []; }
    var link = 'some_xpath';

    self.getText('selector', function(result) {
        if(result.status !== -1){
            self.globals.values.push = result.value;
        }
    });

    return self;
};

Because the command returns this. It can be chained and you could be sure the commands run in order without manually writing promises.

example:

var firstPage = bowser.pages.first()
var secondPage = browser.page.secondPage();

firstPage.getTextPromise('@element1')
   .getTextPromise('@element2')
   .getTextPromise('@element3');
secondPage.expect.element('@dataElement1').text.to.equal(global.values[0]).before(5000)
    .expect.element('@dataElemnet2').contains.text(global.values[1])
    .expect.element('@dataElement3').contains.text(global.values[2]);

I haven't tested this out so it may need a slight tweak. Hopefully it gives a rough idea of how to chain your commands the nightwatch way. If you run into a problem let me know.

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