在nightwatch phantomjs浏览器上进行基本测试失败

时间:2016-08-04 13:14:12

标签: node.js selenium phantomjs nightwatch.js

我正在使用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"时,测试通过,"确定。 3个断言通过。"。 但是当它是" phantomjs"时,一个通过,一个失败,一个错误在执行期间:"测试失败:执行期间出错1次,断言1次失败,1次失败。 (3.511s)"

有错误:

  1. 等待元素存在1000毫秒时超时。 - 预期"可见"但得到了:"未找到"

  2. 错误:无法找到元素:"输入[type = text]"使用:css选择器

  3. 我手动启动服务器。

    我测试了最新版本的phantomjs(2.1.1)和1.9.8

    编辑:

    使用phantomjs时,Selenium服务器也会报告此错误:

    WebElementLocator - _handleLocateCommand - 找不到的元素:GAVE UP。

2 个答案:

答案 0 :(得分:2)

我过去曾经这样做过,并且观察到phantomjs浏览器的默认大小非常小。您可以尝试调整浏览器大小并检查会发生什么。

如下所示,

L

答案 1 :(得分:0)

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

<div class="box">
  <div class="box-header">
    <a href="#" class="export">Export Table data into Excel</a>
  </div>
  <!-- /.box-header -->
  <div class="box-body dvData" id="dvData">
    <table id="example1" class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>Rendering engine</th>
          <th>Browser</th>
          <th>Platform(s)</th>
          <th>Engine version</th>
          <th>CSS grade</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Trident</td>
          <td>Internet Explorer 4.0
          </td>
          <td>Win 95+</td>
          <td> 4</td>
          <td>X</td>
        </tr>
        <tr>
          <td>Trident</td>
          <td>Internet Explorer 5.0
          </td>
          <td>Win 95+</td>
          <td>5</td>
          <td>C</td>
        </tr>
        <tr>
          <td>Trident</td>
          <td>Internet Explorer 5.5
          </td>
          <td>Win 95+</td>
          <td>5.5</td>
          <td>A</td>
        </tr>
        <tr>
          <td>Trident</td>
          <td>Internet Explorer 6
          </td>
          <td>Win 98+</td>
          <td>6</td>
          <td>A</td>
        </tr>
        <tr>
          <td>Trident</td>
          <td>Internet Explorer 7</td>
          <td>Win XP SP2+</td>
          <td>7</td>
          <td>A</td>
        </tr>

        </tfoot>
    </table>
  </div>
  <!-- /.box-body -->
</div>
<!-- /.box -->