获取错误:等待量角器与页面同步时出错:{} [英] Getting error: Error while waiting for Protractor to sync with the page: {}

查看:23
本文介绍了获取错误:等待量角器与页面同步时出错:{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 e2e.conf.coffee 文件是:

exports.config =
  baseUrl: 'http://localhost:9001'
  specs: [
    'e2e/**/*.coffee'
  ]

  framework: 'jasmine'

我的节点项目在端口 9001 上运行和侦听.

I have my node project running and listening on port 9001.

我的测试是:

describe 'Happy Path', ->
  it 'should show the login page', ->
    console.log browser

    expect(browser.getLocationAbsUrl()).toMatch("/view1");
  it 'should fail to login', ->
    setTimeout ->
      console.log "FAIL!"
    , 1200

我得到的错误是:

Failures:

  1) Happy Path should show the login page
   Message:
     Error: Error while waiting for Protractor to sync with the page: {}
   Stacktrace:
     Error: Error while waiting for Protractor to sync with the page: {}
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
==== async task ====
WebDriver.executeScript()
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
==== async task ====
Asynchronous test function: it("should show the login page")
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>
    at <anonymous>==== async task ====

我做错了什么??

推荐答案

(非常)简短的版本: 使用 browser.driver.get 而不是 browser.get.

更长的版本: Protractor 基本上是 Selenium 及其 Javascript WebDriver 代码的包装器.Protractor 添加代码以等待 Angular稳定下来"(即,完成它的 $digest 循环),然后再继续您的测试代码.但是,如果您的页面上没有 Angular,那么 Protractor 将永远"等待(实际上只是直到它超时)等待 Angular 稳定下来.

The longer version: Protractor is basically a wrapper around Selenium and its Javascript WebDriver code. Protractor adds code to wait for Angular to "settle down" (i.e., finish going through its $digest loops) before proceeding with your test code. However, if your page doesn't have Angular on it, then Protractor will wait "forever" (actually just until it times out) waiting for Angular to settle.

Protractor 向您的测试公开的 browser 对象是 Protractor 的一个实例(即,如果您在 Stack Overflow 上看到 var ptor = protractor.getInstance(); ptor.doSomething(),然后您可以在那些旧答案中用 browser 替换 ptor ).Protractor 还将封装的 Selenium WebDriver API 公开为 browser.driver.因此,如果您调用 browser.get,您正在使用 Protractor(它会等待 Angular 稳定下来),但是如果您调用 browser.driver.get,您'正在使用 Selenium(它不了解 Angular).

The browser object that Protractor exposes to your test is an instance of Protractor (i.e., if you see old answers on Stack Overflow with var ptor = protractor.getInstance(); ptor.doSomething(), then you can replace ptor with browser in those old answers). Protractor also exposes the wrapped Selenium WebDriver API as browser.driver. So if you call browser.get, you're using Protractor (and it will wait for Angular to settle down), but if you call browser.driver.get, you're using Selenium (which does not know about Angular).

大多数时候,您将测试 Angular 页面,因此您需要使用 browser.get 来获得 Protractor 的好处.但是,如果您的登录页面根本不使用 Angular,那么您应该在测试登录页面的测试中使用 browser.driver.get 而不是 browser.get.请注意,在测试的其余部分中,您还需要使用 Selenium API 而不是量角器 API:例如,如果您的页面中某处有一个 id="username" 的 HTML 输入元素,您将需要使用 browser.driver.findElement(by.id('username')) 而不是 element(by.model('username')) 访问它.

Most of the time, you'll be testing Angular pages, so you'll want to use browser.get to get the benefits of Protractor. But if your login page doesn't use Angular at all, then you should be using browser.driver.get instead of browser.get in the tests that test your login page. Do note that you'll also need to use the Selenium API rather than the Protractor API in the rest of the test: for example, if you have an HTML input element with id="username" somewhere in your page, you'll want to access it with browser.driver.findElement(by.id('username')) instead of element(by.model('username')).

有关更多示例,请参阅量角器测试套件中的此示例(或尝试此链接(如果前一个链接消失).另请参阅量角器文档,其中说明:

For more examples, see this example from the Protractor test suite (or try this link if the previous one ever goes away). See also the Protractor docs which state:

当在页面上找不到 Angular 库时,Protractor 将失败.如果您的测试需要与非角度页面交互,请直接使用 browser.driver 访问 webdriver 实例.

Protractor will fail when it cannot find the Angular library on a page. If your test needs to interact with a non-angular page, access the webdriver instance directly with browser.driver.

示例代码:在上面的登录测试中,您可能希望执行以下操作:

Example code: In your login test above, you would want to do something like:

describe 'Logging in', ->
  it 'should show the login page', ->
    browser.driver.get "http://my.site/login.html"
    // Wait for a specific element to appear before moving on
    browser.driver.wait ->
      browser.driver.isElementPresent(by.id("username"))
    , 1200
    expect(browser.driver.getCurrentUrl()).toMatch("/login.html");
  it 'should login', ->
    // We're still on the login page after running the previous test
    browser.driver.findElement(by.id("username")).sendKeys("some_username")
    browser.driver.findElement(by.id("password")).sendKeys("some_password")
    browser.driver.findElement(by.xpath('//input[@type="submit"]')).click()

(注意:我没有做过多少 CoffeeScript,完全有可能我在上面的代码中犯了一个 CoffeeScript 语法错误.你可能想在盲目复制和粘贴之前检查它的语法.我然而,我对逻辑充满信心,因为它几乎是从我测试非 Angular 登录页面的 Javascript 代码中逐字复制和粘贴的.)

(A note of caution: I haven't done much CoffeeScript, and it's entirely possible I made a CoffeeScript syntax error in the code above. You may want to check its syntax before blindly copying and pasting it. I am, however, confident in the logic, because that's copied and pasted almost verbatim from my Javascript code that tests a non-Angular login page.)

这篇关于获取错误:等待量角器与页面同步时出错:{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆