top of page
Search

Wait for an expected function condition in Selenium

Updated: Jul 29

Have you ever in a situation that you need to wait for a result in order to check if we should to wait for more time or stop waiting? And what if that result is returned by a function?

Example:

  • A table contains a list of customer, each customer item has a button to delete.

  • We have to wait for the list appear on the table and we click button delete of the bottom customer

Solution:

  • We write a method (A) to check the number of rows displayed on the table, if > 0, returns true, if not, returns false.

  • We write a wait function (B), input that method and wait time. Inside the wait function, it will check the return value of method A, if true, the wait completes, if false, it continues to wait until it met the given wait time.

My code:


The first method is to wait until the expected condition with time out in seconds:

In which:

  1. WebDriverWait is a built-in class of Selenium Wait.

  2. getBrowser() is to get the current WebDriver object.

The second method is to call the criteria method that needs to wait:

And we can apply it like:

So it will wait for the method (contains an IF-ELSE). If it returns true, the wait completes, if return false, it will wait and check again until the time is out.

28 views0 comments

Comments


bottom of page