Showing posts with label Selenium RC. Show all posts
Showing posts with label Selenium RC. Show all posts

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!

Thursday, October 14, 2010

Selenium RC : Using a spreadsheet with Selenium

Quite a lot of the work I've done with Selenium is very repetitive e.g. navigating to an application, logging in, doing some test and logging out.

It made sense to put this in some kind of spreadsheet where each line would have the application URL, login credentials (user field name, user name, password field name, password) and the name of the "Submit" button. Then do some application work perhaps and then logout.

The program could go through the spreadsheet row by row populating the values and then posting the page.

I put all the data into a csv file so I could use opencsv to read the rows.

The code looks something like:


CSVReader reader = new CSVReader(new FileReader("x.csv"));

while ((nextLine = reader.readNext()) != null)
{
populateParams(nextLine);

if (!csvURL.equals("Exit"))
{
selenium = new DefaultSelenium( "localhost",
4444,
"*iexplore",
csvURL);

selenium.start();
selenium.windowMaximize();

selenium.open(csvURL);

selenium.waitForPageToLoad("30000");
selenium.type(csvUserText, csvUser);
selenium.type(csvPasswordText, csvPassword);
selenium.click(csvSubmit);
selenium.waitForPageToLoad("30000");

...
}


The code runs through the spreadsheet until it finds an "Exit" in the URL field.

populateParams looks like:


public void populateParams (String nextLine[])
{
...
csvURL = nextLine[1];
csvUserText = nextLine[2];
csvUser = nextLine[3];
csvPasswordText = nextLine[4];
csvPassword = nextLine[5];
csvSubmit = nextLine[6];
...
}


Enjoy!

Wednesday, October 13, 2010

Selenium RC : Setting up a Firefox proxy

The normal way to use Selenium RC if your environment has a proxy installed is to use the command line e.g.

java -jar selenium-server.jar -Dhttp.proxyHost=xxxproxy.company.nz -Dhttp.proxyPort=8082

I could never quite get this to work.

I use the Firefox Profile Manager.

The command line is:

firefox -P


You need to do this from wherever Firefox is installed on your PC e.g.

x:\Program Files\Mozilla Firefox


When the "User Profile" box pops up, choose "Create Profile" and follow the wizard.

Run the command line again and select the new profile and then select "Start Firefox"

Configure Firefox to use a manual proxy:

Tools / Options / Advanced / Network / Settings

Configure the manual proxy to point to the proxy in your environment.

The Selenium RC command line you need to use this is:

java -jar selenium-server.jar -firefoxProfileTemplate "Path to profile created above"


GOTCHA: Remember to run the Profile manager again and select "default" to return to your normal profile otherwise any new changes will be mode to the wrong profile.

Enjoy!