In order to test a system, we need to be able to interact with it and make assertions about it. When the system we wish to test is a web application UI, we need to be able to interact with the page/browser and making assertions about the same. Let’s look at how we can this with Selenium Remote Control and JUnit.
The most common interactions are accomplished through the following methods in DefaultSelenium:
- open(String url)
- click(String locator)
- type(String locator, String value)
- select(String locator, String optionLocator)
- check(String locator)
- waitForPageToLoad(String timeoutInMilliseconds)
Now that we have a way of interacting, we need to be able to get information about a page. DefaultSelenium has many methods for getting information about a page. The ones that I use the most are:
- getTitle()
- getText(String locator)
- getValue(String locator)
- isEditable(String locator)
- isElementPresent(String locator)
- getSelectedLabel(String locator)
- getSelectedValue(String locator)
- isSomethingSelected(String locator)
- isChecked(String locator)
- getAlert()
Rather than trying to describe all of the methods above (that’s what the JavaDoc is for), lets look at some code.
Getting Started
First, let’s create the test class with the set up, tear down, and constants we’ll need. (Note that the example code refers to a real page.)
public class TestPageForSeleniumRemoteControlTest
extends TestCase
{
private static final String MAX_WAIT_TIME_IN_MS = "60000";
private static final String BASE_URL = "http://www.bitmotif.com";
private static final String TEST_PAGE_URL =
BASE_URL + "/test-page-for-selenium-remote-control";
private static final String TEST_PAGE_TITLE =
"Bit Motif » Test Page For Selenium Remote Control";
private Selenium selenium = new DefaultSelenium( "localhost",
4444,
"*firefox",
BASE_URL);
private SeleniumServer seleniumServer;
public void setUp()
throws Exception
{
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}
public void tearDown()
throws Exception
{
selenium.stop();
seleniumServer.stop();
}
}
Checking the Title
Now, let’s open a page and check the title.
public void test_NavigatingPages_WithoutClickingLink()
throws Exception
{
selenium.open(BASE_URL);
selenium.open(TEST_PAGE_URL);
assertEquals(TEST_PAGE_TITLE, selenium.getTitle());
}
Navigating
Next, let’s test clicking a link that takes us to a new page. We need to use waitForPageToLoad. If we don’t, funny things can happen. For example, we could ask for information before the page is loaded. It wouldn’t be there and the test would fail.
public void test_NavigatingPages_ClickingLink()
throws Exception
{
selenium.open(BASE_URL);
selenium.click("link=Test Page For Selenium Remote Control");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertEquals(TEST_PAGE_TITLE, selenium.getTitle());
}
Verifying an Element Exists
If we want to check that an element exists, we can do the following:
public void testIsElementPresent()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertTrue(selenium.isElementPresent("id=textInput"));
}
Text Inputs
Let’s type something in a text box. Notice that we use the getValue to check what is in the text input.
public void testEnterValuesIntoTextField_CheckWithGetValue()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("", selenium.getValue("id=textInput"));
selenium.type("id=textInput", "Text In The Field");
assertEquals("Text In The Field", selenium.getValue("id=textInput"));
}
What happens when we use getText to check what we typed? In this case, the text we are interested in is actually the value of the input. Since the input element does does not have any text, the getText method returns an empty string.
public void testEnterValuesIntoTextField_CheckWithGetText()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("", selenium.getText("id=textInput"));
selenium.type("id=textInput", "Text In The Field");
assertEquals("", selenium.getText("id=textInput"));
}
Checkboxes
Let’s click a checkbox:
public void testClickingACheckBox_UseCheckAndIsCheckedMethods()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertFalse(selenium.isChecked("id=checkBoxInput"));
selenium.check("id=checkBoxInput");
assertTrue(selenium.isChecked("id=checkBoxInput"));
}
Now, let’s look at clicking a checkbox and checking its value a different way — let’s use click and getValue.
public void testCheckingACheckBox_UseClickAndGetValueMethods()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("off", selenium.getValue("id=checkBoxInput"));
selenium.click("id=checkBoxInput");
assertEquals("on", selenium.getValue("id=checkBoxInput"));
}
Radio Buttons
When working with radio buttons, the form of the locator is a little different. In the locator, we give both the name (input name) and value (value of the radio button).
public void testClickingARadioButton_UseCheckAndIsCheckedMethods()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertFalse(selenium.isChecked("name=radioButton value=a"));
assertFalse(selenium.isChecked("name=radioButton value=b"));
selenium.check("name=radioButton value=b");
assertTrue(selenium.isChecked("name=radioButton value=b"));
assertFalse(selenium.isChecked("name=radioButton value=a"));
}
We could also use click and getValue when working with radio buttons. But, it gets a little hard to work with. We can’t just ask for the value of the input. And, the individual buttons have a value of “off” or “on”. Consider:
public void testClickingARadioButton_UseClickAndGetValueMethods()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("off", selenium.getValue("name=radioButton"));
assertEquals("off", selenium.getValue("name=radioButton value=a"));
assertEquals("off", selenium.getValue("name=radioButton value=b"));
selenium.click("name=radioButton value=b");
assertEquals("off", selenium.getValue("name=radioButton"));
assertEquals("off", selenium.getValue("name=radioButton value=a"));
assertEquals("on", selenium.getValue("name=radioButton value=b"));
}
Selects
Selects can be a little more complex than your average input. First, there is the select itself we must identify. Then, there are the options in the select. The options in a select may be identified with ids and values, just the visible text in the option, or some combination thereof. So, we have to use a locator for the select itself and a locator for for the option(s) we are interested in. The locator of the option(s) can be the option element’s id, value, label, or index in the select.
Once something is selected, we can use getValue, getSelectedValue, and getSelectedLabel.
public void testSelectFromDropDown_NoValuesInSelect_UseLabelOptionLocator()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("option one", selenium.getSelectedLabel("id=selectWithLabelsOnly"));
assertEquals("option one", selenium.getValue("id=selectWithLabelsOnly"));
selenium.select("id=selectWithLabelsOnly", "label=option two");
assertTrue(selenium.isSomethingSelected("id=selectWithLabelsOnly"));
assertEquals("option two", selenium.getSelectedLabel("id=selectWithLabelsOnly"));
assertEquals("option two", selenium.getValue("id=selectWithLabelsOnly"));
}
Lets look at the same test but using the index verison of the select locator.
public void testSelectFromDropDown_NoValuesInSelect_UseIndexOptionLocator()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertEquals("option one", selenium.getSelectedLabel("id=selectWithLabelsOnly"));
assertEquals("option one", selenium.getValue("id=selectWithLabelsOnly"));
selenium.select("id=selectWithLabelsOnly", "index=1");
assertTrue(selenium.isSomethingSelected("id=selectWithLabelsOnly"));
assertEquals("option two", selenium.getSelectedLabel("id=selectWithLabelsOnly"));
assertEquals("option two", selenium.getValue("id=selectWithLabelsOnly"));
}
Notice what we get if we look at a select with values.
public void testSelectFromDropDown_LabelsAndValuesInSelect_UseLabelLocator()
throws Exception
{
selenium.open(TEST_PAGE_URL);
assertTrue(selenium.isSomethingSelected("id=selectWithLabelsAndValues"));
String selectedLabel = selenium.getSelectedLabel("id=selectWithLabelsAndValues");
assertEquals("option one", selectedLabel);
assertEquals("1", selenium.getValue("id=selectWithLabelsAndValues"));
assertEquals("1", selenium.getSelectedValue("id=selectWithLabelsAndValues"));
selenium.select("id=selectWithLabelsAndValues", "label=option two");
assertTrue(selenium.isSomethingSelected("id=selectWithLabelsAndValues"));
selectedLabel = selenium.getSelectedLabel("id=selectWithLabelsAndValues");
assertEquals("option two", selectedLabel);
assertEquals("2", selenium.getValue("id=selectWithLabelsAndValues"));
assertEquals("2", selenium.getSelectedValue("id=selectWithLabelsAndValues"));
}
Alert Boxes
Often we need to work with alert boxes. The getAlert method is the man. Invoking this method is the same as clicking “OK” on the alert box. It also returns the text in the alert box.
public void testAlertBox()
throws Exception
{
selenium.open(TEST_PAGE_URL);
selenium.click("id=popUpDiv");
assertEquals("You clicked the div.", selenium.getAlert());
}
Conclusion
With Selenium and JUnit, we can easily test web UIs. The examples here are basic, but they demonstrate how just a few basic Selenium commands can get you most of what you need to test the UI.
[...] If we place this in the test class we created in a previous tutorial and run it, we should see a green bar. Pretty cool, eh? [...]
Hi,
I am trying to open a web browser using selenium. i had written a small script but it gives he following exceptions.
Exception in thread “main” com.thoughtworks.selenium.SeleniumException:
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandP
rocessor.java:73)
at com.thoughtworks.selenium.DefaultSelenium.open(DefaultSelenium.java:2
22)
at Auto.loginsuccess(Auto.java:20)
at Auto.main(Auto.java:40)
The program is :
import com.thoughtworks.selenium.*;
import java.io.*;
import junit.framework.*;
import java.util.regex.Pattern;
import java.util.ArrayList;
public class Auto extends TestCase {
private Selenium browser;
public void setUp(){
browser=new DefaultSelenium(”localhost”,4444,”*firefox”,”http://localhost:8080″);
browser.start();
public void loginsuccess(){
Result.add(”CHECKING LOGINSUCCESS:TEST CASE 1″);
browser.open(”mail.do”);
browser.type(”user”, “admin”);
browser.type(”passwd”, “admin123″);
browser.click(”//input[@value=’Log On’]”);
browser.waitForPageToLoad(”500000″);
if(browser.isTextPresent(”Logged on as:
admin (Edit Profile)”))
Result.add(”Login Successful.”);
else
Result.add(”Login Unsuccessful.”);
}
public static void main(String[] args) throws
InterruptedException{
Auto test=new Auto();
test.setUp();
test.loginsuccess();
}
i am stuck up and i am not able to identify the error.
please reply on my mail
Thanks in advance.
Hi all,
I have now a trouble with config and start selenium-java-client-driver
Please help me the step by step in details to deal it.
Thanks
If we have 2 methods and second method “depends on” first method.can we pass arguments from method 1 to method 2?
Hi
Can you give an example to test and XML file? I have a page wich opens up an xml file in the bowser. Now i need to test that page to see if the xml file has opened correctly or not. please help
Hi
Take a look on Simplium which is a framework based on JUnit 4.5 and Selenium Remote Control. It aids the writing and executing of test cases.
http://simplium.sektor.se
/Markus
Hi,
I’m facing a problem with deleting an entry from a list of checkboxes.
I checked the source for that particular entry of mine on Firefox and found this:
NewDomain
My checkbox is listed dynamically among other checkboxes, and it’s order can be found out in the value of name attribute as %3Brow%3D1%. So this says that it’s in 1st row. But if some other entry/checkbox occupies the first row, my test case fails.
I only know of the name that is inserted in between span tags, i.e. “New Domain”.
How do I make my test case intelligent enough to find the span name, i.e. “New Domain” and check it’s related checkbox.
:::viagra::hgfvcdviagra:;cialis::levitra::abilify ::hgbt viagra:::vfgtbh:buy generic viagra :::
Hi all,
can anyone help me sorting out this small piece of code,
the Selenium.click(“link=User”);
is not working
import com.thoughtworks.selenium.*;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import java.util.regex.Pattern;
public class testNG01 extends SeleneseTestNgHelper
{
@Test public void testtestNG01() throws Exception
{
selenium = new DefaultSelenium(“localhost”,
4444, “*iexplore”, “http://192.168.5.211/login.php”);
selenium.start();
selenium.open(“//login.php”);
selenium.type(“uname”, “admin”);
selenium.type(“pword”, “password”);
selenium.click(“//input[@value='Submit']“);
selenium.waitForPageToLoad(“30000″);
selenium.click(“link=User”);
}
}
Hi,
The tutorials of selenium java RC is very helpful. Thanks a lot!