简体   繁体   English

nightwatch.js - 找不到元素

[英]nightwatch.js - Element could not be located

I have problem with located objects.我对定位的对象有疑问。

my env:我的环境:

  1. List item项目清单
  2. chromedriver and chrome - 77 chromedriver 和 chrome - 77
  3. selenium-server-standalone-3.141.59.jar selenium-server-standalone-3.141.59.jar
  4. node.js - 10.16.3 node.js - 10.16.3
  5. java jdk - 1.8.0.221 java jdk - 1.8.0.221

test: demo.js测试:demo.js

module.exports = {
  'Demo test Ecosia.org': function (browser) {
    browser
      .url('https://www.ecosia.org/')
      .assert.titleContains('Ecosia')
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.containsText('.mainline-results', 'Nightwatch.js')
      .end();
  }
};

nightwatch.json nightwatch.json

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "page_objects_path": "./pages",
  "selenium" : {
    "start_process" : true,
    "server_path" : "lib/drivers/selenium-server-standalone-3.141.59.jar",
    "log_path" : "",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "lib/drivers/chromedriver.exe"
    }
  },
  "test_settings" : {
      "default" : {
       "launch_url" : "http://localhost",
       "selenium_port" : 4444,
       "selenium_host" : "localhost",
       "silent" : true,
       "screenshots" : {
        "enabled" : true,
        "path" : "screenshots",
        "on_failure" : true
       },
       "desiredCapabilities" : {
        "browserName" : "chrome",
        "marionette" : "true",
        "javascriptEnabled": true,
        "acceptSslCerts": true
       },
       "acceptSslCerts" : false,
       "use_xpath" : true
    }
    }
  }

package.json package.json

{
  "name": "vs_studio",
  "version": "1.0.0",
  "description": "to learn automation testing",
  "main": "nightwatch.js",
  "scripts": {
    "test": ""
  },
  "author": "Michal",
  "license": "ISC",
  "dependencies": {
    "nightwatch": "^1.2.4"
  },
  "devDependencies": {
    "chromedriver": "^77.0.0",
    "geckodriver": "^1.18.0"
  }
}

below result of test execution:以下测试执行结果:


[Demo] Test Suite
=================
Running:  Demo test Ecosia.org

√ Testing if the page title contains "Ecosia"  - 17 ms.
   NoSuchElementError: An error occurred while running .isVisible() command on <input[type=search]>:
       at process._tickCallback (internal/process/next_tick.js:68:7)
× Testing if element <input[type=search]> is visible. Element could not be located in 1000 ms. - expected "true" but got: "null"
    at Object.Demo test Ecosia.org (C:\Users\mincberm\Documents\VS_Studio\tests\demo.js:6:15)
    at process._tickCallback (internal/process/next_tick.js:68:7)


FAILED: 1 assertions failed, 1 errors and  1 passed (7.352s)
_________________________________________________

TEST FAILURE: 1 error during execution 1 assertions failed, 1 passed. 14.576s

 × demo
 – Demo test Ecosia.org (7.352s)
   Testing if element <input[type=search]> is visible. Element could not be located in 1000 ms. - expected "true" but got: "null"
       at Object.Demo test Ecosia.org (C:\Users\mincberm\Documents\VS_Studio\tests\demo.js:6:15)
       at process._tickCallback (internal/process/next_tick.js:68:7)

  NoSuchElementError: An error occurred while running .isVisible() command on <input[type=search]>:
       at process._tickCallback (internal/process/next_tick.js:68:7)

i try to execute test on chrome (version 71, 74,75 and 77) and on few version of firefox and I have the same provlem我尝试在 chrome(版本 71、74、75 和 77)和 firefox 的几个版本上执行测试,我有同样的问题

I don't believe Nightwatch has any waitForPageLoaded, so you're going to want to use.waitForElementPresent and use an element from whatever the console tells you loads last.我不相信 Nightwatch 有任何 waitForPageLoaded,所以你会想要使用.waitForElementPresent 并使用控制台告诉你最后加载的任何元素。 Otherwise you'll be asserting before the page load is complete, and you'll have these problems often.否则你会在页面加载完成之前断言,你会经常遇到这些问题。

I modify code:我修改代码:

module.exports = {
  'Demo test Ecosia.org': function (browser) {
    browser
      .url('https://www.ecosia.org/')
      .assert.titleContains('Ecosia')
      .waitForElementPresent('input[type=search]', 20000)
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.containsText('.mainline-results', 'Nightwatch.js')
      .end();
  }
};

and I have still problem:我还有问题:

[Demo] Test Suite
=================
Running:  Demo test Ecosia.org

√ Testing if the page title contains "Ecosia"  - 32 ms.
× Timed out while waiting for element <input[type=search]> to be present for 20000 milliseconds. - expected "found" but got: "not found"
    at Object.Demo test Ecosia.org (C:\Users\mincberm\Documents\VS_Studio\tests\demo.js:6:8)
    at process._tickCallback (internal/process/next_tick.js:68:7)


FAILED: 1 assertions failed and  1 passed (22.329s)
_________________________________________________

TEST FAILURE:  1 assertions failed, 1 passed. 28.396s

 × demo
 – Demo test Ecosia.org (22.329s)
   Timed out while waiting for element <input[type=search]> to be present for 20000 milliseconds. - expected "found" but got: "not found"
       at Object.Demo test Ecosia.org (C:\Users\mincberm\Documents\VS_Studio\tests\demo.js:6:8)
       at process._tickCallback (internal/process/next_tick.js:68:7)

I guess this is a late answer, but since I ended up here I guess I could answer the question too.我想这是一个迟到的答案,但既然我来到这里,我想我也可以回答这个问题。 It looks like you have forgotten to wait before title assertion.看起来您忘记在标题断言之前等待。 It looks like the error is on the title assertion, not the input element.看起来错误出在标题断言上,而不是输入元素上。 This is also similar to what is posted on Nigthwatch.js webpage .这也类似于Nigthwatch.js 网页上发布的内容。

module.exports = {
  'Demo test Ecosia.org': function (browser) {
    browser
      .url('https://www.ecosia.org/')
      .waitForElementVisible('body', 20000)
      .assert.titleContains('Ecosia')
      .waitForElementPresent('input[type=search]', 20000)
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.containsText('.mainline-results', 'Nightwatch.js')
      .end();
  }
};

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

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