Page Object Model with Page Factory
Hello everyone, I’m going to talk about Page Factory model using Selenium Webdriver. So we will search on google.
PageFactory is a concept of Page object model that enables creating object storage, sustainable, reusable and managing elements from one place. It is used to initialize elements of a Page class without having to use ‘FindElement’ or ‘FindElements’. Annotations can be used to supply descriptive names of target objects to improve code readability.The PageFactory model detects Web elements with @FindBy annotation.
Let’s start now.
Pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
BasePage ->first we create a folder named base. Then we create the Base Page class inside the base folder.
public class BasePage {
public String baseUrl = "https://www.google.com/";
public WebDriver driver;
@BeforeTest
public void setBaseUrl() {
driver = new ChromeDriver();
driver.get(baseUrl);
}
@AfterTest
public void endSession() {
driver.quit();
}
HomePage ->first we create a folder named pages. Then we create the HomePage class inside the pages folder.
public class HomePage {
WebDriver _driver;
public HomePage(WebDriver driver) {
this._driver=driver;
PageFactory.initElements(_driver, this);
}
@FindBy(name = "q")
private WebElement searchGoogle;
@FindBy(xpath = "//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']")
private WebElement searchClick;
public void setGoogleTextbox(String searchTextbox) {
searchGoogle.sendKeys(searchTextbox);
}
public void searchClick() {
searchClick.click();
}
}
This class is important because we create the page factory here.We started a page factory with constructive method. You might think that. How can I do without using the Page factory model?There is a difference in readability.The following structure was made without using the page object model. For now, you might not see much difference. But you can see the benefit as the codes increase.
public class HomePage {
private static WebElement element = null;
public static void setGoogleTextbox(WebDriver driver,String searchTextbox) {
element = driver.findElement(By.name("q"));
element.sendKeys(searchTextbox);
}
public static void searchClick(WebDriver driver) {
element = driver.findElement(By.xpath("//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']"));
element.click();
}
}
HomePageTest ->first we create a folder named tests. Then we create the HomePageTest class inside the tests folder. We write the operations we do in this class.
public class HomePageTest extends BasePage {
@Test
public void searchGoogle() {
HomePage homePage = new HomePage(driver);
homePage.setGoogleTextbox("Test Automation");
homePage.searchClick();
}
}
You can find the full code on github : https://github.com/yavercemal/PageFactory
Then run the HomePageTest class.