简体   繁体   English

带有 protractorjs 的 Angular 2 失败:等待 Protractor 与页面同步时出错:“window.getAllAngularTestabilities 不是函数”

[英]Angular 2 with protractorjs Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

[Testing angular 2 web app] [测试 Angular 2 网络应用程序]

This error occurs with browser.ignoreSynchronization = false when set to true, this error does not occur, why is this ?当设置为true时browser.ignoreSynchronization = false会发生此错误,不会发生此错误,这是为什么?

I also have useAllAngular2AppRoots: true set in my protractor config.我还在量角器配置中设置了useAllAngular2AppRoots: true

Protractor runs on top of WebDriverJS. Protractor 在 WebDriverJS 之上运行。 WebDriverJS is a Javascript interface equivalent to Java's Webdriver that let's you control browsers programmatically, which in turn helps to write automated test cases WebDriverJS 是一个相当于 Java 的 Webdriver 的 Javascript 接口,它可以让您以编程方式控制浏览器,从而有助于编写自动化测试用例

The problem in testing Angular apps using WebDriverJS is that Angular has its own event loop separate from the browser's.使用 WebDriverJS 测试 Angular 应用程序的问题是 Angular 有自己的事件循环,与浏览器的事件循环分开。 This means that when you execute WebDriverJS commands, Angular might still be doing its thing.这意味着当你执行 WebDriverJS 命令时,Angular 可能仍在做它的事情。

One work around for this is by telling WebDriverJS to wait for an arbitrary amount of time (ie 3000ms) so that Angular finishes its work of rendering but this was not the right way of doing things.Therefore Protractor was created to synchronize your tests with Angular's event loop, by deferring running your next command until after Angular has finished processing the previous one.解决此问题的一种方法是告诉 WebDriverJS 等待任意时间(即 3000 毫秒),以便 Angular 完成其渲染工作,但这不是正确的做事方式。因此,创建 Protractor 是为了将您的测试与 Angular 同步事件循环,通过推迟运行你的下一个命令,直到 Angular 完成处理前一个命令。

But again there is a catch here, this becomes problematic when you are testing Non-Angular Applications, Protractor keeps on waiting for angular to synchronise even though there is no Angular to complete its cycle and will throw an error which you are observing!但是这里还有一个问题,当您测试非 Angular 应用程序时,这会成为问题,即使没有 Angular 完成其周期,量角器也会继续等待 Angular 同步,并且会抛出您正在观察的错误!

Thus, for non-Angular pages, you can tell Protractor not to look for Angular by setting browser.ignoreSynchronization = true -- which in practical terms will mean that you're just using WebDriverJS.因此,对于非 Angular 页面,您可以通过设置browser.ignoreSynchronization = true来告诉 Protractor 不要寻找 Angular——这实际上意味着您只是在使用 WebDriverJS。

So by adding that to your configuration when Protractor cannot find Angular on your page, you're giving up on all that makes testing Angular apps easier than plain WebDriverJS.因此,当 Protractor 在您的页面上找不到 Angular 时,将其添加到您的配置中,您将放弃所有使测试 Angular 应用程序比普通 WebDriverJS 更容易的东西。 And yes, adding browser.sleep after all your commands will likely work, but it's cumbersome, will break as soon as Angular takes longer than the pause you set, and makes your tests take excessively long.是的,在你的所有命令之后添加 browser.sleep 可能会起作用,但它很麻烦,一旦 Angular 花费的时间超过你设置的暂停时间就会中断,并使你的测试花费过长。

Conclusion : Only use browser.ignoreSynchronization = true when testing a page that does not use Angular.结论:仅在测试不使用 Angular 的页面时使用browser.ignoreSynchronization = true

Reference: https://vincenttunru.com/Error-Error-while-waiting-for-Protractor-to-sync-with-the-page/参考: https ://vincenttunru.com/Error-Error-while-waiting-for-Protractor-to-sync-with-the-page/

You should absolutely make sure that your page only gets loaded one single time on the test.您绝对应该确保您的页面在测试中只加载一次。 We had this problem with a login mockup that caused the loaded page to reload right after the first load was done inside the boostrap code of the Angular 2 application.我们在登录模型中遇到了这个问题,导致加载的页面在 Angular 2 应用程序的boostrap代码中完成第一次加载后立即重新加载。 This caused all sorts of unpredictable behaviour with tests failing with timeouts or the above error - or running fine.这导致了各种不可预知的行为,测试因超时或上述错误而失败 - 或运行正常。

So, make sure you have a consistent page lifecycle prior to the test.因此,请确保您在测试之前具有一致的页面生命周期。

To extend on the point @ram-pasala made:延伸@ram-pasala 提出的观点:

One work around for this is by telling WebDriverJS to wait for an arbitrary amount of time (ie 3000ms) so that Angular finishes its work of rendering but this was not the right way of doing things解决此问题的一种方法是告诉 WebDriverJS 等待任意时间(即 3000 毫秒),以便 Angular 完成其渲染工作,但这不是正确的做事方式

Here's how waiting for the function to become available might look like.以下是等待该功能可用的样子。 It's for TestCafe, but should be easy to adapt to other testing frameworks:它适用于 TestCafe,但应该很容易适应其他测试框架:

export const waitForAngular = ClientFunction((timeoutMs: number = 60_000) => {
  const waitForGetAllAngularTestabilities = (timeoutMs: number) => {
    if (timeoutMs <= 0) throw new Error('Waited for window.getAllAngularTestabilities, but timed out.')
    const win = (window as any);
    if (win.getAllAngularTestabilities) {
      return Promise.resolve(win.getAllAngularTestabilities)
    } else {
      return new Promise(res => setTimeout(() => res(true), 100)).then(() => waitForGetAllAngularTestabilities(timeoutMs - 100))
    }
  }
  return waitForGetAllAngularTestabilities(30_000).then((getAllAngularTestabilities) => {
    return Promise.all(
      getAllAngularTestabilities().map(
        (t) =>
          new Promise((res, rej) => {
            setTimeout(() => rej(`Waited ${timeoutMs}ms for Angular to stabilize but timed out.`), timeoutMs);
            return t.whenStable(() => {
              res(true);
              console.log('Angular stable');
            });
          }),
      ),
    )
  })
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败:等待量角器与页面同步时出错:“在窗口中找不到角度” - Failed: Error while waiting for Protractor to sync with the page: “angular could not be found on the window” 量角器“等待量角器与页面同步时出错”浏览Angular站点 - Protractor “Error while waiting for Protractor to sync with the page” browsing Angular site 等待量角器与页面同步时出错:在运行量角器测试用例时“在窗口中找不到角度” - Error while waiting for Protractor to sync with the page: “angular could not be found on the window” on running protractor test case 执行量角器测试时出现“失败:等待量角器与页面同步时出错” - "Failed: Error while waiting for Protractor to sync with the page" while executing Protractor tests 在等待量角器与页面同步时刷新时发生错误,在捕获警报时 - Error while waiting for protractor to sync with the page on catching alert during refresh 使用量角器时,单击导致发出http请求的按钮时,我收到此消息:等待量角器同步时出错 - While using Protractor, when clicking a button which causes an http request to be made, I receive this: error while waiting for protractor to sync 使用$ resource时,量角器会等待与页面同步 - Protractor times out waiting for sync with page when using $resource 量角器-页面对象:失败:不是函数 - Protractor - Page object: Failed: is not a function 量角器不等待页面加载 - Protractor not waiting for page load 量角器Angular 2失败:未知错误:未定义角度 - Protractor Angular 2 Failed: unknown error: angular is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM