使用nightwatchjs点击登录页面中的提交按钮后如何重定向到仪表板页面

时间:2017-12-21 07:29:15

标签: nightwatch.js

我是守夜人js的初学者。我坚持使用代码。有关我的疑问,请参阅以下注释。

假设我有两个页面作为登录和仪表板页面。单击登录页面中的提交按钮后,如何重定向到仪表板页面?

代码:

'Testing Navigation': function( _browser ) {

    _browser

    .url('D:/nightwatch_login/registration.html')

    //.waitForElementVisible( 'body', 1000 )

    .verify.visible('#username')

    .verify.visible('#password')

                .verify.visible('input[id="unchecked_checkbox"]', 'Checkbox is visible')

      .click('input[id="unchecked_checkbox"]')

      .pause(1000)

      .element('id', 'unchecked_checkbox', function(response) {

        _browser.assert.ok(response.value.ELEMENT, 'Checkbox response is OK');

        _browser.elementIdSelected(response.value.ELEMENT, function(result){

          _browser.verify.ok(result.value, 'Checkbox is selected');

        });

      })

    .verify.value( 'input[type=submit]', 'Log In' )

                _browser.navigate('D:/nightwatch_login/welcome.html')

                //.assert.urlEquals("file:///D:/nightwatch_login/" + 'welcome.html')

    //.verify.elementNotPresent('.error')

                .end();

  }

但我面临以下错误:

**Testing if the URL equals "file:///D:/nightwatch_login/welcome.html".  - expected "file:///D:/nightwatch_login/welcome.html" but got: "file:///D:/nightwatch_login/registration.html"**


D:\nightwatch_login\test>nightwatch

Starting selenium server... started - PID:  6356



[Google] Test Suite

=======================



Running:  Testing Form Input Values

 √ Testing if element <#username> is visible.

 √ Testing if element <#password> is visible.

 √ Checkbox is visible

 √ Passed [ok]: 0.5392208705726396-3 ok Checkbox response is OK

 √ Passed [ok]: Checkbox is selected

 √ Testing if value of <input[type=submit]> equals: "Log In".





 × Testing if the URL equals "file:///D:/nightwatch_login/welcome.html".  - expected "file:///D:/nightwatch_login/welcome.html" but got: "file:///D:/nightwatch_login/registration.html"

    at Object.module.exports.Testing Form Input Values (D:\nightwatch_login\examples\tests\google.js:230:10)

    at _combinedTickCallback (internal/process/next_tick.js:67:7)





FAILED:  1 assertions failed and 6 passed (20.871s)

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的代码应该是这样的:

'Testing Navigation': function(_browser) {

    _browser
        .url('D:/nightwatch_login/registration.html')
        .verify.visible('#username')
        .verify.visible('#password')
        .verify.visible('input[id="unchecked_checkbox"]', 'Checkbox is visible')
        .click('input[id="unchecked_checkbox"]')
        .pause(1000)
        .element('id', 'unchecked_checkbox', function (response) {
            _browser.assert.ok(response.value.ELEMENT, 'Checkbox response is OK');
            _browser.elementIdSelected(response.value.ELEMENT, function (result) {
                _browser
                    .verify.ok(result.value, 'Checkbox is selected')
                    .verify.value('input[type=submit]', 'Log In')
                    .click('input[type=submit]', 'Log In')
                    .waitForElementPresent('#username', 5000, function(){})
                    .navigate('D:/nightwatch_login/welcome.html')
                    .assert.urlEquals("file:///D:/nightwatch_login/" + 'welcome.html')
                    .verify.elementNotPresent('.error')
                    .end();
            });
        })

}