Showing posts with label Selenium IDE. Show all posts
Showing posts with label Selenium IDE. 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!

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!

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!