Selenium Java Page Object Model with Page Factory
File: BasePOM.java
package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class BasePOM 
{
    public static WebDriver driver=null;
    @BeforeClass
    public void OpenBrowser()
    {
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://newtours.demoaut.com");
    }
    @AfterClass
    public void CloseBrowser()
    {
        driver.close();
    }
}
File: Test1VerifyHomePageTitle.java
package tests;

import org.testng.annotations.Test;

public class Test1VerifyHomePageTitle extends BasePOM
{
    @Test
    public void VerifyTitle()
    {
        if(driver.getTitle().equals("Welcome: Mercury Tours"))
        {
            System.out.println("Title Matched");
        }
        else
        {
            System.out.println("Title Not Matched");
        }       
    }
}
File: Test2SubmitFindFlightDetails.java
package tests;

import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import pages.FindFlightPage;
import pages.HomePage;

public class Test2SubmitFindFlightDetails extends BasePOM
{
    @BeforeMethod
    public void Login()
    {
        HomePage mhp = PageFactory.initElements(driver, HomePage.class);
        mhp.setUserName("mercury");
        mhp.setPassword("mercury");
        mhp.clickLoginButton();
    }
    @Test
    public void FindFlight()
    {   
        FindFlightPage ffp = PageFactory.initElements(driver, FindFlightPage.class);
        ffp.TripType("oneway");
        ffp.SelectFromIn("London", 3);
        ffp.ServiceClass("Business");
        ffp.ClickFindFlights();
    }
}
File: Test3SelectFlight.java
package tests;

import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;

import pages.SelectFlight;

public class Test3SelectFlight extends Test2SubmitFindFlightDetails
{
    @AfterMethod
    public void SelectDepartReturn()
    {
        SelectFlight sf = PageFactory.initElements(driver, SelectFlight.class);
        sf.SelectDepartFlight("Pangea");
        sf.SelectReturnFlight("631");
        sf.ClickReserveFlights();
    }
}
File: HomePage.java
package 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 HomePage 
{
    WebDriver driver;
    public HomePage(WebDriver driver)
    {
        this.driver=driver;
    }
    @FindBy(how = How.XPATH, using = "//input[@name='userName']") WebElement username;
    @FindBy(how = How.XPATH, using = "//input[@name='password']") WebElement password;
    @FindBy(how = How.XPATH, using = "//input[@name='login']") WebElement login;
    
    public void setUserName(String uname)
    {
        username.sendKeys(uname);
    }
    public void setPassword(String passwd)
    {
        password.sendKeys(passwd);
    }
    public void clickLoginButton()
    {
        login.click();
    }
}
File: FindFlightPage.java
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;

import tests.BasePOM;

public class FindFlightPage extends BasePOM
{
    WebDriver driver;
    public FindFlightPage(WebDriver driver)
    {
        this.driver=driver;
    }
    @FindBy(how = How.XPATH, using = "//input[@name='findFlights']") WebElement findflights;
    
    public void TripType(String ttype)
    {
        driver.findElement(By.cssSelector("input[value='"+ttype+"']")).click();
    }
    public void SelectFromIn(String from, int to)
    {
        Select from_dropdown = new Select(driver.findElement(By.name("fromPort")));
        from_dropdown.selectByVisibleText(from);
        
        Select in_dropdown = new Select(driver.findElement(By.name("toPort")));
        in_dropdown.selectByIndex(to);
    }
    public void ServiceClass(String sclass)
    {
        driver.findElement(By.cssSelector("input[value='"+sclass+"']")).click();
    }
    public void ClickFindFlights()
    {
        findflights.click();
    }
}
File: SelectFlight.java
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class SelectFlight 
{
    WebDriver driver;
    public SelectFlight(WebDriver driver)
    {
        this.driver=driver;
    }
    @FindBy(how = How.XPATH, using = "//input[@name='reserveFlights']") WebElement reserveflights;

    public void SelectDepartFlight(String name)
    {
        driver.findElement(By.xpath("//*[@name='outFlight' and contains(@value,'"+name+"')]")).click();
    }
    public void SelectReturnFlight(String name)
    {
        driver.findElement(By.xpath("//*[@name='inFlight' and contains(@value,'"+name+"')]")).click();
    }
    public void ClickReserveFlights()
    {
        reserveflights.click();
    }
}
File: testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test">
    <test name="Page Object Model">
        <classes>
            <class name="tests.Test1VerifyHomePageTitle"/>
            <class name="tests.Test2SubmitFindFlightDetails"/>
            <class name="tests.Test3SelectFlight"/>
        </classes>
    </test>
</suite>
Output
[RemoteTestNG] detected TestNG version 7.0.0

Title Matched

===============================================
Test
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================


Advertisement