简体   繁体   English

Selenium RemoteWebDriver中的批处理/原子操作?

[英]Batch/atomic operations in Selenium RemoteWebDriver?

Looking at the source code of the RemoteWebDriver , every operation, including getting the text of a just-obtained WebElement , requires sending a separated command to the browser. 查看RemoteWebDriver的源代码,每个操作(包括获取刚获得的WebElement的文本)都需要向浏览器发送单独的命令。 I am worried that in a highly dynamic Javascript web app, this could result in inconsistent reads / random exceptions. 我担心在高度动态的Javascript Web应用程序中,这可能会导致读取/随机异常不一致。

Can anyone explain how Selenium calls are synchronized with the JavaScript event queue on the page? 谁能解释Selenium调用如何与页面上的JavaScript事件队列同步? If they're not synchronized, is there any way to do multiple Selenium calls in a batch/transaction/atomic operation apart from embedding large blobs of custom JavaScript? 如果它们不同步,除了嵌入大块的自定义JavaScript之外,还有什么方法可以在批处理/事务/原子操作中进行多个Selenium调用?

Selenium does not provide any facilities to ensure that any single Selenium operation is done atomically or that multiple operations are performed as part of a single atomic transaction. Selenium没有提供任何设施来确保任何单个Selenium操作是原子完成的,或者确保多个操作作为单个原子事务的一部分执行。 For instance, it is possible for a single click operation to fail if the element to be clicked moves while Selenium clicks it. 例如,如果要单击的元素在Selenium单击时移动,则单击操作可能会失败。

Yes, this can lead to problems. 是的,这可能会导致问题。 It is up to you to write your code so that it can recover from bad conditions. 您可以自行编写代码,以使其能够从恶劣的情况中恢复。 You need to know what it is you are interacting with and: 您需要知道与之交互的是什么,并且:

  1. Wait for a condition that indicates that the UI is stable. 等待表明用户界面稳定的条件。 For instance, I have Datatables tables on some pages. 例如,某些页面上有Datatables表。 One thing I check before testing that a table contains the right information is make sure the little banner that Datatables automatically popup when a table is being updated is not visible. 在测试表包含正确的信息之前,我检查的一件事是确保在更新表时Datatables自动弹出的小横幅不可见。 Otherwise, I may be checking a table that has not been updated yet and still contains old data. 否则,我可能正在检查一个尚未更新但仍包含旧数据的表。

  2. Detect a bad state and recover. 检测到不良状态并恢复。 In one case, I want to click a button in a part of the UI that is refreshed on the basis of various conditions. 在一种情况下,我想单击UI的一部分中的按钮,该按钮会根据各种条件进行刷新。 It does not matter whether I click the button before or after a refresh but the problem is that if a refresh happens between the time I get the button and the time I try to click it, Selenium will raise StaleElementReferenceException . 我是否在刷新之前或之后单击按钮都没有关系,但是问题是,如果在我获得按钮的时间与尝试单击按钮的时间之间发生刷新,Selenium将引发StaleElementReferenceException I have to catch this exception, reacquire the button and try to click it again. 我必须捕获此异常,重新获取按钮,然后尝试再次单击它。 (For the record, it is not worth in this case to test the UI before clicking. 99.999% of the time the click will go through without having to try it a second time.) (为便于记录,在这种情况下,在单击之前测试UI是不值得的。单击将有99.999%的点击经历而无需再次尝试。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM