Skip to main content

ExpectedConditions in Selenium

How do you usually check that a page is displayed in selenium or not? Most of us would check that the Page Title and the URL is correct:



String expUrl = “https://testersdigest.blogspot.com”;

String expTitle = “My Testing Stories”;



WebDriverWait wait = new WebDriverWait(driver, 15);



wait.until(ExpectedConditions.titleIs(expTitle));

wait.until(ExpectedConditions.urlToBe(expUrl));



First-line here will validate the title check, next one for the URL. But, using the ExpectedConditions class which allows combining the 2 lines we can do it more cleaner:



wait.until(ExpectedConditions.and(ExpectedConditions.urlToBe(expUrl),ExpectedConditions.titleIs(expTitle))); //ExpectedConditions.and() waits until multiple conditions are true.



Not only and() but we also we have or() and not() for ExpectedConditions.



-- wait.until(ExpectedConditions.or(ExpectedConditions.urlToBe(expUrl),ExpectedConditions.titleIs(expTitle))); //waits until at least a condition is true.


-- wait.until(ExpectedConditions.not(ExpectedConditions.urlToBe(expUrl))); //waits until the condition is not true.



Comments