com.tricentis.automobileinsurance.utility.AutomobileUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.tricentis.automobileinsurance.utility.AutomobileUtil.java

Source

/**
 * 
 */
package com.tricentis.automobileinsurance.utility;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import com.tricentis.automobileinsurance.pagemodel.EnterVehicleDataPageModel;
import com.tricentis.automobileinsurance.pagemodel.VehicleInsurancePageModel;

/**
 * @author Room no 2
 *
 */
public class AutomobileUtil {

    /**
     * 
     */
    public FileInputStream fileInputStream = null;
    /**
     * 
     */
    public Properties properties = null;
    /**
     * 
     */
    public WebDriver driver;
    /**
     *
     */
    public Actions action;

    /**
     * 
     * 
     */
    public Select select;
    /**
     * 
     */
    public VehicleInsurancePageModel vehicleInsurancePageModel;
    /**
     * 
     */
    public EnterVehicleDataPageModel enterVehicleDataPageModel;

    /**
     * 
     */
    protected void setUp() {

        loadTestData();
        setEnvironment(properties.getProperty("browserType"));
        initializePageModel();
    }

    /**
     * 
     */
    public void initializePageModel() {

        vehicleInsurancePageModel = PageFactory.initElements(driver, VehicleInsurancePageModel.class);
        enterVehicleDataPageModel = PageFactory.initElements(driver, EnterVehicleDataPageModel.class);
    }

    /**
     * 
     */
    public void loadTestData() {

        try {
            fileInputStream = new FileInputStream(new File(AutomobileConstant.AUTOMOBILE_PROPERTYLOC));
            properties = new Properties();
            properties.load(fileInputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @param browserName
     */
    public void setEnvironment(String browserName) {
        if (browserName.equalsIgnoreCase("firefox")) {
            /*Open firefox browser*/
            System.setProperty(AutomobileConstant.FIREFOXDRIVER_KEY, AutomobileConstant.FIREFOXDRIVER_VALUE);
            driver = new FirefoxDriver();
        } else if (browserName.equalsIgnoreCase("chrome")) {
            System.setProperty(AutomobileConstant.CHROMEDRIVER_KEY, AutomobileConstant.CHROMEDRIVER_VALUE);
            driver = new ChromeDriver();
        } else {
            System.setProperty(AutomobileConstant.IEDRIVER_KEY, AutomobileConstant.IEDRIVER_VALUE);
            driver = new InternetExplorerDriver();
        }
        /*implicit wait*/
        driver.manage().timeouts().implicitlyWait(AutomobileConstant.IMPLICIT_WAIT, TimeUnit.SECONDS);
        /*maximize browser window*/
        driver.manage().window().maximize();
        /* hit the application URL*/
        driver.get(AutomobileConstant.APPLICATION_URL);
        action = new Actions(driver);
    }

    public void navigateToInsurantDataPage() {

        vehicleInsurancePageModel.getTypeOfVehicle().get(0).click();
        /**
         * write below code to fill Enter Vehicle Data page
         */
        selectValueFromDropdown(enterVehicleDataPageModel.getDropdownsOnEnterVehicleDataPage().get(0), 5);
        enterVehicleDataPageModel.getEngineperformanceInputFieldOnEnterVehicleDataPage().sendKeys("2500");
        enterVehicleDataPageModel.getDateofmanufactureInputFieldOnEnterVehicleDataPage().sendKeys("05/07/2015");
        selectValueFromDropdown(enterVehicleDataPageModel.getDropdownsOnEnterVehicleDataPage().get(1), 2);
        selectValueFromDropdown(enterVehicleDataPageModel.getDropdownsOnEnterVehicleDataPage().get(2), 1);
        enterVehicleDataPageModel.getListpriceInputFieldOnEnterVehicleDataPage().sendKeys("250");
        enterVehicleDataPageModel.getLicenseplatenumberInputFieldOnEnterVehicleDataPage().sendKeys("MH14EL6990");
        enterVehicleDataPageModel.getAnnualmileageInputFieldOnEnterVehicleDataPage().sendKeys("505");
        enterVehicleDataPageModel.getNextBtnOnEnterVehicleDataPage().click();
    }

    public void selectValueFromDropdown(WebElement element, int index) {
        select = new Select(element);
        select.selectByIndex(index);
    }

    /**
     * 
     */
    public void tearDown() {
        driver.close();
    }
}