将量角器测试移至异步/等待状态(保留硒控制流程) [英] Moving Protractor tests to async/await (leaving selenium control flow)

查看:124
本文介绍了将量角器测试移至异步/等待状态(保留硒控制流程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在量角器测试中从硒控制流程中移除appart.

这是我到目前为止所经历的步骤和问题:

1.禁用逐个测试

我的第一个想法是使用SELENIUM_PROMISE_MANAGER以便从硒承诺转移到异步/等待代码.

不幸的是,我没有办法对SELENIUM_PROMISE_MANAGER=0进行一项测试,而将其他测试保持在SELENIUM_PROMISE_MANAGER=1以便顺利进行迁移.

2.保留它,但也使用异步/等待

所以我想到了在保持SELENIUM_PROMISE_MANAGER=1的同时使用async/await的代码.

不幸的是,它似乎不兼容: https://github.com/angular/protractor/tree/master/exampleTypescript/asyncAwait

关于硒方面的问题没有任何内容,所以我想知道这是一个建议还是真正的它总是失败.

3.覆盖承诺.USE_PROMISE_MANAGER

使用protractor.promise.USE_PROMISE_MANAGER=false对我不起作用(在beforeEach中).

4.调用异步代码上的控制流

protractor.promise.controlFlow().execute(/* my async code*/);

对我来说,似乎混合使用async/await来控制流...

什么是正确的策略?

我想知道,在量角器测试中转移到异步/等待的正确策略是什么?

我不想将所有内容异步/等待一次更改,而是将更改分散到多个小更改中

解决方案

不兼容,因为

此问题中阅读所有内容.

由于它是非常技术性的,所以我(相对不熟练的人)理解的方式是jasminewd2protractorwebDriver包括. ControlFlow不能同时处理native promisesmanaged promises.它仅处理managed promises,并且由许多jasminewd2-命令创建.但是,async/await创建native promises并将其推入ControlFlow,后者以两种promise类型的混合结束,并且最终都带来了严重的计时问题.由于ControlFlow可以按预期方式工作并且无法按预期运行,因此无法解决. async/await的情况从来都不是ControlFlow设计的一部分.

对我来说,关闭ControlFlow意味着managed promises被忽略.但是由于async/await,任何managed promise都将包裹在native promise内,因此没有关系,当您仅通过跟踪native promises来跟踪所有异步任务时,将忽略managed promises.

如何同时测试已迁移和尚未迁移的测试用例

创建两个单独的conf.js(或任何您的配置文件命名)并将它们分开.如果您有共同的部分(即helper函数),则也要迁移这些部分,并且可能会将未迁移的部分也保留为重复部分. (创建为.js文件或使用布尔值指示符执行​​或)

@DubZzz发现了一个可能的技巧,可以平稳地移动到async / await.他的技巧是使用async / await转换helpers并在protractor.promise.controlFlow().execute中调用它们.然后逐个测试地移动,最后删除controlFlow的执行和标志.

对于迁移,我可以提供以下链接:

官方介绍和迁移指南

答案明确的类似问题

关于异步/等待和诺言的好博客

有关弃用Promise Manager的官方主题.它还包含有关如何使异步/等待工作的有用信息.

I want to move appart from the selenium control flow in my Protractor tests.

Here are the steps and problems I went through so far:

1. Disable it test by test

My first idea was to use SELENIUM_PROMISE_MANAGER in order to move from selenium promises to async/await code.

Unfortunately, I found no way to put one test to SELENIUM_PROMISE_MANAGER=0 while keeping others to SELENIUM_PROMISE_MANAGER=1 in order to do the migration smoothly.

2. Keep it but use async/await too

So I thought about a code using async/await while keeping SELENIUM_PROMISE_MANAGER=1.

Unfortunately it seems not to be compatible: https://github.com/angular/protractor/tree/master/exampleTypescript/asyncAwait

There is nothing about such issue on Selenium side so I am wondering if this is an advice or a real it always fails.

3. Override promise.USE_PROMISE_MANAGER

Using protractor.promise.USE_PROMISE_MANAGER=false does not work for me (in beforeEach).

4. Call the control flow on async code

protractor.promise.controlFlow().execute(/* my async code*/);

For me it seems to mix async/await to control flow...

What is the right strategy?

I am wondering, what is the right strategy to move to async/await in Protractor tests?

I don't want to put everything in async/await in one change but to spread the change in multiple small changes

解决方案

Incompatible, because

Read all about in this issue here.

As it's very technical, the way I (as relatively untechnical guy) understood it, is that jasminewd2, protractor and webDriver incl. ControlFlow are not designed to handle a mix of native promises and managed promises. It handle only managed promises and such are created by many jasminewd2-commands. However, async/await creates native promises and pushes it into the ControlFlow, which ends in a mix of both promises type and it all ends up with heavy timing issues. It can't be resolved as the ControlFlow works as expected and as it was designed for. The situation with async/await was never part of the ControlFlow design.

Switching off the ControlFlow to me means, managed promises get ignored. But because of async/await any managed promise would be wrapped inside a native promise and therefore it doesn't matter, that managed promises get ignored as you keep track of all asynchronous tasks already by tracking native promises only.

How to test both, migrated and not yet migrated test cases

Create two separate conf.js (or whatever your config file is named) and keep them separated. If you have common parts, i.e. helper functions, also migrate those and potentially keep the un-migrated as a duplicate as well. (create to .js-files or use a boolean-indicator to execute either or)

@DubZzz found a possible trick to move smoothly to async / await. His trick is to convert helpers using async / await and call them in protractor.promise.controlFlow().execute. Then move test by test and finally remove controlFlow execute and flag.

For Migration these links I can provide:

The official introduction and migration guide

A similar question witha well documented answer

A good blog about async/await and promises

The official thread about deprecate promise Manager. It also contains helpful information in how to make async/await work.

这篇关于将量角器测试移至异步/等待状态(保留硒控制流程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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