Wednesday, February 17, 2010

Using TestNG with Selenium in Eclipse

Class file demonstrating usage of TestNG with Selenium
Best way to follow this is to copy paste the class in a new class in eclipse and look at the errors.
This class uses the excel libraries and functions that are defined in my other posts.
excel
file manipulation
If you need instructions on setting up testng with eclipse, please comment and I will post.


***** Class ******

import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.server.SeleniumServer;
import java.io.*;

public class RegressionTestDriver {

public Selenium selenium;
public SeleniumServer seleniumServer;

//* Spreadsheet with the test scenarios
private String sTestDataFile = "C:\\Workspace\\Testfiles\\Regression\\Regression_Test_Driver.xls";

private String sFirstName;
private String sLastName;
private String sMiddleName;
private String address1;
private String address2;
private String sCity;
private String sState;
private String sZip;
String sToday, sTodayx;
String sTime;
String sFile;
String sBaseLocation;
String sTempLocation;
String sFileLocation;
String sReqfileofInterest;
String sResfileofInterest;
String sTemplateDatafile;
String sActualDataFile;
String sTestResult;
String sTestMessage;
int ifound;


@BeforeTest
public void init() throws Exception {

//Start Selenium Server
seleniumServer = new SeleniumServer();
seleniumServer.start();
TestProps.setPropsFile("C:/Workspace/Testfiles/Staging.Properties");
//Start the browser
selenium = new DefaultSelenium("localhost",
4444, "*chrome", TestProps.getProperty("SELENIUM_URL"));
}

@Test
public void FIDSRegression() throws Exception {

String sDatafile = null;
String [][] asTestData;

//* Read the test scenarios into an array
asTestData = ExcelUtils.ReadExcelData(sTestDataFile, "TestCaseData");
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd");
sToday = sdf.format(date);
sdf = null;
sdf = new SimpleDateFormat("MMddyyyy");
sTodayx = sdf.format(date);
sdf = null;
sdf = new SimpleDateFormat("HH-mm-ss");
sTime = sdf.format(date);

sBaseLocation = "C:\\Workspace\\Testfiles\\" ;
sTempLocation = "C:\\Temp\\Temps\\";

//* Create folder for logging results
FileUtils.CreateFolder(sBaseLocation + "Regression\\" + sToday);
sFileLocation = sBaseLocation + "Regression\\" + sToday + "\\";

//* Create a spreadsheet to log the test case details and the result
String sXLSReport = sFileLocation + "Regression_Test_Results_" + sTime + ".xls";

//* Copy the testcase file and save it as a report file
ExcelUtils.CopyExcelFile(sTestDataFile,sXLSReport);

//* Loop through the test cases

for (int iTestCase = 1; iTestCase <= 5; iTestCase++){ Logs.sTestResult = "PASS"; FileUtils.AppendFile(Logs.sLogFile, System.getProperty("line.separator") + "Executing Regression test Case: " + iTestCase); sDataSource = asTestData[iTestCase][2]; sOptin = asTestData[iTestCase][3]; sDatafile = asTestData[iTestCase][4]; date = null; date = new Date(); sdf = null; sTime = null; sdf = new SimpleDateFormat("HH-mm-ss"); sTime = sdf.format(date); selenium.start(); //* Open the login page and complete the form selenium.open("www.mysitexyz.com/login"); selenium.type("username", sID); selenium.type("password", "P@ssw0rd"); selenium.click("submit"); selenium.waitForPageToLoad("30000"); Thread.sleep(1000); FileUtils.ReplaceContents(sTemplateDatafile, sActualDataFile,iIndex, sSessionId.length(), sSessionId); selenium.stop(); Thread.sleep(1000); ExcelUtils.WriteExcelDataSingle(sEmployeeId,sXLSReport,iTestCase,19,""); Thread.sleep(1000); } } @AfterTest public void stop() throws Exception { selenium.stop(); seleniumServer.stop(); } }

No comments:

Post a Comment