【问题标题】:nightwatch.js is not working for phantom.jsnightwatch.js 不适用于 phantomjs
【发布时间】:2018-01-16 19:04:05
【问题描述】:

我使用 nightwatch.js 为我的产品自动化 e2e 测试用例。它在 chrome、firefox 和其他基于 UI 的浏览器上运行良好。但是,我需要在 phantom.js 上运行它,以便将它作为 Jenkins 的一部分作为自动化的无头浏览器运行。

我试过了,但是测试脚本无法与 phantom.js 一起使用。

测试脚本:

describe('TEST PHANTOMJS#',function() {

  afterEach((client,done) => {
    client.end(() => done());
  });

  it('successful test google.com',(client)=> {

    // Launch google.com
    client.url('https://www.google.com').resizeWindow(1000,800);
    console.log('Launched Google')
    client.expect.element('body1').to.be.present.before(1000); // test error
    console.log('Completed testing')
  });
});

我的 nightwatch.json 配置:

{
  "src_folders": [
    "tests"
  ],
  "output_folder": "reports",
  "custom_commands_path": "",
  "custom_assertions_path": "",
  "page_objects_path": "",
  "selenium": {
    "start_process": true,
    "server_path": "./bin/selenium/selenium-server-standalone-3.0.1.jar",
    "log_path": "",
    "port": 4444,
    "cli_args": {
      "webdriver.chrome.driver": "./bin/chrome/chromedriver",
      "webdriver.gecko.driver": "./bin/firefox/geckodriver",
      "webdriver.edge.driver": "./bin/ie/IEDriverServer.exe"
    }
  },
  "test_settings": {
    "default": {
      "selenium_port": 4444,
      "selenium_host": "localhost",
      "default_path_prefix": "/wd/hub",
      "silent": true,
      "screenshots": {
        "enabled": true,
        "on_failure": true,
        "path": "./screen-shots"
      },
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "phantomjs.binary.path": "./node_modules/phantomjs-prebuilt/bin/phantomjs",
        "phantomjs.page.settings.userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36",
        "phantomjs.cli.args": []
      },
      "test_runner": {
        "type": "mocha",
        "options": {
          "ui": "bdd",
          "reporter": "list"
        }
      }
    }
  }
}

运行./node_modules/.bin/nightwatch --env qa --verbose 后,我看到以下日志

> nightwatch --env qa --verbose

Starting selenium server... started - PID:  11037

    TEST PHANTOMJS# successful test google.com: Launched Google
Completed testing
INFO Request: POST /wd/hub/session 
 - data:  {"desiredCapabilities":{"browserName":"phantomjs","javascriptEnabled":true,"acceptSslCerts":true,"platform":"ANY","phantomjs.binary.path":"./node_modules/phantomjs-prebuilt/bin/phantomjs","phantomjs.page.settings.userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36","phantomjs.cli.args":[]}} 
 - headers:  {"Content-Type":"application/json; charset=utf-8","Content-Length":372}
INFO Response 200 POST /wd/hub/session (1409ms) { state: null,
  sessionId: 'd16c7439-18ec-4b67-85eb-e3dda6fe0075',
  hCode: 1253002783,
  value: 
   { applicationCacheEnabled: false,
     rotatable: false,
     handlesAlerts: false,
     databaseEnabled: false,
     version: '2.1.1',
     platform: 'MAC',
     browserConnectionEnabled: false,
     proxy: { proxyType: 'direct' },
     nativeEvents: true,
     acceptSslCerts: false,
     driverVersion: '1.2.0',
     'webdriver.remote.sessionid': 'd16c7439-18ec-4b67-85eb-e3dda6fe0075',
     'phantomjs.page.settings.userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
     locationContextEnabled: false,
     webStorageEnabled: false,
     browserName: 'phantomjs',
     takesScreenshot: true,
     driverName: 'ghostdriver',
     javascriptEnabled: true,
     cssSelectorsEnabled: true },
  class: 'org.openqa.selenium.remote.Response',
  status: 0 }
INFO Got sessionId from selenium d16c7439-18ec-4b67-85eb-e3dda6fe0075
INFO Request: POST /wd/hub/session/d16c7439-18ec-4b67-85eb-e3dda6fe0075/url 
 - data:  {"url":"https://www.google.com"} 
 - headers:  {"Content-Type":"application/json; charset=utf-8","Content-Length":32}

理想情况下,它应该通过报告错误来完成测试。然而,它被卡住了,没有进一步移动。

任何帮助将不胜感激。

【问题讨论】:

  • 太奇怪了,由于代理问题,它无法正常工作:-(现在正在尝试弄清楚如何为幻像 js 设置代理。

标签: phantomjs nightwatch.js


【解决方案1】:

当您在具有 HTTPS 的网站上使用 PhantomJS 时,通常必须使用 ignore-ssl-errors 选项运行脚本。否则,您经常会遇到麻烦...如果您的脚本适用于所有图形浏览器,但不适用于 PhantomJS,那么您的问题很可能与 SSL/TLS 有关。

nightwatch.json 中配置 PhantomJS,确保添加 CLI 选项:

"phantomjs.cli.args": ["--ignore-ssl-errors=true"]

如果没有该选项,以下脚本将无法工作(它不会打印页面标题),但添加后它会起作用:

module.exports = {
  'PhantomJS': function(browser) {
    browser
      .url('https://www.google.com')
      .waitForElementVisible('body', 1000)
      .getTitle(function (title) {
        console.log(title);
      })
      .end();
  }
};

【讨论】:

  • 我怀疑这是一个代理问题,我添加了“proxy”:{“proxyType”:“manual”,“httpProxy”:“proxy.mycompany.com:8080”}。它的工作。但现在我有另一个问题。我的 URL 被重定向到另一个 url,如何使用 phantomjs 支持重定向?
猜你喜欢
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多