Selenium 2.0 WebDriver – Some useful APIs

by seetaram on July 23, 2011

To start with WebDriver we need to learn about some of the useful APIs that are provided for automating user actions on an application.

Let us list some of the actions that we need to automate while automating a test case:

  1. Click a link, button
  2. Type value in an Edit box
  3. Select a value from the dropdown
  4. “Check / Uncheck “ a checkbox
  5. Select a radio button

This is exactly what I am going to discuss in this post.

Click a link / button:

To click on an object through webdriver first we need to find out which locator we are going to use.  Is it ID, name, xpath, or css? For this purpose we can utilize firebug / xpath checker to find out is there any id / name exists for the object we are going to perform action upon.  Then write the code as below:

driver.findElement(By.xpath("//a[@href='CustomerInfo.htm']")).click();

In the above line of code “driver” could be FirefoxDriver, InternetExplorerDriver, ChromeDriver, HtmlUnitDriver, etc.  On one of these browsers we are going to find an element and then click as per the code.

findElement is an API provided by the webdriver which requires argument “By.xpath”.  The “xpath” can be replaced by one of the below methods if we need to identify the element with any other attributes such as css, name, classname, etc:

  1. className
  2. cssSelector
  3. linkText
  4. name
  5. partialLinkText
  6. tagName
  7. xpath
  8. id

To understand the above methods one needs the basic understanding of the HTML.  Id, name, input, type, a, etc are the HTML tags / attributes.  Using these HTML tags and attributes “xpath” can be constructed and this I have already explained in one of my earlier posts (How to write xpath)

Type value in an Editbox

Have a look at the below line of code.

driver.findElement(By.name("FirstName")).sendKeys("Google");

Here the webdriver finds the object first with findElement API and then keys in the value with sendKeys method.

Select a value from the dropdown

To select a value from a dropdown, follow the below steps:

  1. Declare a List and assign all the values of dropdown using findElements method
  2. Use a for loop to go through the elements one by one
  3. Using an IF condition match the required option
  4. Click the required option (.setSelected is deprecated)

Use the below code and put that into a function which does the job.

	public static void selectValue(String valToBeSelected){
        List <WebElement> options = driver.findElements(By.tagName("option"));
		for (WebElement option : options) {
			if (valToBeSelected.equalsIgnoreCase(option.getText())){
				option.click();
			}
		    }
	}

call the static method wherever necessary – selectValue(“Texas”) will select the value Texas from the dropdown country.

“Check / Uncheck “ a checkbox

To “Check / Uncheck” a checkbox, the object needs to be identified using findElement method and then just click.  To find out whether the checkbox is checked or not utilize the method – element.isSelected()

        WebElement kancheck = driver.findElement(By.name("kannada"));
        kancheck.click();
        System.out.println(kancheck.isSelected());

Above code snippet will first click the checkbox named kannada and then verifies whether it is clicked or not.

Select a radio button

Follow the same steps which are used in Checkbox to select a radio button and then verify the status using isSelected() method.

        WebElement gender = driver.findElement(By.xpath("//input[@name='male']"));
        gender.click();
        System.out.println(gender.isSelected());

Above are the basic actions needed for Test Automation.  I will try to explain this with an example in the next post.

{ 12 comments… read them below or add one }

Rahul November 8, 2011 at 2:02 pm

Hi Seetaram,
I want to select country as “India” from drop down list, I have written code as given below.
But I’m getting compilation error while doing so. Can you please let me know the reason.
Also, don’t we have direct api to select value from drop down list in Webdriver?

public void selectValue(String valToBeSelected){
WebElement we = driver.findElement(By.name(“country”));
//error in the below line as “the type list is not generic, it can not be parameterised with args”
List options = we.findElements(By.tagName(“option”));
for (WebElement option : options)
{
if (valToBeSelected.equalsIgnoreCase(option.getText()))
{
option.click();
break;
}
}
}

Thanks in adv
Rahul :)

Reply

seetaram November 9, 2011 at 3:38 am

Dear Rahul,

The reason might be:
1. WebElement is not imported
2. List is not imported

Solution:
import java.util.List;
import org.openqa.selenium.WebElement;

Use the below code:

Select select = new Select(driver.findElement(By.id(“salutation”)));
select.selectByValue(ValToBeSelected);

We can modify your code as below:
Select select = new Select(driver.findElement(By.name(“country”)));
select.selectByValue(“Mr.”);

For the above code to work, you need to add the import statement as below:
import org.openqa.selenium.support.ui.Select;

– Seetaram

Reply

Rahul November 9, 2011 at 10:00 am

Thank you very much Seetaram, I have tried on both the ways given above.. Working fine now..
I have one quick query ::

Suppose this is the source ::

Afghanistan
Albania
India
China

Here,

Select sel = new Select(driver.findElement(By.name(“country”)));
sel.selectByValue(“”);
–> It selected China, As the value given is empty which matches with the value of China

Select sel = new Select(driver.findElement(By.name(“country”)));
sel.selectByIndex(3);
–> It selected China , As index starts from 0 and China is the 3rd element

If I want to select by option’s text i.e by using “India”, rather than using value as 3 or index as 2, How can I do that?

–Rahul

Reply

Rahul November 9, 2011 at 10:02 am

Afghanistan
Albania
India
China

Reply

Rahul November 9, 2011 at 10:04 am

select name=”country”
option value=”1″>Afghanistan /option
option value=”2″>Albania /option
option value=”3″>India /option
option value=”*”>China /option
/select

Reply

seetaram November 9, 2011 at 12:04 pm

sel.selectByValue(“India”);

Reply

Mahesh January 4, 2012 at 5:45 pm

Hi Seetaram,

In

Reply

Mahesh January 4, 2012 at 5:50 pm

I want to select “Unread” value from drop down in gmail home screen.
In testCreateAccount Class, i write below code:

selectValue(driver,”Unread”);

and calling by

public static void selectValue(WebDriver driver, String valToBeSelected){
List options = driver.findElements(By.id(“:pj”));
for (WebElement id : options) {
if (valToBeSelected.equalsIgnoreCase(id.getText())){
id.click();
}
}

But its not selecting “Unread” from drop down. Please suggest.

Reply

seetaram January 5, 2012 at 9:04 am

Above method does not work for this… check the HTML source code associated with Gmail home and then you need to tweak this java method according to your needs…

Reply

babu February 18, 2012 at 9:40 am

hi ,seetaram.,
Can u give some examples of webdriver apis like find element

thanks and regards.,
Babu.m

Reply

shmon March 15, 2012 at 2:36 am

Hi Seetaram,

I’m trying to select a checkbox but webdriver is not able to find the element on page, it is selecting radio boxes just fine;

WebElement element = driver.findElement(By.id(“123″));

if(element.isDisplayed()) {
element.click();
System.out.println(element.isSelected());
}

But it gives error that Unable to locate element. Any idea?

Thanks

Reply

sreekumar April 5, 2012 at 11:13 am

Please give example / usage of isSelected() for a dropdownlist. My requirement is to verify selected item from the dropdown list.

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email