Protractor 在 AngularJS 之外的可用性 [英] Usability of Protractor outside of AngularJS

查看:13
本文介绍了Protractor 在 AngularJS 之外的可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近从使用 AngularJS 切换到了 ReactJS,但我真的很喜欢使用 Protractor E2E 测试运行器,所以我想知道关于 Protractor 的两件事.

So I have recently switched from using AngularJS to ReactJS but I did really like working with the Protractor E2E test runner so I was wondering 2 things about Protractor.

在一个完全不使用 AngularJS 的网站上使用 Protractor 有什么大问题吗?我知道 Protractor 默认会尝试与 Angular 同步,你会得到:

Are there any major issues with using Protractor on a site that does not use AngularJS at all? I know that Protractor by default tries to synchronous with Angular and you get the:

Error: Angular could not be found on the page X : retries looking for angular exceeded

type message 但是我相信可以通过在 browser.get() 之前执行 browser.ignoreSynchronization = true 来防止这种情况.除此之外还有其他问题吗?

type message however I believe that can be prevented by doing browser.ignoreSynchronization = true before browser.get(). Are there any other issues besides that?

另一个问题是 Protractor 是否会成为 AngularJS 特有的,例如,它只能测试 AngularJS 代码?我认为任何 AngularJS 的特定功能都可以被绕过(就像你可以使用 browser.ignoreSynchronization = true 一样),我只是想确保这是 Protractor 未来的核心目标.

The other question is will Protractor ever become AngularJS specific, as in, it can only test AngularJS code? I would think that any AngularJS specific feature can be bypassed (like you can with browser.ignoreSynchronization = true), I just want to make sure that is a core goal of Protractor going forward.

推荐答案

虽然 Protractor 是为 Angular 应用程序编写端到端测试代码而设计的,但它仍然可以用于测试非 Angular 应用程序.

Although Protractor is designed to write end-to-end testing code for Angular application, it still can be used to test non-Angular application.

有两种常见的解决方案来实现这一点:

There are two common solutions to achieve this:

browser.driver.find(By.id('test'));

为方便起见,您可以将其导出到全局命名空间并通过别名访问它.

For convenience, you can export it to to the global namespace and access it by alias name.

onPrepare: function () {
  global.drv = browser.driver;
}

停止等待 Angular 完成工作

如您所述,browser.ignoreSynchronization = true 可以禁用 Protractor 的默认等待行为.当您的测试代码不专用于非 Angular 应用程序时,您采用的解决方案(在 browser.get() 之前执行 browser.ignoreSynchronization = true)可能会导致错误.browser.ignoreSynchronization 是一个全局范围设置,这意味着一旦您更改它的值,这个标志将影响整个测试套件.因此,除非在每个 Angular 测试中相应地更新标志,否则您的 Angular 测试会出现一些错误.

Stop waiting for Angular to finish its work

As you mentioned, browser.ignoreSynchronization = true can disable the default waiting behavior of Protractor. The solution you adopted (doing browser.ignoreSynchronization = true before browser.get()) may cause error when your testing code is not dedicated to non-Angular application. browser.ignoreSynchronization is a global scope setting which means that this flag will affect the whole test suite once you change its value. Thus your Angular tests will get some error unless the flag gets updated accordingly in every Angular test.

试试这种优雅而灵活的方式.

Try this elegant and flexible way.

在你的 config.js 中定义一个 ignoreSynchronization 标志设置函数

Define a ignoreSynchronization flag setter function in your config.js

onPrepare = function () {
  global.isAngularApp = function (flag) {
    return browser.ignoreSynchronization = flag;
  }
}

您可以在测试代码中确定标志的值

And you can determine the value of the flag in your test code

beforeEach(function() {
  isAngularApp(false);  //for non-Angular site
});

对于您的非 Angular 应用,您现在需要自行保持同步.您可以通过采用对用户更直观的方法来做到这一点,您现在等待元素出现/消失/具有价值/没有价值/等等.用户会看到并做出反应的所有事物.您可以使用 Selenium 的隐式或显式等待以及适当的 ExpectedConditions 类来完成此操作.这些是 Selenium Docs 提供的详细信息.

For your non-angular apps, you will now need to maintain synchronization on your own. You can do so by taking a more visual-to-the-user approach where you now wait on elements to appear/disappear/have a value/have no value/etc. All things that a user would see and react to. You can accomplish this with Selenium's implicit or explicit waits and the appropriate ExpectedConditions class. These are details by the Selenium Docs.

希望对你有帮助.

这篇关于Protractor 在 AngularJS 之外的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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