How can I create a POM test case in Selenium Webdriver?

0 votes
Can anyone suggest how can I create a POM test case in Selenium Webdriver?
May 11, 2019 in Selenium by Akshay
200 views

1 answer to this question.

0 votes

Hey Akshay, follow these steps to create a POM test case in Selenium Webdriver:

1. Create a New Package file and name it as ‘pageObjects’, by right clicking on the Project and select New > Package. Keep in mind to create different packages for Page Objects, Utilities, Test Data, Test Cases and Modular actions. It's recommended to use this structure, as it is easy to understand, easy to use and easy to maintain.

2. Create a New Class file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. For eg. In our case it is Home Page and LogIn Page.

3. Now create a Static Method for each Element (Object) in the Home Page. Each method will have an Argument (driver) and a Return value (element).

package pageObjects;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
public class Home_Page {
    private static WebElement element = null;
public static WebElement lnk_MyAccount(WebDriver driver){
    element = driver.findElement(By.id("account"));
    return element;
    }
public static WebElement lnk_LogOut(WebDriver driver){
    element = driver.findElement(By.id("account_logout"));
return element;
    }
} 

4. Next create a New Class and name it as POM_TC by right click on the ‘automationFramework‘ Package and select New > Class. We will be creating all our test cases under this package. 

package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import pageObjects.Home_Page;
import pageObjects.LogIn_Page;

public class PageObjectModel {
   private static WebDriver driver = null;
   public static void main(String[] args) {
     driver = new FirefoxDriver();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.get("http://www.store.demoqa.com");
     // Use page Object library now
     Home_Page.lnk_MyAccount(driver).click();
     LogIn_Page.txtbx_UserName(driver).sendKeys("testuser_1");
     LogIn_Page.txtbx_Password(driver).sendKeys("Test@123");
     LogIn_Page.btn_LogIn(driver).click();
     System.out.println(" Login Successfully, now it is the time to Log Off buddy.")
     Home_Page.lnk_LogOut(driver).click();
     driver.quit();
   }
}

5. Finally your Project Explorer would look like this:

answered May 12, 2019 by Anvi
• 14,010 points

Related Questions In Selenium

0 votes
1 answer

How to create a test case with TestNG in Selenium?

Hey Deepak, to create a Test Case ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 27,800 points
137 views
0 votes
1 answer

How to pass parameter through testng.xml file to a test case in Selenium Webdriver?

Hello Revathi, to pass parameter through testng.xml ...READ MORE

answered Jun 12, 2019 in Selenium by Abha
• 27,800 points
1,432 views
0 votes
1 answer
0 votes
1 answer

How can I read a CSV file in Selenium using Webdriver?

Hey Trisha, you can read a CSV ...READ MORE

answered Jul 17, 2019 in Selenium by Abha
• 27,800 points
1,872 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,141 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
1,787 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
1,747 views
0 votes
1 answer

How can I refresh a browser window in different ways using Selenium Webdriver?

Hello Piyush, you can refresh a browser ...READ MORE

answered May 28, 2019 in Selenium by Anvi
• 14,010 points
346 views
0 votes
1 answer

How can I clear the text in a text box using Selenium WebDriver?

Hello Akriti, you can clear the text ...READ MORE

answered May 29, 2019 in Selenium by Anvi
• 14,010 points
652 views