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 in Selenium by Akshay
80 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 by Anvi
• 13,960 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
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 in Selenium by Abha
• 27,180 points
159 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,110 points
1,007 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,130 points
1,153 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,130 points
893 views
0 votes
1 answer
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 in Selenium by Anvi
• 13,960 points
313 views