So, I was just creating a simple selenium/JBehave code when a question appeared.
I'll first post the code simplified and then explain what is my question later.
So here we have a simple AbstractClass that will be inherited on my PageObjects. This class only contains a method to wait for certain elements on the page to be loaded. You can see how Im using it in the PageObject class (added a comment there).
AbstractPage.java
public abstract class AbstractPage {
public void waitPageLoad() {
WebDriverWait wait = new WebDriverWait(webDriverProvider.get(), 30);
wait.until(ExpectedConditions.visibilityOfAllElements(elementsToWait()));
}
protected List<WebElement> elementsToWait() {
return null;
}
}
PageObject.java
public class PageObject extends AbstractPage{
@FindBy(id = "webElement1")
private WebElement webElement1;
@FindBy(id = "webElement2")
private WebElement webElement2;
public void clickWebElement1() {
webElement1.click();
}
public void sendKeysWebElement2(String strKeys) {
webElement2.sendKeys(strKeys);
}
//Note how im using the elementsToWait here
@Override
protected List<WebElement> elementsToWait() {
return Arrays.asList(webElement1, webElement2);
}
}
Now on my steps, if I want to wait for the page to load first and then do the actions that i want, I'd need to call the 'waitPageLoad()' method from my abstract class inside one of the steps (or all of them to be sure).
PageObjectSteps.java
@Component
public class PageObjectSteps {
private PageObject pageObject;
@When("User wants to click on webElement1")
public void accountToDeposit () {
pageObject.waitPageLoad(); //Calling here just as an example
pageObject.clickWebElement1();
}
@When("User wants to type on webElement2 '$strType'")
public void amountToDeposit(@Named("strType") String strType) {
pageObject.sendKeysWebElement2(strType);
}
}
Now my question is:
Is there a way that I could call the waitPageLoad() everytime that my pageObject is used but WITHOUT calling the method on the steps?
For example, I'd have a different waitPageLoad() for each pageObject, depending on what I'd need to wait for. On this example I'd wait for the webElement1 and webElement2 to be visible.
Does selenium have something like: @AlwaysWait where I could use before a method and it would be called everytime the page object is used(again, WITHOUT calling it in the steps)? Or a notation that would make a method to be called everytime the page object is used?
Example:
@AlwaysWait
public void waitPageObjectLoad() {
WebDriverWait wait = new WebDriverWait(webDriverProvider.get(), 30);
wait.until(ExpectedConditions.visibilityOfAllElements(webElement1, webElement2));
}
Hopefully I made myself understandable, Thanks in advance.
PS: Asking around, I know that somehow you can use java reflections framework to do it, but I was wondering if you could do it with selenium only.