繁体   English   中英

Nightwatch setValue 方法不起作用

[英]Nightwatch setValue method doesn't work

我目前正在尝试建立一个 Nightwatch 项目,看看它是否有任何好处。 我现在正在关注本教程 教程中的内容有效,但是当我尝试对其进行一些修改时,它不再起作用。 我查看了Developer API guide ,但我想我仍然缺少一些东西? 我使用的代码粘贴在下面:

var conf = require('../../nightwatch.conf.js');

module.exports = {
  'Demo test' : function (browser) {
    browser
      .url('http://localhost/myWebsite?newwindow=0')
      .waitForElementVisible('body', 6000)
      .setValue('input[name=txtLogin]', 'login')
      .setValue('input[name=txtPassword]', 'password')
      .waitForElementVisible('input.btnLogin', 2000)
      .click('button[id=btnLogin]')
      .pause(6000)
      .assert.elementPresent("#selectTitle")
      .assert.containsText('#selectTitle', 'schedules')
      .assert.urlContains('login/login_start.asp')
      .saveScreenshot(conf.imgpath(browser) + 'titleScreen.png')
      .end();

  }
};

cmd错误:

Running:  Demo test 
 √ Element <body> was visible after 41 milliseconds.
ERROR: Unable to locate element: "input[name=txtLogin]" using: css selector
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:8:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
ERROR: Unable to locate element: "input[name=txtPassword]" using: css selector
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:9:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
 × Timed out while waiting for element <input.btnLogin> to be present for 2000 milliseconds.  - expected "visible" but got: "not found"
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:10:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)

最后,html 只是为了完成:

<input type="text" class="inputText" id="txtLogin" name="txtLogin" >

<input type="password" class="inputText" id="txtPassword"  name="txtPassword" >

setValue对我来说也很棘手。 有时它会放置值,然后清除它们,当前(2017 年 10 月 10 日)每次我使用setValue函数时,它都会突然开始打开 ​​Chrome 设置选项卡。

我现在要做的是首先定义一个这样的函数

var setValue =  function(sel, value) {
    $(sel).val(value).change();
};

然后像这样从 Nightwatch 链函数中调用它

browser.url('https://google.com')
  .execute(setValue, ['#search', 'keyword'])
  .click('#search-btn')
  .waitForElementVisible('an example of selector')
  .assert.containsText('selector', '100000xxxx results')

在 setValue 之前尝试.waitForElementVisible('input[name=txtLogin]',6000) 它解决了我所有的问题。 <body>会立即出现,而input[name=txtLogin]需要一些时间才能出现。

@编辑:

var conf = require('../../nightwatch.conf.js');

module.exports = {
  'Demo test' : function (browser) {
    browser
      .url('http://localhost/myWebsite?newwindow=0')
      .waitForElementVisible('body', 6000)
      .waitForElementVisible('input[name="txtLogin"]', 6000)
      .setValue('input[name="txtLogin"]', 'login')
      .setValue('input[name="txtPassword"]', 'password')
      .waitForElementVisible('input.btnLogin', 2000)
      .click('button[id="btnLogin"]')
      .assert.elementPresent("#selectTitle")
      .assert.containsText('#selectTitle', 'schedules')
      .assert.urlContains('login/login_start.asp')
      .saveScreenshot(conf.imgpath(browser) + 'titleScreen.png')
      .end();

  }
};

像上面一样尝试复制/粘贴

我遇到了这个问题,最终回到了一个测试脚本,该脚本已经确认不是我。

setValue 和 Selenium 驱动程序似乎存在问题https://github.com/nightwatchjs/nightwatch/issues/1147

我的工作是使用 .execute 函数并使用 jQuery 或 vanilla javascript 更新表单字段。

.execute(function(){document.getElementById('_EmailAddress').value = 'email@address.co.uk';})
.execute(function(){$('#_Password').val('PasswordString');})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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