I am trying to figure out how to use explicit wait in Selenium using the POM structure. I tried to google, but could not find any leads.
2 Answers
The Page Object pattern is about creating an API for your pages, to create functions named after actions you can take in the page.
Naturally, to perform certain actions, you need to wait certain elements to be ready to be acted upon. Thus, you can simply add the waits inside the functions specifically:
class PaymentPage {
fun fillUpForm(creditCardNumber) {
creditCardNumberField.fill(creditCardNumber);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElement(greenCheckmarkForValidCreditCardNumber);
performPaymentButton.click();
}
}
}
The function fillUpForm will hide all necessary knowledge to perform the action of filling up the form, its clients (tests) need to know only that it needs to pass the credit card number. If some other condition come to be necessary before clicking the perform payment button, you need to change only the fillUpForm.
According to security testing services, one of the ways to implement POM in a framework using Selenium and Java,is to create a Resusable Resources class which will include all the explicit waits, click, type functionalities and we can import that class in all the required page classes and use the same.
To implement explicit wait,write a function as follows in your reusable resources class and call it in the pages as and when required.
public static WebDriverWait wait;
public static void setExplicitWait(int seconds){
wait = new WebDriverWait(driver, seconds);
}
Now in the pages, to call this function, we call follow this approach:-
ReusableResources.setExplicitWait(10).until(ExpectedConditions.elementToBeClickable(abcButton));
This functionality will wait for 10 seconds before throwing any error.
This sums up the concept of using Explicit waits in POM.
-
Your link doesn't talk anything about the topic nor is a reference for the information that you gave. Commented Nov 18, 2021 at 13:24
PageFactory
implementation of POM. If so, you can take a look at this example.