【问题标题】:Basic test fails on nightwatch phantomjs browsernightwatch phantomjs 浏览器的基本测试失败
【发布时间】:2016-08-04 13:14:12
【问题描述】:

我正在使用 selenium 服务器,node.js 上的 nightwatch 和 selenium 的 phantomjs。我仅将 phantomjs 用于无头浏览器(使用它 selenium 在运行测试时不会以可视方式打开浏览器)。

当我使用 firefox 作为浏览器时,我的基本测试通过了,但当我使用 phantomjs 浏览器时失败了。

我设置了基本的谷歌测试:

module.exports = {
  'Demo test Google' : function (browser) {
    browser
      .url('http://www.google.com')
      .waitForElementVisible('body', 1000)
      .setValue('input[type=text]', 'nightwatch')
      .waitForElementVisible('button[name=btnG]', 1000)
      .click('button[name=btnG]')
      .pause(1000)
      .assert.containsText('#main', 'Night Watch')
      .end();
  }
};

并设置 phantomjs 服务器。这是我的 nightwatch.json:

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

  "selenium" : {
    "start_process" : false,
    "server_path" : "selenium-server-standalone-2.53.1.jar",
    "log_path" : "",
    "host" : "127.0.0.1",
    "port" : 3001,
    "cli_args" : {
      "webdriver.chrome.driver" : "",
      "webdriver.ie.driver" : ""
    }
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 3001,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "phantomjs.binary.path" : "phantomjs.exe"       
      }
    }
  }
}

当我将 browserName 设置为“firefox”时,测试通过,“OK。3 个断言通过。”。 但是当是“phantomjs”时,一个通过,一个失败,一个错误:“TEST FAILURE: 1 error during execution, 1 assertions failed, 1 pass. (3.511s)”。

有错误:

  1. 在等待元素出现 1000 毫秒时超时。 - 预期“可见”但得到:“未找到”

  2. 错误:无法使用 css 选择器定位元素:“input[type=text]”

我手动启动服务器。

我使用最新版本的 phantomjs (2.1.1) 和 1.9.8 进行了测试

编辑:

Selenium 服务器在使用 phantomjs 时也报此错误:

WebElementLocator - _handleLocateCommand - 未找到元素:放弃。

【问题讨论】:

    标签: node.js selenium phantomjs nightwatch.js


    【解决方案1】:

    我过去遇到过这样的事情,并观察到 ​​phantomjs 浏览器的默认大小非常小。您能否尝试调整浏览器大小并检查会发生什么。

    如下所示,

    module.exports = { 'Demo test Google' : function (browser) { browser .url('http://www.google.com') .resizeWindow(1000, 800); .waitForElementVisible('body', 1000) .setValue('input[type=text]', 'nightwatch') .waitForElementVisible('button[name=btnG]', 1000) .click('button[name=btnG]') .pause(1000) .assert.containsText('#main', 'Night Watch') .end(); } };

    【讨论】:

    • tnx 为响应,但不起作用,和以前一样。顺便提一句。我从 nightwatch 得到的回应是:“可能 google 正在做一些基于 User-Agent 的 javascript 魔术,以避免自动抓取或类似情况。”
    • 抱歉没用!!如果您在此找到相关内容,请更新此主题。
    • @Shri 感谢 .resizeWindow(1000, 800) 为我工作:D
    【解决方案2】:

    是的,调整浏览器窗口的大小可能会解决这个问题。我们在 Phantom JS 中运行我们的测试套件,并在初始化浏览器窗口时在代码中进行了设置:

    client.maximizeWindow();
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多