【问题标题】:Cannot use setValue() in nightwatch.js不能在 nightwatch.js 中使用 setValue()
【发布时间】:2020-06-06 06:03:48
【问题描述】:

我正在尝试在 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) 在 Object.request (http.js:42:10) 在 HttpRequest.createHttpRequest 在新的承诺 () 在 Selenium2Protocol.sendProtocolAction

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

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

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

这个用户和我的问题几乎一模一样,但没有答案:setValue method in Nightwatch is not working

我使用的是chromedriver版本80.0.3987.16

我正在使用nightwatch版本1.3.4

我使用npm install chromedriver 安装了chromedriver 到npm,并在我的nightwatch.json 文件中将路径设置为chromedriver.exe:

{
  "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"
      }
    }
  }
}

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

【问题讨论】:

  • 你是通过npm下载chromedriver还是手动下载?
  • 我通过 npm --npm install -g chromedriver 安装了 chromedriver,并在nightwatch.json 中为"webdriver.chrome.driver": "C:/Path/To/chromedriver.exe" 设置了路径。我已经编辑了我的问题以包含我的完整 nightwatch.json 文件。
  • 我无法在 npm 中找到该版本。我不确定在全球范围内安装它是否有任何影响。我有项目github.com/spnraju/nightwatchjs-selenium-example/blob/master/…。我已将您的测试添加到其中,并且运行良好。我正在检查最新版本的 chromedriver。
  • 也适用于最新版本 (80.0.1) 的 chromedriver。尝试卸载当前的 chromedriver 并安装最新版本。
  • 感谢您根据自己的回购检查。我将检查我的环境变量并确保我的路径中也没有杂散的chromedriver.exe。运行chromedriver -v 给我输出ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185})

标签: javascript selenium nightwatch.js


【解决方案1】:

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

特别感谢@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 字段加上"w3c": false 现在一切都按预期工作了。

【讨论】:

  • ChromeDriver / Chrome 正在从 OSS 过渡到 W3C。 "w3c": false 不是理想的解决方案。
  • 我很乐意提供更好的解决方案 - 这个问题仍然没有公认的答案。我对守夜人还是很陌生,所以我不确定约定是什么。我在 chrome 75 的发行说明 github.com/nightwatchjs/nightwatch/releases/tag/v1.1.12 中看到了这个问题,但我的问题是 chrome 80。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多