com.java.xerotests.EmployeeCreationTests.java Source code

Java tutorial

Introduction

Here is the source code for com.java.xerotests.EmployeeCreationTests.java

Source

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.testng.annotations.*;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import static org.testng.AssertJUnit.assertEquals;
import com.java.utils.TestSuiteSetup;
import com.java.xero.*;
import com.java.testdata.*;

public class EmployeeCreationTests {

    WebDriver driver;
    GeneralFunctions generalFunctions = null;
    EmployeeCreationPage employeeCreationPage = null;

    String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    String sTimeAppend = null;

    // Testcase variables
    private String sTestCaseID = null;
    private String sParam0 = null;
    private String sParam1 = null;
    private String sParam2 = null;
    private String sParam3 = null;
    private String sParam4 = null;
    private String sParam5 = null;
    private String sParam6 = null;
    private String sParam7 = null;
    private String sParam8 = null;
    private String sParam9 = null;
    private String sParam10 = null;
    private String sParam11 = null;
    private String sParam12 = null;
    private String sParam13 = null;
    private String sParam14 = null;
    private String sParam15 = null;
    private String sParam16 = null;
    private String sParam17 = null;
    private String sParam18 = null;
    private String sParam19 = null;
    private String sParam20 = null;

    public static TestSuiteSetup CSvmap = new TestSuiteSetup();
    public TestDataObj TestDataObject;

    private void setParameterValues(TestDataObj TestDataObject) {

        try {

            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            sTestCaseID = TestDataObject.getTestcaseID();
            sParam0 = TestDataObject.getParam0();
            sParam1 = TestDataObject.getParam1();
            sParam2 = TestDataObject.getParam2();
            sParam3 = TestDataObject.getParam3();
            sParam4 = TestDataObject.getParam4();
            sParam5 = TestDataObject.getParam5();
            sParam6 = TestDataObject.getParam6();
            sParam7 = TestDataObject.getParam7();
            sParam8 = TestDataObject.getParam8();
            sParam9 = TestDataObject.getParam9();
            sParam10 = TestDataObject.getParam10();
            sParam11 = TestDataObject.getParam11();
            sParam12 = TestDataObject.getParam12();
            sParam13 = TestDataObject.getParam13();
            sParam14 = TestDataObject.getParam14();
            sParam15 = TestDataObject.getParam15();
            sParam16 = TestDataObject.getParam16();
            sParam17 = TestDataObject.getParam17();
            sParam18 = TestDataObject.getParam18();
            sParam19 = TestDataObject.getParam19();
            sParam20 = TestDataObject.getParam20();

            generalFunctions = PageFactory.initElements(driver, GeneralFunctions.class);
            employeeCreationPage = PageFactory.initElements(driver, EmployeeCreationPage.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @BeforeMethod(alwaysRun = true)
    public void setup() {
        try {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            sTimeAppend = sdf.format(cal.getTime()).toString();
            try {
                String line;
                Process p = Runtime.getRuntime().exec("tasklist.exe");
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                input.close();
            } catch (Exception err) {
                err.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Employee Creation with all the mandatory fields
    @Test(groups = { "EmployeeCreation-Test" })
    public void EmployeeCreation_EC001() {
        boolean result = false;
        boolean flag = false;

        TestDataObject = (TestDataObj) CSvmap.mMap.get("EC001");
        setParameterValues(TestDataObject);
        sParam4 = sParam4 + sTimeAppend;
        sParam5 = sParam5 + sTimeAppend;
        try {
            System.out.println("Test Case EC001 started");
            flag = generalFunctions.LoginFunction(sParam0, sParam1, sParam2);

            if (flag = true) {
                result = employeeCreationPage.EmployeeCreationWithMandatoryFields(sParam3, sParam4, sParam5,
                        sParam6, sParam7, sParam8, sParam9, sParam10, sParam11, sParam12, sParam13);
                assertEquals(true, result);
            } else
                System.out.println("EC001 test case failed");
            generalFunctions.CloseBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Employee creation with all the mandatory and optional fields
    @Test(groups = { "EmployeeCreation-Test" })
    public void EmployeeCreation_EC002() {
        boolean result = false;
        boolean flag = false;

        TestDataObject = (TestDataObj) CSvmap.mMap.get("EC002");
        setParameterValues(TestDataObject);
        sParam5 = sParam5 + sTimeAppend;
        sParam6 = sParam6 + sTimeAppend;
        sParam7 = sParam7 + sTimeAppend;
        try {
            System.out.println("Test Case EC002 started");
            flag = generalFunctions.LoginFunction(sParam0, sParam1, sParam2);

            if (flag = true) {
                result = employeeCreationPage.EmployeeCreationWithAllFields(sParam3, sParam4, sParam5, sParam6,
                        sParam7, sParam8, sParam9, sParam10, sParam11, sParam12, sParam13, sParam14, sParam15,
                        sParam16, sParam17, sParam18, sParam19, sParam20);
                assertEquals(true, result);
            } else
                System.out.println("EC002 test case failed");
            generalFunctions.CloseBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Employee deletion
    @Test(groups = { "EmployeeCreation-Test" })
    public void EmployeeCreation_EC003() {
        boolean result = false;
        boolean flag = false;

        TestDataObject = (TestDataObj) CSvmap.mMap.get("EC003");
        setParameterValues(TestDataObject);
        sParam4 = sParam4 + sTimeAppend;
        sParam5 = sParam5 + sTimeAppend;

        try {
            System.out.println("Test Case EC003 started");
            flag = generalFunctions.LoginFunction(sParam0, sParam1, sParam2);
            if (flag = true) {
                result = employeeCreationPage.EmployeeDeletion(sParam3, sParam4, sParam5, sParam6, sParam7, sParam8,
                        sParam9, sParam10, sParam11, sParam12, sParam13);
                assertEquals(true, result);
            } else
                System.out.println("Employee deletion failed. EC003 failed");
            generalFunctions.CloseBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Security test case to check if we can inject scripts while employee
    // creation
    @Test(groups = { "EmployeeCreation-Test" })
    public void EmployeeCreation_EC004() {
        boolean result = false;
        boolean flag = false;

        TestDataObject = (TestDataObj) CSvmap.mMap.get("EC004");
        setParameterValues(TestDataObject);

        try {
            System.out.println("Test Case EC004 started");
            flag = generalFunctions.LoginFunction(sParam0, sParam1, sParam2);
            if (flag = true) {
                result = employeeCreationPage.EmployeeCreationWithMandatoryFields(sParam3, sParam4, sParam5,
                        sParam6, sParam7, sParam8, sParam9, sParam10, sParam11, sParam12, sParam13);
                assertEquals(true, result);
            } else
                System.out.println("Script injected! EC004 failed");
            generalFunctions.CloseBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Attempt to create employee without filling any fields
    @Test(groups = { "EmployeeCreation-Test" })
    public void EmployeeCreation_EC005() {
        boolean result = false;
        boolean flag = false;

        TestDataObject = (TestDataObj) CSvmap.mMap.get("EC005");
        setParameterValues(TestDataObject);
        try {
            System.out.println("Test Case EC005 started");
            flag = generalFunctions.LoginFunction(sParam0, sParam1, sParam2);
            if (flag = true) {
                result = employeeCreationPage.EmployeeCreationWithoutFillingAnyFields(sParam3);
                assertEquals(true, result);
            } else
                System.out.println("EC005 test case failed");
            generalFunctions.CloseBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}