Running Selenium RC script
Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC is great for testing complex AJAX-based web user interfaces under a Continuous Integration system. It is also an ideal solution for users of Selenium IDE who want to write tests in a more expressive programming language like Java,Perl etc.
Selenium IDE is much simpler and can be used only on firefox. Selenium RC lets you to write much complex scripts using the programming language features. And you can mention the browser on which you want to run the script. Selenium RC supports Internet Explorer, Firefox, googlechrome etc.
We can use any IDE for developing your scripts. Lets start creating the Selenium RC script using TestNG. You will need to install the following first.
We need to install the following first:
- JDK : You can be download it from here http://java.sun.com/javase/downloads/index.jsp Set the classpath.
- Selenium RC: Download it from here: http://seleniumhq.org/download Extract the files in a folder named SeleniumRC.
- TestNG : Download TestNG from http://testng.org/doc/download.html Unzip. The files will get extracted to a folder called testng.
- Eclipse : Download eclipse from http://www.eclipse.org/downloads/.Choose the "Eclipse IDE for Java Developers".Unzip the downloaded file. The files will get extracted to a folder called 'eclipse'.
- TestNG plug-in for Eclipse: Start Eclipse by clicking the eclipse icon in the eclipse folder that's mentioned earlier. Click on Help->Install New Softwares. Enter "http://beust.com/eclipse/" in the "Work With:" and press Enter. You should see TestNG. Select it and then press Next till you reach Finish. Restart Eclipse.
Let's start with the SeleniumRC and TestNG script now:
- Open eclipse. Start by creating a new project : File->New->Java Project. Give a name say 'SeleniumLowcostloadtest'.
- Add the required jars. Select the project-' SeleniumLowcostloadtest ' , right click Build Path->Configure Build Path. Click Libraries->Add External Jars. You will have to add the testng and selenium jars.
- Create a new package in the project. Select the project -'SeleniumLowcostloadtest' and then right click New->Package. Give a name say 'First'.
- Create a new java program. Select the package-'First' and then right click New->Class. Enter the name as say 'Lowcostloadtestclass'.Press Enter.
- Now go to Selenium IDE and open the ‘www.lowcostloadtest.com' script which we had created in our first blog- 'Starting with Selenium IDE'. Click on Options->Format->Java(TestNG)Selenium. You can see that the format of your test case is now changed.
The format is in the testng format now. Your script would look like below:
- Copy paste the whole code into eclipse(Lowcostloadtestclass). You can see some errors.
- We can run the server in two ways:
Click on Run to start the server.
Otherwise we can Run the server by cmd.
ii) Run the Server by cmd
Start your selenium server. Go to command prompt and then go to SeleniumRC/JavaServer folder (where you installed selenium rc). Type in
java -jar selenium-server.jar -port 4444This is assuming that the port 1234 is free. You can give any other port number if 4444 is being used. Just ensure that the port num given in the testng script matches with the port num used to bring up the selenium server.
Your command prompt screen should look something like below:
java -jar selenium-server.jar -port 4444This is assuming that the port 1234 is free. You can give any other port number if 4444 is being used. Just ensure that the port num given in the testng script matches with the port num used to bring up the selenium server.
Your command prompt screen should look something like below:
- Now run the testng script by going back to eclipse. Right click on the testng script. Click RunAs->TestNG Test.
Your script should run successfully.
Parameterization:
For parameterization, just edit the code like below:
import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.io.File;
import java.util.regex.Pattern;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ParaLowcostloadtestclass extends SeleneseTestBase {
@BeforeClass
//@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
public void startSelenium() //String seleniumHost, String seleniumPort, String browser, String webSite) throws Exception
{
selenium= new DefaultSelenium("localhost",4444,"*googlechrome","http://www.lowcostloadtest.com/");
selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");
selenium.open("/");
//Thread.sleep(5000);
selenium.windowMaximize();
}
@DataProvider(name = "DP1")
public static Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("Data.xls","Submit", "Start");
return(retObjArr);
}
@Test(dataProvider = "DP1")
public void testLowcostloadtestclass(String Login, String Password) throws Exception {
selenium.open("http://www.lowcostloadtest.com/");
assertEquals(selenium.getTitle(), "Low Cost Load Test");
selenium.click("link=Login");
selenium.waitForPageToLoad("300000");
assertEquals(selenium.getTitle(), "Login Form");
selenium.type("id=login",Login);
selenium.type("id=password", Password);
selenium.click("name=Submit");
selenium.waitForPageToLoad("300000");
assertEquals(selenium.getTitle(), "Member Index");
selenium.click("link=Logout");
selenium.waitForPageToLoad("300000");
assertEquals(selenium.getTitle(), "Logged Out");
}
public static String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception
{
String[][] tabArray=null;
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
Cell tableStart=sheet.findCell(tableName);
startRow=tableStart.getRow();
startCol=tableStart.getColumn();
Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false);
endRow=tableEnd.getRow();
endCol=tableEnd.getColumn();
tabArray=new String[endRow-startRow-1][endCol-startCol-1];
ci=0;
for (int i=startRow+1;i<endRow;i++,ci++){
cj=0;
for (int j=startCol+1;j<endCol;j++,cj++){
tabArray[ci][cj]=sheet.getCell(j,i).getContents();
}
}
return(tabArray);
}
}
Attach the .xls document to the project which is shown below:
Hemalatha.P















No comments:
Post a Comment