简体   繁体   中英

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. I am worried that in a highly dynamic Javascript web app, this could result in inconsistent reads / random exceptions.

Can anyone explain how Selenium calls are synchronized with the JavaScript event queue on the page? 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?

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. For instance, it is possible for a single click operation to fail if the element to be clicked moves while Selenium clicks it.

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. 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. 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. 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 . 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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