简体   繁体   English

Nightwatch setValue 方法不起作用

[英]Nightwatch setValue method doesn't work

I'm currently trying to set up an Nightwatch project, to see if it's any good.我目前正在尝试建立一个 Nightwatch 项目,看看它是否有任何好处。 I'm following This tutorial right now.我现在正在关注本教程 The stuff from the tutorial works, but when I try to modify it a little bit, it doesn't work anymore.教程中的内容有效,但是当我尝试对其进行一些修改时,它不再起作用。 I looked over the Developer API guide , but I guess I'm still missing something?我查看了Developer API guide ,但我想我仍然缺少一些东西? Code I use is pasted below:我使用的代码粘贴在下面:

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

  }
};

Error in cmd: 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)

And finally, html just to be complete:最后,html 只是为了完成:

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

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

The setValue is tricky for me as well. setValue对我来说也很棘手。 Sometimes it puts the values but then clears them and currently (10/10/2017) out of nowhere it started opening the Chrome Settings tab every time I use the setValue function.有时它会放置值,然后清除它们,当前(2017 年 10 月 10 日)每次我使用setValue函数时,它都会突然开始打开 ​​Chrome 设置选项卡。

What I do now, is first define a function like that我现在要做的是首先定义一个这样的函数

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

Then call it from the Nightwatch chain functions like that然后像这样从 Nightwatch 链函数中调用它

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

Try to .waitForElementVisible('input[name=txtLogin]',6000) before setValue.在 setValue 之前尝试.waitForElementVisible('input[name=txtLogin]',6000) IT fixed all my problems.它解决了我所有的问题。 <body> will appear immidietly and input[name=txtLogin] will need some time to appear. <body>会立即出现,而input[name=txtLogin]需要一些时间才能出现。

@EDIT: @编辑:

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

  }
};

Try like above just copy/paste像上面一样尝试复制/粘贴

I was having problems with this and ended up going back to a test script that had worked to confirm that it wasn't me.我遇到了这个问题,最终回到了一个测试脚本,该脚本已经确认不是我。

There appears to be a problem with setValue and the Selenium drivers https://github.com/nightwatchjs/nightwatch/issues/1147 setValue 和 Selenium 驱动程序似乎存在问题https://github.com/nightwatchjs/nightwatch/issues/1147

My work around was to use the .execute function and update the form fields with jQuery or vanilla javascript.我的工作是使用 .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