首页 > 解决方案 > 不能在 nightwatch.js 中使用 setValue()

问题描述

我正在尝试在 nightwatch.js 中编写一个简单的脚本,该脚本将打开 Google,并在搜索字段中输入文本。我在使用这两种setValue()方法向元素发送文本时遇到了问题。

这是我的脚本:

module.exports = {
  'Search bar displayed on Google Home Page'(driver) {

    driver
      .url('http://www.google.com')
      .assert.urlContains('google')
      .assert.title('Google')
      .waitForElementPresent('input[title="Search"]')
      .pause(5000)
      .setValue('input[title="Search"]', 'test123') // error happens here
      .end()
  },
}

使用时setValue(),我看到以下错误:

运行 .setElementValue() 协议操作时出错:TypeError [ERR_UNESCAPED_CHARACTERS]:尝试为“/wd/hub/session/ed0680ce58544facf2a4b193eccbc223/element/[object Object]/value”创建 HTTP 请求时出错:请求路径包含新的未转义字符ClientRequest (_http_client.js:115:13) at Object.request (http.js:42:10) at HttpRequest.createHttpRequest at new Promise () at Selenium2Protocol.sendProtocolAction

出于某种原因,.setValue()正在尝试作为请求 URL 中Object object的ID 发送。WebElement

脚本成功执行assertand .waitForElementPresent('input[title="Search"]'),所以我知道该元素在页面上。我添加了一个pause(5000)以确保在尝试发送密钥之前页面有足够的时间加载。我之前也尝试过跑步.click().keys()尝试使元素成为焦点。

我相信语法是正确的,但我仍然是守夜人的新手,所以这也可能是一个问题。

这个用户和我有几乎完全相同的问题,但没有答案:Nightwatch 中的 setValue 方法不起作用

我正在使用chromedriver版本80.0.3987.16

我正在使用nightwatch版本1.3.4

chromedriver通过npm使用安装并在我的文件中npm install chromedriver设置路径:chromedriver.exenightwatch.json

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "./page_objects",
  "globals_path" : "",

  "selenium" : {
    "start_process" : true,
    "server_path" : "./node_modules/selenium-standalone/.selenium/selenium-server/3.141.5-server.jar",
    "log_path" : "./reports",
    "host": "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "./node_modules/chromedriver/lib/chromedriver/chromedriver.exe",
      "webdriver.gecko.driver" : "",
      "webdriver.edge.driver" : ""
    }
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "marionette": true,
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },

    "chrome" : {
      "desiredCapabilities": {
        "browserName": "chrome"
      }
    }
  }
}

有人可以帮我理解这里的问题吗?

标签: javascriptseleniumnightwatch.js

解决方案


通过在我的文件中添加"w3c": falseunder解决了这个问题。"chrome"nightwatch.json

特别感谢@Raju,nightwatch.conf.js他们的示例存储库中的文件有一个这样的例子。

我改变了这个:

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "marionette": true,
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }

对此:

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "marionette": true,
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "chromeOptions": {
            "w3c": false
        }
      }
    }

添加chromeOptions字段 plus"w3c": false现在一切都按预期工作。


推荐阅读