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

Tuesday, October 26, 2010

Selenium IDE : Different examples on input

Using the excellent FireFox add-on Firebug and the ubiquitous Google search page, we get this HTML for the "Google Search" button.





There are many ways to code the IDE for this button e.g.


click btnG

click //input[@value='Google Search']

click //input[@type='submit']

click //html/body/span/center/span/center/form/table/tbody/tr/td[2]/span/span/input



The latter ones are examples of XPath and XPath knowledge is VERY useful when coding the IDE scripts.

To get the XPath construct inside Firebug, right click on the HTML segment and then select "Copy XPath".

To check your script without having to run it, load the Click command that you want to test and then select the "Find" button to the right of "Target". The "Google Search" button will be highlighted with a green border if the script works otherwise an error message will be displayed.





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 IDE : Input too "early"

I've seen this behavior a number of times.

You run a Selenium IDE script where you have a

waitForTitle
type - token - text

scenario and Selenium will complain that the field "token" does not exist.

But it does - at this point, you are staring at it in the browser.

What sometimes seems to happen is that the page loads, Selenium sees the title and moves to the next line of the script but the actual fields haven't loaded yet and so can't be found.

The way around this is to look for a bit of text near the input field and then do a

waitForTextPresent

That way the input field is there when the script requires it.

Enjoy!

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!