Open Bisllly opened 1 year ago
You need builder to actualize prototype in test file. Supposed you need to test the add button, just come up with the prototype first
Find.element(StudentUI.ADD_STUDENT_BUTTON).click();
then create Find
class, along with FindBuilder
public class Find extends BaseActions {
private String action;
public Find(WebDriver driver) { super(driver); }
public static FindBuilder element(String addStudentButton) {
return new FindBuilder(addStudentButton);
}
public static class FindBuilder {
private String addStudentButton;
public FindBuilder(String addStudentButton) { this.addStudentButton = addStudentButton; }
public void click() {
findElementByXpath(addStudentButton).click();
}
}
}
Then you can extend it with Actions. Along with using Page, UI, Entity, etc.
package actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Find {
private WebDriver driver;
private String locator;
private Find(WebDriver driver) {
this.driver = driver;
}
public static Find using(WebDriver driver) {
return new Find(driver);
}
public WebElement element(String locator) {
return driver.findElement(By.xpath(locator));
}
public Find click() {
WebElement element = element(locator);
element.click();
return this;
}
}
Find.using(driver).element(LogInPage.NON_SSO_BTN).click();
Find
class to encapsulate the functionality.WebDriver
instance.Start by creating the Find
class.
Identify the required fields:
WebDriver driver
to store the WebDriver
instance.String locator
to store the locator value.Create a private constructor to accept a WebDriver
instance and initialize the driver
field.
Implement a static using()
method that takes a WebDriver
instance and returns a new Find
instance with the provided WebDriver
set.
Implement the element()
method that takes a locator as a parameter and uses the driver.findElement()
method with the XPath locator to locate and return the corresponding WebElement
.
Implement the click()
method:
element()
method with the stored locator.WebElement
by calling its click()
method.Find
instance (this
) to support method chaining.Find.using(driver)
.element(LogInPage.NON_SSO_BTN)
.click();
That's the step-by-step process to devise the code. It involves identifying the desired usage, determining the necessary components, and implementing the methods to support the desired functionality and method chaining.
Here is an example of how you could structure your testing files using the POM: