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 12, 2019 in Selenium by Akshay
1,471 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 13, 2019 by Anvi
• 14,150 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 22, 2019 in Selenium by Abha
• 28,140 points
1,211 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
• 28,140 points
7,664 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
• 28,140 points
13,073 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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 29, 2019 in Selenium by Anvi
• 14,150 points
3,237 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,150 points
6,786 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP