Selenium WebDriver和通用等待或延迟(Selenium WebDriver and generic wait or delay)

编程入门 行业动态 更新时间:2024-10-19 14:51:28
Selenium WebDriver和通用等待或延迟(Selenium WebDriver and generic wait or delay)

所以我拼命寻找一种方法来延迟WebDriver中的一些执行,但我似乎找不到一个。

我尝试运行黑盒测试的Web应用程序,使用ajax调用,但这些ajax调用不会在DOM上呈现任何内容,因此我无法使用显式等待。 此外,隐式只适用于find_element语句,并且再次没有用。

我使用time.sleep()获得了成功,但我希望有一种更好的方法可以延迟执行。

So I'm desperately looking for a way to delay some executions in WebDriver but I don't seem to find one.

The web app which I try to run black box test against, works with ajax calls but these ajax calls do not render anything on DOM, thus I can't use explicit wait. Also, the implicit only works for find_element statements and again won't be useful.

I had success using time.sleep() but I hope there is a nicer way of delaying the execution.

最满意答案

根据我的理解(这是凌晨1点,我可能会遗漏一些东西),你需要你的测试与AngularJS同步,等待突出的请求和角度“安定下来”。

这就是说,在Javascript世界中, protractor完美地解决了 - 它总是知道Angular何时准备就绪,它使测试更自然,你甚至不考虑同步问题 - 它运行顺畅,开箱即用:

您不再需要为测试添加等待和睡眠。 在网页完成待处理任务时,量角器可以自动执行测试中的下一步,因此您不必担心等待测试和网页同步。

至于Python,有一个pytractor项目听起来像你应该评估的东西:

pytractor是Python的Selenium绑定的扩展。 它的目标是使用Python更容易地测试angular.js应用程序。

它建立在量角器的某些部分,即Angular.js的“官方”Javascript E2E / Scenario测试框架。

作为红旗,请注意项目未得到积极维护。 至少,您可以学习源代码并使用代码中引入的思想。

请注意,内部protractor和pytractor注入异步执行的客户端脚本。 为了等待Angular“准备好”,他们都使用angular.getTestability(el).whenStable() ( source )。


也可以看看:

了解Selenium中的执行异步脚本 在Python中使用Angular JS(Protractor)和Selenium 强制Selenium等待AngularJS

From what I understand (it's 1am here, I may miss something), you need your tests to be synchronized with AngularJS, waiting for outstanding requests and angular to "settle down".

This is what, in Javascript world, protractor solves perfectly - it always knows when Angular is ready and it makes the tests much more natural, you don't even think about synchronization issues - it works smoothly and out of the box:

You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.

As for Python, there is pytractor project that sounds like something you should evaluate:

pytractor is an extension to the Selenium bindings for Python. Its goal is to make testing of angular.js applications easier with Python.

It is built on some parts of protractor, the "official" Javascript E2E/Scenario testing framework for Angular.js.

As a red flag, note that the project is not actively maintained. At least, you may study the source and use the ideas introduced in the code.

Note that internally protractor and pytractor inject client-side scripts which are asynchronously executed. In order to wait for Angular to be "ready", they both use angular.getTestability(el).whenStable() (source).


See also:

Understanding execute async script in Selenium Using Angular JS(Protractor) with Selenium in Python Force Selenium to wait for AngularJS

更多推荐

本文发布于:2023-08-01 13:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WebDriver   Selenium   generic   delay   wait

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!