Search code examples
seleniumselenium-webdrivertestngsoftware-quality

How to Set up POM Structure using selenium java and (TestNG Framework)


I'm new for QA automation and I have average knowledge in java, so I decided to use (Selenium+Java) to do automation. I will attach the code I did to the automation.and the script runs smoothly. But the Structure I did is incorrect as I want to follow the (POM-Selenium).POM-Page Oriented Model

This Script navigates as Follows

Login(Page)-->Peronal(Drop/down Selection)--->AddEdit((Drop/down Selection))-->Personal(Page)-->Add(Button)-->PersonalDetails(Page)

The test scenario is......

the user should "login" to the system, and have to click "personal" drop down in the navigation tab then there will be an "AddEdit" drop downselection.then the user is directed to the page titled as "personal", the user should click "Add" button on that page to get directed to another page called "personal Details" user can add the relevant fields which are provided from the page to add a new Client.

Please help me to arrange this to POM Structure.because I'm having very hard time to think how it goes.what frustrates me is in order user to add a relevant record he/she should be logged in to the system and in POM it says login is a separate page I have to navigate through three pages to complete the task.it would be great if you can help me out.and it's open to discussion and sorry if my English is bad. and please consider me as a total noob when it comes to automation :) thanks!

This is the Test Script I wrote...

package TestNG;

import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.openqa.selenium.*;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

//public String baseUrl;
//public  String driverPath;
//public WebDriver driver;

public class Tester {
    public String baseUrl = "http://xxx.xxx.xxx.xxx:xxxx/xxx/";
    public String driverPath = "C:\\Users\\User\\Desktop\\geckodriver.exe";
    public WebDriver driver;

    @Test(priority = 0)
    public void Login() {

        System.out.println("launching firefox browser");
        System.setProperty("webdriver.gecko.driver", driverPath);
        driver = new FirefoxDriver();
        driver.get(baseUrl);
        WebElement username = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"userId\"]")));
        username.sendKeys("xxxxxxxxx");
        WebElement password = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"loginPassword\"]")));
        password.sendKeys("xxxxxxxxxxxxxxxxxx");
        WebElement button = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                .presenceOfElementLocated(By.xpath("/html/body/div/div[3]/div/div/form/div[3]/div[2]/div/button")));
        button.click();
        String expectedTitle = "xxxxxxxxxxxxxxxxxxxxxxxx";
        String actualTitle = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[2]/div/div/h1")))
                .getText().toString();
        // System.out.println(actualTitle);
        Assert.assertEquals(actualTitle, expectedTitle);

        // driver.close();

    }

    @Test(priority = 1)
    public void Personal_Tab_Selection() {

        clickWhenReady("/html/body/div[2]/div[2]/nav/div/div[2]/div/div[1]/ul/li[5]/a", 10, driver);

    }

    @Test(priority = 2)
    public void Add_Edit_Selection() {

        clickLinkByHref("/rsa/5/15/staff/n/i/list", driver);

    }

    @Test(priority = 3)
    public void Add_Button() {
        clickWhenReady("/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/a", 10, driver);

    }

    @Test(priority = 4)
    public void Radio_Button_AMW() {
        WebElement amw_radio = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[1]/div/div/label[1]")));
        amw_radio.click();
    }

    @Test(priority = 5)
    public void Radio_Button_service_provider() {
        WebElement service_provider = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[2]/div/div/label[1]")));
        service_provider.click();

    }

    @Test(priority = 6)
    public void Service_Provider_Name_select() {

        WebElement Service_Provider_DD = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(
                        By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[3]/div/div/a")));
        Service_Provider_DD.click();

        driver.findElement(By.cssSelector("ul > li:nth-child(2)")).click();

    }

    @Test(priority = 7)
    public void Employee_Code_Enter() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[5]/div/input"))
                .sendKeys("01112");

    }

    @Test(priority = 8)
    public void Click_Salutation() {
        WebElement Salutation_DD = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/a")));
        Salutation_DD.click();
    }

    @Test(priority = 9)
    public void Salutation_Click() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/div/ul/li[5]"))
                .click();

    }

    @Test(priority = 10)
    public void employee_name() {

        WebElement empname = driver
                .findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[7]/div/input"));
        empname.sendKeys("Test2");
    }

    @Test(priority = 11)
    public void Sap_plant_code() {
        WebElement plant_code = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/a")));
        plant_code.click();

    }

    @Test(priority = 12)
    public void sap_code_set() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/div/ul/li[3]"))
                .click();

    }

    @Test(priority = 13)
    public void sap_vendor_code() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[9]/div/input"))
                .sendKeys("test_2");

    }

    @Test(priority = 14)
    public void employee_role_select() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/ul")).click();

    }

    @Test(priority = 15)
    public void select_Technician_role() {

        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/div/ul/li"))
                .click();

    }

    @Test(priority = 16)
    public void select_status() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/a")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/div/ul/li[1]"))
                .click();

    }

    @Test(priority = 17)
    public void click_save_button() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/button[1]")).click();

    }

    @Test(priority = 18)
    public void Record_add_notification_Check() {

        if ((new WebDriverWait(driver, 10)).until(ExpectedConditions
                .presenceOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/div/button"))) != null) {
            driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/button")).click();
        } else {
            infoBox("not Added", "Not Added");
        }

    }

    public static void infoBox(String infoMessage, String titleBar) {
        JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
    }

    public static void clickWhenReady(String location, int timeout, WebDriver driver) {

        WebElement element = null;

        WebDriverWait wait = new WebDriverWait(driver, timeout);

        element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(location)));

        element.click();

    }

    public static void clickLinkByHref(String href, WebDriver driver) {

        List<WebElement> anchors = driver.findElements(By.tagName("a"));

        Iterator<WebElement> i = anchors.iterator();

        while (i.hasNext()) {
            WebElement anchor = i.next();
            if (anchor.getAttribute("href").contains(href)) {
                anchor.click();

                break;
            }

        }

    }

}

Solution

  • A few words about your script:

    1. presenceOfElementLocated(By.xpath("//*[@id=\"userId\"]")) - If an element have an id you should always try to use id instead of xpath

    2. By.xpath("/html/body/div/div[3]/div/div/form/div[3]/div[2]/div/button")) - It is recommended to use logical xpath instead of absolute xpath as much as possible. Else your xpath becomes vulnerable.

    3. Now as you are willing to follow the POM, you need to define all the elements of a page in a single page which will be called the PageFactory. Likewise, all the Elements of a webpage will reside in separate class.

    For e.g. an entry for an element on a webpage may look like:

    @FindBy(id="user_login") 
    WebElement username;
    
    1. As you are using TestNG, you can move the Browser related code to a seperate class Browserfactory. From your test class, within @BeforeTest Annotation call the methods of Browserfactory to initialize the browser, open the url.

    These are some of the basic steps to implement your code through POM.

    Let me know if this solves your query.