Selenium Course
- 62k Enrolled Learners
- Weekend/Weekday
- Live Class
Maintaining 1000 lines of code in a single class file is a cumbersome task and also it increases its complexity. In order to maintain the project structure and efficient performance of the selenium scripts, it is necessary to use different pages for different tasks. To ease the access of distributing the code into different modules, the Page object model comes to the rescue. In this article on Page Object Model in Selenium, you will be learning some of the core concepts of Page object Model and Page factory with the help of an example.
You may also go through this recording of Selenium Projects of the Selenium Training by Selenium Certified Experts where you can understand the topics in a detailed manner with examples.
This video talks about Page object model fundamentals, Page factory and its implementation with page object model.
Page Object Model is a design pattern in test automation to create an Object Repository for web UI elements. Every web page in the application should have a corresponding page class. This Page class will find the WebElements and also may contain page methods which perform operations on those WebElements.
The tests then use the methods of this page object class whenever they need to interact with the UI of that page. The benefit here is that, if the page UI changes, then the tests need not to be changed, only the code within the page object needs to be changed. After that, all the changes that support new UI are located in one place. That’s why locators and test scripts are stored separately.
Find out our Selenium Training in Top Cities/Countries
India | Other Cities/Countries |
Bangalore | US |
Hyderabad | UK |
Pune | Canada |
Chennai | Australia |
Mumbai | Singapore |
Kolkata | Edinburgh |
Now let’s see why do you need the Page Object Model.
The below-mentioned steps depict the need of Page Object Model in Selenium.
I hope you understood why do you need a page object model. Now, let’s move further and see some of the advantages of the Page Object Model in Selenium.
Advantages of the Page Object Model:
So these are a few of the advantages that make POM as unique and easy to work with for automation testers. Now, let’s dive further and understand what is Page Factory.
Page Factory is an inbuilt Page Object Model concept for Selenium WebDriver but it is very optimized. Here, you follow the concept of separation of Page Object Repository and Test Methods.
Additionally, with the help of the PageFactory class, I will use annotations @FindBy to find WebElement.
I hope you understand Page Factory in Selenium. Now, let’s dive deeper into this article and understand how the Page Object Model works with the help of the example below.
Scenario: Here, you need to enter the valid credentials in the ‘Facebook Login’ Page in order to redirect to the ‘Facebook Home‘ Page and then log out from the account.
Follow the below steps to implement the Page Object Model Design Pattern.
Step 1: Create TestBase class. Here I have created an object of WebDriver, maximize browser, implementing waits, launching URL and etc.
In the below example program, I have taken Chrome browser and set the System Property to launch Chrome browser.
package edureka.tests; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; public class TestBase { public static WebDriver driver = null; @BeforeSuite public void initialize() throws IOException{ System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"srctestjavadriverschromedriver.exe"); driver = new ChromeDriver(); //To maximize browser driver.manage().window().maximize(); //Implicit wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //To open facebook driver.get("https://www.facebook.com"); } @AfterSuite //Test cleanup public void TeardownTest() { TestBase.driver.quit(); } }
Step 2: Creating classes for each page (Eg., Facebook Login Page, Facebook Inbox Page) to hold element locators and their methods. Usually, you can create page objects for all available pages in the AUT. For each page,youe create a separate class with a constructor. Identify all the locators and keep them in one class. It allows us to reuse the locators in multiple methods and also helps us in easy maintenance, if there is any change in the UI, you can simply change on one Page.
Here, I have created java files (FacebookLoginPage.java and FacebookInboxPage.java) for the corresponding pages (Facebook Login Page, and Facebook Inbox Page) to hold element locators and their methods.
package edureka.pages; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; public class FbHomePage { WebDriver driver; public FbHomePage(WebDriver driver){ this.driver=driver; } //Using FindBy for locating elements @FindBy(how=How.XPATH, using="//div[text 1="Settings'" language="()='Account"][/text]") WebElement profileDropdown; @FindBy(how=How.XPATH, using="//text()[.='Log Out']/ancestor::span[1]") WebElement logoutLink; // Defining all the user actions (Methods) that can be performed in the Facebook home page // This method to click on Profile Dropdown public void clickOnProfileDropdown(){ profileDropdown.click(); } // This method to click on Logout link public void clickOnLogoutLink(){ logoutLink.click(); } }
package edureka.pages; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; public class FbLoginPage { WebDriver driver; public FbLoginPage(WebDriver driver){ this.driver=driver; } //Using FindBy for locating elements @FindBy(how=How.XPATH, using="//input[@type='email'][@name='email']") WebElement emailTextBox; @FindBy(how=How.XPATH, using="//input[@type='password'][@name='pass']") WebElement passwordTextBox; @FindBy(how=How.XPATH, using="//input[@type='submit'][@id='u_0_5']") WebElement signinButton; // Defining all the user actions (Methods) that can be performed in the Facebook home page</span> // This method is to set Email in the email text box public void setEmail(String strEmail){ emailTextBox.sendKeys(strEmail); } // This method is to set Password in the password text box public void setPassword(String strPassword){ passwordTextBox.sendKeys(strPassword); // This method is to click on Login Button public void clickOnLoginButton(){ signinButton.click(); } }
Step 3: Here, you need to create Test (Eg., FBLoginTest) based on the above pages. As per my test scenario which was mentioned above,test scripts run as follows.
package edureka.tests; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.Test; import pages.FbHomePage; import pages.FbLoginPage; public class FbLoginTest extends TestBase{ @Test public void init() throws Exception{ //driver.get("https://www.facebook.com"); FbLoginPage loginpage = PageFactory.initElements(driver, FbLoginPage.class); loginpage.setEmail("your-username"); loginpage.setPassword("your-password"); loginpage.clickOnLoginButton(); FbHomePage homepage = PageFactory.initElements(driver, FbHomePage.class); homepage.clickOnProfileDropdown(); homepage.verifyLoggedInUserNameText(); homepage.clickOnLogoutLink(); } }
Finally, you need to create the testng.xml file and link to the above created test case class files.
Step 4: Creating testng.xml file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Everjobs Suite"> <test name="Page Object Model Project"> <classes> <class name="edureka.tests.TestBase" /> <class name="edureka.tests.FbLoginTest" /> </classes> </test> </suite> <!-- Suite -->
On executing this testnG.xml file on test suite, it will redirect to facebook.com webpage and enter all the credentials. It will then verify username and then logout of the account. This is how Page Object Model can be implemented with Page Factory.
With this, we come to an end of this article on Page Object Model in Selenium. I hope you understood the concepts and it added value to your knowledge.
Got a question for us? Please mention it in the comments section of Page Object Model in Selenium article and we will get back to you.
Course Name | Date | Details |
---|---|---|
Selenium Course | Class Starts on 4th January,2025 4th January SAT&SUN (Weekend Batch) | View Details |
Selenium Course | Class Starts on 13th January,2025 13th January MON-FRI (Weekday Batch) | View Details |
edureka.co