com.java.xero.EmployeeCreationPage.java Source code

Java tutorial

Introduction

Here is the source code for com.java.xero.EmployeeCreationPage.java

Source

/*
 * EmployeeCreationPage.java
 * version : 1.0
 * author : Sandeep C Matanavar
 * license : Open
 */
package com.java.xero;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class EmployeeCreationPage {
    private WebDriver driver;
    WebDriverWait wait = null;

    public EmployeeCreationPage(WebDriver driver) {
        this.driver = driver;
    }

    private String payRollDropdown = "//a[@id='Payroll']/parent::*";
    private String employeesSelection = "//a[contains(text(),'Employees') and @data-type='menu-focus']";
    private String addEmployeeButton = "//a[contains(text(),'Add Employee') and @class='btn btn-blue']";
    private String firstNameTextBox = "//input[@data-xid='firstGivenName']";
    private String lastNameTextBox = "//input[@data-xid='surname']";
    private String birthDayTextBox = "//input[@data-xid='birthday']";
    private String monthDropdown = "//a[contains(text(),'Month')]";
    private String yearTextBox = "//input[@data-xid='birthyear']";
    private String addressLineTextBox = "//input[@data-xid='addressLine1']";
    private String suburbTextBox = "//input[@data-xid='suburb']";
    private String stateDropdown = "//a[contains(text(),'Select')]";
    private String postCodeTextBox = "//input[@data-xid='postCode']";
    private String saveButton = "//button[contains(text(),'Save')]";

    // Function to Create employee
    // Idea here is to find the web elements and mimic the user action while
    // creating employee
    public boolean EmployeeCreationWithMandatoryFields(String organizationName, String sFirstName, String sLastName,
            String sBirthDay, String sMonth, String sYear, String sGender, String sAddress, String sSuburb,
            String sState, String sPostCode) {
        boolean flag = false;
        try {
            WebElement organisationNameElement = driver
                    .findElement(By.xpath("//a[contains(text(),'" + organizationName + "') and @class='name']"));
            organisationNameElement.click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(payRollDropdown)).click();
            // Select mySelectEmp= new Select(payRollDropdown);
            // mySelectEmp.selectByVisibleText("Employees");

            driver.findElement(By.xpath(employeesSelection)).click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(addEmployeeButton)).click();

            Thread.sleep(5000);
            driver.findElement(By.xpath(firstNameTextBox)).clear();
            driver.findElement(By.xpath(firstNameTextBox)).sendKeys(sFirstName);

            driver.findElement(By.xpath(lastNameTextBox)).clear();
            driver.findElement(By.xpath(lastNameTextBox)).sendKeys(sLastName);

            driver.findElement(By.xpath(birthDayTextBox)).clear();
            driver.findElement(By.xpath(birthDayTextBox)).sendKeys(sBirthDay);

            driver.findElement(By.xpath(monthDropdown)).click();
            // Select mySelectMonth= new Select(monthDropdown);
            // mySelectMonth.selectByVisibleText(sMonth);

            WebElement monthSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sMonth + "')]"));
            monthSelection.click();

            driver.findElement(By.xpath(yearTextBox)).clear();
            driver.findElement(By.xpath(yearTextBox)).sendKeys(sYear);

            WebElement genderRadioButton = driver
                    .findElement(By.xpath("//input[@type='radio' and @value='" + sGender + "']"));
            genderRadioButton.click();

            driver.findElement(By.xpath(addressLineTextBox)).clear();
            driver.findElement(By.xpath(addressLineTextBox)).sendKeys(sAddress);

            driver.findElement(By.xpath(suburbTextBox)).clear();
            driver.findElement(By.xpath(suburbTextBox)).sendKeys(sSuburb);

            driver.findElement(By.xpath(stateDropdown)).click();
            // Select mySelectState= new Select(stateDropdown);
            // mySelectState.selectByVisibleText(sState);

            WebElement stateSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sState + "')]"));
            stateSelection.click();

            driver.findElement(By.xpath(postCodeTextBox)).clear();
            driver.findElement(By.xpath(postCodeTextBox)).sendKeys(sPostCode);

            driver.findElement(By.xpath(saveButton)).click();
            Thread.sleep(5000);

            String employeeSaveValidationXpath = "//p[contains(text(),'" + sFirstName + " " + sLastName
                    + " has been saved successfully.')]";
            if (driver.findElements(By.xpath(employeeSaveValidationXpath)).size() != 0) {
                flag = true;
                System.out.println("Employee created successfully");
            } else
                System.out.println("Failed to create an employee");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    private String titleTextBox = "//input[@data-xid='title']";
    private String middleNameTextBox = "//input[@data-xid='otherGivenNames']";
    private String jobTitleTextBox = "//input[@data-xid='jobTitle']";
    private String phoneNumberTextBox = "//input[@data-xid='telephone']";
    private String mobileNumberTextBox = "//input[@data-xid='mobile']";
    private String emailTextBox = "//input[@data-xid='email']";
    private String addressLine2 = "//input[@data-xid='addressLine2']";

    public boolean EmployeeCreationWithAllFields(String organizationName, String sTitle, String sFirstName,
            String sMiddleName, String sLastName, String sBirthDay, String sMonth, String sYear, String sJobTitle,
            String sGender, String sPhoneNumber, String sMobileNumber, String sEmail, String sAddress1,
            String sAddress2, String sSuburb, String sState, String sPostCode) {
        boolean flag = false;
        try {
            wait = new WebDriverWait(driver, 100);

            WebElement organisationNameElement = driver
                    .findElement(By.xpath("//a[contains(text(),'" + organizationName + "') and @class='name']"));
            wait.until(ExpectedConditions.visibilityOf(organisationNameElement));
            organisationNameElement.click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(payRollDropdown)).click();

            driver.findElement(By.xpath(employeesSelection)).click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(addEmployeeButton)).click();
            Thread.sleep(5000);

            driver.findElement(By.xpath(titleTextBox)).clear();
            driver.findElement(By.xpath(titleTextBox)).sendKeys(sTitle);

            driver.findElement(By.xpath(firstNameTextBox)).clear();
            driver.findElement(By.xpath(firstNameTextBox)).sendKeys(sFirstName);

            driver.findElement(By.xpath(middleNameTextBox)).clear();
            driver.findElement(By.xpath(middleNameTextBox)).sendKeys(sMiddleName);

            driver.findElement(By.xpath(lastNameTextBox)).clear();
            driver.findElement(By.xpath(lastNameTextBox)).sendKeys(sLastName);

            driver.findElement(By.xpath(birthDayTextBox)).clear();
            driver.findElement(By.xpath(birthDayTextBox)).sendKeys(sBirthDay);

            driver.findElement(By.xpath(monthDropdown)).click();
            // Select mySelectMonth= new Select(monthDropdown);
            // mySelectMonth.selectByVisibleText(sMonth);

            WebElement monthSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sMonth + "')]"));
            monthSelection.click();

            driver.findElement(By.xpath(yearTextBox)).clear();
            driver.findElement(By.xpath(yearTextBox)).sendKeys(sYear);

            driver.findElement(By.xpath(jobTitleTextBox)).clear();
            driver.findElement(By.xpath(jobTitleTextBox)).sendKeys(sJobTitle);

            WebElement genderRadioButton = driver
                    .findElement(By.xpath("//input[@type='radio' and @value='" + sGender + "']"));
            genderRadioButton.click();

            driver.findElement(By.xpath(phoneNumberTextBox)).clear();
            driver.findElement(By.xpath(phoneNumberTextBox)).sendKeys(sPhoneNumber);

            driver.findElement(By.xpath(mobileNumberTextBox)).clear();
            driver.findElement(By.xpath(mobileNumberTextBox)).sendKeys(sMobileNumber);

            driver.findElement(By.xpath(emailTextBox)).clear();
            driver.findElement(By.xpath(emailTextBox)).sendKeys(sEmail);

            driver.findElement(By.xpath(addressLineTextBox)).clear();
            driver.findElement(By.xpath(addressLineTextBox)).sendKeys(sAddress1);

            driver.findElement(By.xpath(addressLine2)).clear();
            driver.findElement(By.xpath(addressLine2)).sendKeys(sAddress2);

            driver.findElement(By.xpath(suburbTextBox)).clear();
            driver.findElement(By.xpath(suburbTextBox)).sendKeys(sSuburb);

            driver.findElement(By.xpath(stateDropdown)).click();

            WebElement stateSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sState + "')]"));
            stateSelection.click();

            driver.findElement(By.xpath(postCodeTextBox)).clear();
            driver.findElement(By.xpath(postCodeTextBox)).sendKeys(sPostCode);

            driver.findElement(By.xpath(saveButton)).click();
            Thread.sleep(5000);

            String employeeSaveValidationXpath = "//p[contains(text(),'" + sFirstName + " " + sLastName
                    + " has been saved successfully.')]";
            if (driver.findElements(By.xpath(employeeSaveValidationXpath)).size() != 0) {
                flag = true;
                System.out.println("Employee created successfully");
            } else
                System.out.println("Failed to create an employee");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    private String errorMessageText = "//p[contains(text(),'There are multiple errors on the page that require your attention.')]";

    public boolean EmployeeCreationWithoutFillingAnyFields(String organizationName) {
        boolean flag = false;
        try {
            wait = new WebDriverWait(driver, 100);

            WebElement organisationNameElement = driver
                    .findElement(By.xpath("//a[contains(text(),'" + organizationName + "') and @class='name']"));
            wait.until(ExpectedConditions.visibilityOf(organisationNameElement));
            organisationNameElement.click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(payRollDropdown)).click();

            driver.findElement(By.xpath(employeesSelection)).click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(addEmployeeButton)).click();
            Thread.sleep(5000);

            driver.findElement(By.xpath(saveButton)).click();
            Thread.sleep(5000);
            if (driver.findElements(By.xpath(errorMessageText)).size() != 0) {
                flag = true;
            } else
                System.out.println("Failed!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    private String deleteButton = "//button[@data-xid='action-delete']";
    private String confirmationPopupDeleteButton = "//p[contains(text(),'Are you sure you want to delete this employee?')]/following::button[contains(text(),'Delete')]";

    public boolean EmployeeDeletion(String organizationName, String sFirstName, String sLastName, String sBirthDay,
            String sMonth, String sYear, String sGender, String sAddress, String sSuburb, String sState,
            String sPostCode) {
        boolean flag = false;
        try {
            WebElement organisationNameElement = driver
                    .findElement(By.xpath("//a[contains(text(),'" + organizationName + "') and @class='name']"));
            organisationNameElement.click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(payRollDropdown)).click();

            driver.findElement(By.xpath(employeesSelection)).click();

            Thread.sleep(10000);
            driver.findElement(By.xpath(addEmployeeButton)).click();

            Thread.sleep(5000);
            driver.findElement(By.xpath(firstNameTextBox)).clear();
            driver.findElement(By.xpath(firstNameTextBox)).sendKeys(sFirstName);

            driver.findElement(By.xpath(lastNameTextBox)).clear();
            driver.findElement(By.xpath(lastNameTextBox)).sendKeys(sLastName);

            driver.findElement(By.xpath(birthDayTextBox)).clear();
            driver.findElement(By.xpath(birthDayTextBox)).sendKeys(sBirthDay);

            driver.findElement(By.xpath(monthDropdown)).click();

            WebElement monthSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sMonth + "')]"));
            monthSelection.click();

            driver.findElement(By.xpath(yearTextBox)).clear();
            driver.findElement(By.xpath(yearTextBox)).sendKeys(sYear);

            WebElement genderRadioButton = driver
                    .findElement(By.xpath("//input[@type='radio' and @value='" + sGender + "']"));
            genderRadioButton.click();

            driver.findElement(By.xpath(addressLineTextBox)).clear();
            driver.findElement(By.xpath(addressLineTextBox)).sendKeys(sAddress);

            driver.findElement(By.xpath(suburbTextBox)).clear();
            driver.findElement(By.xpath(suburbTextBox)).sendKeys(sSuburb);

            driver.findElement(By.xpath(stateDropdown)).click();

            WebElement stateSelection = driver.findElement(
                    By.xpath("//span[@class='xg-menu-item-text' and contains(text(),'" + sState + "')]"));
            stateSelection.click();

            driver.findElement(By.xpath(postCodeTextBox)).clear();
            driver.findElement(By.xpath(postCodeTextBox)).sendKeys(sPostCode);

            driver.findElement(By.xpath(saveButton)).click();
            Thread.sleep(5000);

            String employeeSaveValidationXpath = "//p[contains(text(),'" + sFirstName + " " + sLastName
                    + " has been saved successfully.')]";
            System.out.println("employeeSaveValidationXpath value is " + employeeSaveValidationXpath);
            if (driver.findElements(By.xpath(employeeSaveValidationXpath)).size() != 0) {
                System.out.println("Employee created successfully");
                String employeeDeletedMessageXpath = "//p[contains(text(),'" + sFirstName + " " + sLastName
                        + " has been successfully deleted.')]";
                driver.findElement(By.xpath(deleteButton)).click();
                Thread.sleep(5000);

                // Alert alert= driver.switchTo().alert();
                driver.findElement(By.xpath(confirmationPopupDeleteButton)).click();
                Thread.sleep(5000);
                if (driver.findElements(By.xpath(employeeDeletedMessageXpath)).size() != 0) {
                    flag = true;
                    System.out.println("Employee deleted successfully");
                }
            } else
                System.out.println("Failed to create an employee");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}