To work on selenium automation framework you need to have java programming knowledge and selenium supports various programming languages such as c#, python, ruby, php and perl here I am explaining using java programming.
If you don’t know about selenium automation framework (i.e How to record a test case in selenium IDE ) please refers selenium manuals.
Before going start you need to download fallowing things Java, Selenium IDE, Selenium RC, and Junit.
- Install java ( I have installed in C:\Program Files\Java\jdk1.6.0_16)
- Install Selenium IDE on Firefox
- You don’t need to install Selenium RC and Junit just unzip the files. ( I have unzipped selenium RC to C:\selenium and Junit C:\junit4.7\junit-4.7.jar)
you can go to 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(;). If it does not work set path manually in command line.
Example if java installed in C:\Program Files\Java\jdk1.6.0_16
set CLASSPATH=C: \Program Files\Java\jdk1.6.0_16\bin
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_16
Then set the class path to selenium RC for java specific driver jar file i.e selenium-java-client-driver.jar which has been downloaded earlier and Junit jar file junit-4.7.jar
Example
set CLASSPATH=.;C:\selenium\selenium-remote-control-1.0.1\selenium-java-client-driver-1.0.1\selenium-java-client-driver.jar;C:\junit4.7\junit-4.7.jar
Now fallow the step by step
- Record the test case in Selenium IDE
- Export the recorded test case( File==>export test case as ==> Java(Junit)-SeleniumRC)

- Edit the test case
package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");
}
public void testUntitled() throws Exception {
selenium.open("/search?hl=en&source=hp&q=selenium&meta=&cts=1270555440695&aq=f&aqi=g10&aql=&oq=&gs_rfai=");
selenium.click("link=Selenium - Wikipedia, the free encyclopedia");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("link=methylselenocysteine"));
}
}
To make it run as you need edit the java file add the fallowing lines.
// package com.example.tests; here comment package
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*;
You need to add three new extra methods in the code as follows.
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());
}
And the following is the modified code ready to run with Junit
// package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*;
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.co.in", "*firefox");
}
public void testNew() throws Exception {
selenium.open("/search?hl=en&source=hp&q=selenium&meta=&cts=1270555440695&aq=f&aqi=g10&aql=&oq=&gs_rfai=");
selenium.click("link=Selenium - Wikipedia, the free encyclopedia");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("link=methylselenocysteine"));
}
public static Test suite() {
//method added
return new TestSuite(NewTest.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());
}
}
And compile as javac NewTest.java
Then run the seleniumRC server
C:\selenium\selenium-remote-control-1.0.1\selenium-server-1.0.1>java -jar selenium-server.jar
Then run as java NewTest
Troubleshooting
1. 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 variable
2. Getting the following error while compiling the test case file.
Exception in thread "main" java.lang.NoClassDefFoundError: NewTest
Caused by: java.lang.ClassNotFoundException: NewTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: NewTest. Program will exit.
Your not set the classpath properly so set the class path
Example set CLASSPATH=.;C:\selenium\selenium-remote-control-1.0.1\selenium-java-client-driver-1.0.1\selenium-java-client-driver.jar;C:\junit4.7\junit-4.7.jar
Or
3. 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
4. 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
No comments:
Post a Comment