Bit Motif: Selenium Remote Control For Java - A Tutorial (Part 2)
Bit Motif: Selenium Remote Control For Java - A Tutorial (Part 2)
Home
About
Test Page For Selenium Remote Control
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.)
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.
assertEquals(TEST_PAGE_TITLE, selenium.getTitle());
}
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.
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.
assertEquals("", selenium.getText("id=textInput"));
}
Checkboxes
Let’s click a checkbox:
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.
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).
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:
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.
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.
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"));
}
String selectedLabel =
selenium.getSelectedLabel("id=selectWithLabelsAndValues");
assertEquals("option one", selectedLabel);
assertEquals("1", selenium.getValue("id=selectWithLabelsAndValues"));
assertEquals("1",
selenium.getSelectedValue("id=selectWithLabelsAndValues"));
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.