Sergey Panfilov Sergey Panfilov - 1 month ago 16
Javascript Question

Is it a way to chaining chai.js's expects in nightwatch

I'm want to use chai.js assertion bdd library with Nightwatch.js.

It's works! My code looks like:

var expect = require('chai').expect

module.exports = {
'Login' : function (browser) {
var login = browser.page.login()
login.expect.element('@login').to.not.be.enabled
login.setValue('@email', 'some@some.com')
login.setValue('@password', 'pass')
login.expect.element('@login').to.be.enabled
login.click('@login')
login.end()
}
}


BUT. Nightwatch provides pretty fluent interface for own methods which make you able to chain methods like
browser.setValue('...', '...').click('...').end()
.

The question: Can I achieve chaining of methods if I'm use chaijs?

Explanation, what I want:

module.exports = {
'Login' : function (browser) {
var login = browser.page.login()

login.expect.element('@login').to.not.be.enabled
.setValue('@email', 'some@some.com')
.setValue('@password', 'pass')
.expect.element('@login').to.be.enabled
.click('@login')
.end()
}
}


Can I make my tests to looks like code above?

Answer

In order to use multiple expects you have to do this:

module.exports = {
'Login' : function (browser) {
    var login = browser.page.login()

    login.expect.element('@login').to.not.be.enabled
    login
        .setValue('@email', 'some@some.com')
        .setValue('@password', 'pass')
        .expect.element('@login').to.be.enabled
    login    
        .click('@login')
    .end()
    }
}

This happens when you use .expect(...) in your test, or in custom functions within the page object, you have to use the browser/client/page object var again to start chaining again.

Comments