AngularJS之外的量角器的可用性 [英] Usability of Protractor outside of AngularJS

查看:65
本文介绍了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的网站上使用量角器是否存在重大问题?我知道默认情况下,量角器会尝试与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消息,但是我相信可以通过在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?

另一个问题是,量角器是否会成为特定于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可以禁用量角器的默认等待行为.当您的测试代码不专用于非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
});

对于非角度应用程序,您现在需要自己维护同步.您可以采用一种对用户更直观的方法来实现,即您现在等待元素出现/消失/具有一个值/没有任何值/等等.用户会看到并作出反应的所有事物.您可以通过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.

希望对您有帮助.

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

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