Till now only Selenium IDE was in picture. But now you will need all the things, Selenium RC, Java, Junit. So let's go step by step.
- Recored the testcase using selenium IDE
- Export the test case
- Edit the test case
- Set Classpath
- Run the test case
- Troubleshooting
Export the test case to JAVA
Export the test cases in java language and save at your desired location.
Edit the test case.
Now as you have saved the test cases in JAVA, to make it run you need to the following changes.
- Remove the first line starting with package or comment it with double slash ( // ).
- Make sure the following lines are at top of the file.
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*; - Make sure the class name is same as your file name
- You need to add three new extra methods in the code as follows
Below is the code you will get when you will export the test case from selenium
public static Test suite() {
return new TestSuite(GoogleSearch.class);
}
public void tearDown(){
selenium.stop();
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}package com.example.tests;
And the following is the modified code ready to run with Junit
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com/", "*chrome");
}
public void testNew() throws Exception {
selenium.open("/");
selenium.type("q", "selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("selenium-ide.seleniumhq.org"));
selenium.click("//ol[@id='rso']/li[1]/h3/a/em");
selenium.waitForPageToLoad("30000");
}
}//package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*; //added
public class GoogleSearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://Google.com", "*chrome");
}
public void testNew() throws Exception {
selenium.open("/");
selenium.type("q", "selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("selenium-ide.seleniumhq.org"));
//selenium.click("//ol[@id='rso']/li[1]/h3/a/em");
selenium.click("link=Selenium IDE");
selenium.waitForPageToLoad("30000");
}
public static Test suite() {
//method added
return new TestSuite(GoogleSearch.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}
public static void main(String args[]) {
// Added. Execution will started from here.
junit.textui.TestRunner.run(suite());
}
}
- Save the file bug before you compile it make sure that classpath for both selenium and Junit is set else you will end up with compile time errors.
Set Classpath
You need to set the classpath for both selenium-java-client-driver.jar and junit-4.6.jar(file name may differ depends on the version you have downloaded).
To set the classpath just do the following
- right click on the My Computer icon on desktop and click on properties then click on Advance Tab and then click on Environment Variables.
- Under the System Variables click on CLASSPATH and add the full path of both the above file separated by semicolon(;).
Run the TestCase
Javac Filename.java command.
If everything goes fine your file will compile successfully without any error.
Now before you run the test cases you need to start the Selenium server. To start the server go to the command prompt and run the following command
java -jar path\selenium-server-0.9.2\selenium-server.jar -interactive
Now to run your test case open new command prompt and type the following command
java filename
Now the test case will be executed in the firefox browser.
You can also execute the same test on different browser too.
To execute it on the Internet Explorer just change the line setUp("http://Google.com", "*chrome"); from setUp() method to setUp("http://Google.com", "*iexplore"); to run it on the opera change the line to
setUp("http://Google.com", "*opera");
Troubleshooting
- Getting the following error while compiling the test case file.
'javac' is not recognized as an internal or external command,operable program or batch file.
Solution: locate the bin folder inside you jdk folder and set it's path in path environment varibale - Getting following error while running the test case
java.lang.RuntimeException: Could not contact Selenium Server; have you started it?
Solution: This means that you haven't started the selenium server. So locate the "selenium-server.jar" file and use the following command to start the server
java -jar path\selenium-server.jar -interactive - getting following error while test case is in execution.
com.thoughtworks.selenium.SeleniumException: Permission denied to get property Location.href
Solution:use *chrome instead of *firefox in setup