Some of you may already know Selenium. Basically it’s free and open source tool for web application functional testing. It’s mainly know as replacement for HP’s QuickTestProfessional. I’ve recently discovered Selenium as very helpful for automating data setup for load testing.
Here is a small description howto setup Eclipse and run Selenium script through it.
1) Download Eclipse IDE from http://www.eclipse.org/downloads/
2) Download Selenium RC from http://seleniumhq.org/download/
3) Download Junit from http://www.junit.org/
4) Create new project in Eclipse:
5) Import Selenium and JUnit packages into the project:
Now our Eclipse environment should be ready. Next step is to prepare a test case in Selenium IDE. I’ve prepared small test that searches for “linux” word in google. To export test case as Java, in Selenium IDE go to Options -> Format -> Java (Junit) Selenium RC. As a result you should get something like this:
package com.example.tests; import com.thoughtworks.selenium.*; import java.util.regex.Pattern; public class Untitled 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("/"); selenium.type("q", "linux"); selenium.click("btnG"); } }
Default Junit code generated by Selenium doesn’t use Selenium RC. Because of that I updated the code a little bit to connect to localhost Selenium RC on port 4444. Here is updated version:
package com.example.tests; import com.thoughtworks.selenium.*; import java.util.regex.Pattern; public class Untitled extends SeleneseTestCase { public DefaultSelenium selenium; public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/"); selenium.start(); } public void testUntitled() throws Exception { selenium.open("/"); selenium.type("q", "linux"); selenium.click("btnG"); } }
Now, lets add it in our Eclipse project:
Before we run our test, we need to start Selenium RC. For that open command line and under selenium-remote-control-1.0.1/selenium-server-1.0.1 run command “java -jar selenium-server.jar”. It will run Selenium RC server on default port 4444.
Now, to start the test just select Run -> Run As -> Junit test. It should open two IE browsers. One for Selenium RC and second with Google results for “linux” word.
Because we run Java code, it’s only up to us how we want to run the test. Below is our example update to search for “linux” word in a loop 5 times with 5 seconds intervals.
package com.example.tests; import com.thoughtworks.selenium.*; import java.util.regex.Pattern; public class Untitled extends SeleneseTestCase { public DefaultSelenium selenium; public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/"); selenium.start(); } public void testUntitled() throws Exception { for(int i = 0; i < 5; i++) { selenium.open("/"); selenium.type("q", "linux"); selenium.click("btnG"); selenium.waitForPageToLoad("10000"); Thread.sleep(5000); } } }