Tuesday, October 26, 2010

Selenium RC : Importing a script from Selenium IDE

As a quick start to get up and running with Selenium RC, you can get Selenium IDE to capture the session. Here's the IDE script to search Google for "Selenium RC".



...




















New Test
open /
type q selenium rc
clickAndWait //input[@value='Google Search']

...


Then Options / Format / Java (JUnit)

This produces the following code:


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", "selenium rc");
selenium.click("//input[@value='Google Search']");
selenium.waitForPageToLoad("30000");
}
}



You can download Selenium RC from here.

Unzip the file and then start Selenium RC from a command prompt.

...\selenium-server-1.0.3>java -jar selenium-server.jar


I run JUnit inside Netbeans.

After a bit of massaging, the code above becomes:



import com.thoughtworks.selenium.*;

public class GoogleSearch extends SeleneseTestCase
{

Selenium browser = null;

public void setUp() throws Exception
{

}

public void testGoogleSearch() throws Exception
{

browser = new DefaultSelenium("localhost", 4444,
"*chrome",
"http://www.google.com/");

browser.start();

browser.open("/");
browser.type("q", "selenium rc");
browser.click("//input[@value='Google Search']");
browser.waitForPageToLoad("30000");

verifyEquals("selenium rc - Google Search", browser.getTitle());

//selenium.close();
}
}



This is invoked by a JUnit test:


@Test
public void testGoogleSearch() throws Exception
{
System.out.println("testGoogleSearch");
GoogleSearch instance = new GoogleSearch();
instance.testGoogleSearch();
}


Enjoy!

No comments: