Here is a sample Selenium RC test script written in Java that uses MS Excel data sheet to read the test data and performs a google search and writes test results into a .txt file. It also generates JUnit reports.
package com.example.tests;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
import com.unitedinternet.portal.selenium.utils.logging.*;///required for logging
import java.util.regex.Pattern;
public class mamboReturns extends SeleneseTestCase{
public SeleniumServer seleniumserver;
protected LoggingDefaultSelenium selenium;
private BufferedWriter loggingWriter;
private static final String RESULT_FILE_ENCODING = "UTF-8";
//private static final String DEFAULT_TIMEOUT = "30000";
private static final String OPENQA_URL = "http://www.google.co.in";
private static final String SCREENSHOT_PATH = "screenshots";
private final String RESULTS_BASE_PATH = "target" + File.separator + "loggingResults1";
private String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
private String screenshotsResultsPath = new File(RESULTS_BASE_PATH + File.separator + SCREENSHOT_PATH).getAbsolutePath();
@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
//selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
final String resultHtmlFileName = resultsPath + File.separator + "sampleResultSuccess.html";
System.err.println("resultHtmlFileName=" + resultHtmlFileName);
loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, mamboReturns.RESULT_FILE_ENCODING, true);
LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,mamboReturns.RESULT_FILE_ENCODING);
htmlFormatter.setScreenShotBaseUri(mamboReturns.SCREENSHOT_PATH + "/"); // has to be "/" as this is a URI
htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*firefox",OPENQA_URL), htmlFormatter);
myProcessor.setExcludedCommands(new String[] {});
selenium = new LoggingDefaultSelenium(myProcessor);
seleniumserver.start();
selenium.start();
}
//THIS FUNCTION IS TO WRITE RESULTS INTO A FILE
public void result(String results){
try {
FileWriter fstream = new FileWriter("C:\\TestResults.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(results+"\r\n");
//Close the output stream
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//THIS FUNCTION VALIDATES ANY TEXT AND WRITES THE RESULTS INTO TESTRESULTS.TXT
private int stringvalidate (String string1, String string2, int ctotal, int cpass) {
//
if (string1.equals(string2)) {
cpass ++;
result ("TEST CASE NO. "+ctotal+" PASSED "+" ; TOTAL PASSED = "+cpass);
}
else {
result ("TEST CASE NO. "+ctotal+" FAILED "+" ; TOTAL PASSED = "+cpass);
}
return cpass;
}
@Test
public void testDatadrivenJUnit() throws Exception
{
int i = 0;
FileInputStream fi=new FileInputStream("C:\\Selenium\\Workspace\\dataDrivenTestProject\\test\\Resources\\Data\\search.xls");\\THIS IS THE XLS THAT CONTAINS TEST DATA
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.co.in/");
selenium.windowMaximize();
//for (int i = 1; i < s.getRows(); i++)
{ //Read data from excel sheet
selenium.type("name=q",s.getCell(0,i).getContents());
i++;
selenium.click("btnG");
Thread.sleep(3000); }
verifyEquals(s.getCell(0,i).getContents(), selenium.getValue("q"));
i++;
verifyEquals(s.getCell(0,i).getContents(), selenium.getText("link=Selenium web application testing system"));
i++;
selenium.click("link=Selenium web application testing system");
selenium.waitForPageToLoad("30000");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("//img[@alt='video icon']")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
verifyEquals(s.getCell(0,i).getContents(), selenium.getText("//div[@id='mainContent']/h2[1]"));
}
@AfterClass
public void tearDown() throws InterruptedException
{
selenium.stop();
seleniumserver.stop();
try
{
if (null != loggingWriter)
{
loggingWriter.close();
}
}
catch (IOException e)
{
// do nothing
}
}
public static void main(String args[]) throws Exception
{
mamboReturns testTestadmin = new mamboReturns();
testTestadmin.setUp();
testTestadmin.testDatadrivenJUnit();
testTestadmin.tearDown();
}
}