You are on page 1of 5

DriverSetup.

java
/* DO NOT CHANGE THIS CLASS. THIS CLASS IS FOR REFERENCE ONLY */

import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverSetup {

public static WebDriver driver;

public static WebDriver getDriver() {


System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver");
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxProfile profile=new FirefoxProfile();
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
firefoxOptions.setProfile(profile);
driver = new FirefoxDriver(firefoxOptions);
String baseUrl = "https://webapps.tekstac.com/CustomerRegistration_4284/";
driver.get(baseUrl);
return driver;
}
}

ExcelUtils.java
import java.io.*;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

public class ExcelUtils { //Do not change the class name

//Use the below declared variables


private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static FileInputStream excelFile;
private static String filePath;

public static String[][] readExcelData(String fileName, String sheetName) throws Exception { //Do not change th
e method signature

//Get the excel file path (note: the excel file is located under 'CustomerRegistration/src' directory)
//Using the fileName and sheetName passed to this method

//Example array declaration :


String[][] customer_data=new String[1][5];
excelFile= new FileInputStream(fileName);
ExcelWBook = new XSSFWorkbook(excelFile);
ExcelWSheet = ExcelWBook.getSheet(sheetName);
XSSFRow row = ExcelWSheet.getRow(0);
try{
for(int i=0; i<5; i++){
customer_data[0][i]=String.valueOf(row.getCell(i));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
return customer_data;
//read the excel data and store it in a string array
//return the array

}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" >
<test name="Test">
<classes>
<!-- Add the class name to be tested -->
<class name="TestValidCustomerRegistration"/>
</classes>
</test>
</suite>

TestValidCustomerRegistration.java
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import static org.testng.Assert.assertEquals;
import org.testng.TestNG;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class TestValidCustomerRegistration { //Do not change the class name


//Use the below declared variables
public static WebDriver driver;
CustomerDetails setformValues;

public static String custnametxt;


public static String custmobiletxt;

//Apply the required annotation


@BeforeClass
public WebDriver setUp() { // Do not change the method signature

//create the driver using method 'getDriver' in class DriverSetup.


//Assign it to the variable 'driver'
driver = DriverSetup.getDriver();

//DO NO CHANGE THE BELOW 2 OBJECT CREATIONS


setformValues= new CustomerDetails(driver);

//return driver
return driver;
}

//Apply the required annotation


@DataProvider (name="getValidData")
public static String[][] getValidCustomerData() throws Exception {

//Call the method 'readExcelData' in class 'ExcelUtils' using file name 'CustomerRegistration.xlsx' and sheet n
ame 'CustomerDetails'
//Return the value
return ExcelUtils.readExcelData("CustomerRegistration.xlsx","CustomerDetails");
}

//Apply the required annotation


@Test(dataProvider="getValidData")
public void testValidCustomerRegistration(String custname, String custmobile, String custdob, String custgender
, String custaddr) throws Exception{

//Using 'setFormValues' object set the form values passed as the parameter using DataProvider method 'getValid
CustomerData'

setformValues.setCustomerName(custname);
//Use 'setFormValues' object and call the method setCustomerName() and pass the 'custname' as parameter.

setformValues.setCustomerMobile(custmobile);
//Use 'setFormValues' object and call the method setCustomerMobile() and pass the 'custmobile' as parameter.

setformValues.setCustomerDOB(custdob);
//Use 'setFormValues' object and call the method setCustomerDOB() and pass the'custdob' as parameter.

setformValues.setCustomerGender(custgender);
//Use 'setFormValues' object and call the method setCustomerGender() and pass the 'custgender' as parameter.

setformValues.setCustomerAddress(custaddr);
//Use 'setFormValues' object and call the method setCustomerAddress() and pass the 'custaddr' as parameter.

setformValues.SubmitForm();
//Use 'setFormValues' object and call the method SubmitForm() to click the 'Register' button.

//After clicking the button, it will display the output

custnametxt = driver.findElement(By.xpath("//table/tbody/tr[1]/td[2]")).getText();
custmobiletxt = driver.findElement(By.xpath("//table/tbody/tr[2]/td[2]")).getText();
//Locate the submitted customer name displayed after form submit, retrieve and store customer name in a static v
ariable 'custnametxt'.
//Locate the submitted customer mobile displayed after form submit, retrieve and store customer mobile in a stati
c variable 'custmobiletxt'.

//Apply the required annotation


@AfterClass
public void closeBrowser() {

//close the driver


driver.close();

CustomerDetails.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Listeners;

@Listeners
public class CustomerDetails { //Do not change the class name

//Use the below declared variable


public WebDriver driver;

//Constructors are already given. If required, you can add more code into it but do NOT remove the existing cod
e.
public CustomerDetails(){}

public CustomerDetails(WebDriver driver) {


this.driver= driver;
}

public void setCustomerName(String name) {

//locate and fill the input box of the 'Name' with 'name'
driver.findElement(By.id("name")).sendKeys(name);
}
public void setCustomerMobile(String mobile) {

//locate and fill the input box of the 'Mobile' with 'mobile'
driver.findElement(By.id("mobile")).sendKeys(mobile);
}

By dobElement = By.id("dob");

public void setCustomerDOB(String dob) {


//locate and fill the input box of the 'DOB' with 'dob'

driver.findElement(dobElement).sendKeys(dob);
}

public void setCustomerGender(String gender) {

//locate and click the radio button 'Gender' based on 'gender' value
if(gender.equalsIgnoreCase("Male")){
driver.findElement(By.id("male")).click();
}
else if(gender.equalsIgnoreCase("Female")){
driver.findElement(By.id("female")).click();
}

public void setCustomerAddress(String addr) {

//locate and fill the input box of the 'Address' with 'addr'
driver.findElement(By.id("address")).sendKeys(addr);
}

public void SubmitForm() {

//locate the Register button and click


driver.findElement(By.id("register")).click();
}

You might also like