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: