Selenium APIs for Some Basic Test Automation Actions

by seetaram on July 6, 2011

If you think about Test Automation, what are the basic actions comes into mind? Clicking on a button, selecting some value from a dropdown, typing in some value into an edit box, clicking on a link, checking a checkbox, clicking on a radio button.

Below are some of the actions we can list:

  1. Click
  2. Select
  3. Type
  4. Check / Uncheck

Let us start exploring APIs provided by Selenium Web Application Test Automation tool for these actions.

CLICK:

To click on any object Selenium provides selenium.click (“Locator”) API.  Using this we can click on an object on the user interface.

TYPE:

To type some value into an edit box or any control where we can key in values we can utilize the selenium method “selenium.type(“locator”,”value”)”.  For keying in some values into an object we need to first locate the object on the UI and then need to type the value.

SELECT:

For selecting a value from a dropdown, “selenium.select(“locator”,”value”)” API of selenium can be utilized.

In the above selenium methods “locator” can be ID, name, or xpath of an object which is used to identify the object.

CHECK/UNCHECK:

There are two methods selenium.check and selenium.uncheck which are used to check / uncheck the Web Checkboxes on the application under test.  Usage is illustrated in the below code snippet.

Let us have a look at the below example code:

package com.selftechy.sqaforums;

/*
 * Author - Seetaram Hegde
 */

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

public class SeleniumAPIs extends SeleneseTestCase {
	private static SeleniumServer seleniumServer;
	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
        seleniumServer = new SeleniumServer();
        seleniumServer.start();
		selenium.start();
		selenium.windowMaximize();
	}

	@Test
	public void testOpenTypeClick() throws Exception {
		selenium.open("/");
		selenium.click("link=Advanced search");
		selenium.waitForPageToLoad("30000");
		selenium.type("as_q", "selftechy, selenium");
		selenium.click("//input[@value='Advanced Search']");
		selenium.waitForPageToLoad("30000");
	}

	public void testSelectCheck() throws Exception {
		selenium.open("http://www.sqajobs.com/");
		selenium.click("link=Advanced Search");
		selenium.waitForPageToLoad("30000");
		selenium.type("bx_jtitle", "Sr. Testing Engineer");
		selenium.select("rdjt", "label=All Of These");
		selenium.removeSelection("jids[]", "label=All Categories");
		selenium.addSelection("jids[]", "label=Functional Testing");
		selenium.type("bx_kwd", "selenium");
		selenium.click("idrdKeyw1");
		selenium.click("id_alltype");
		selenium.check("id_jtp_1");
		selenium.uncheck("id_jtp_1");
		selenium.check("id_jtp_1");
		selenium.click("//input[@name='cmdSearch' and @value='  Search  ']");
		selenium.waitForPageToLoad("30000");
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
		seleniumServer.stop();
	}

}

The above example illustrates the usage of various Selenium APIs such as selenium.click, selenium.check, selenium.type, selenium.select, selenium.addSelection, selenium.removeSelection, selenium.windowMaximize, selenium.click, etc.  First start automating test cases using Selenium IDE and export the code into your favorite programming language such as Java, C#, PHP, etc.  Then learn using Firebug, Firepath, Xpath Checker, etc to find out the ID, name, or XPath to identify the object.  Map these locators to string variables with meaningful names and use them in the tests.

{ 4 comments… read them below or add one }

Anantha November 29, 2011 at 12:00 pm

Hi Seetaram,

In the above example you have mentioned “selenium.click(“locator”,”value”)” for the type category. I reckon it is typo error. My special thanks to you. You are a real genius and I followed your blog and one more blog to learn selenium. Your explanation is so user friendly. Keep up the good work and share us the difficult situations you faced with some application and how you fixed it. It will be helpful for us.

Thanks ,
Anantha

Reply

seetaram November 29, 2011 at 6:04 pm

Thanks Anantha for pointing out the error.. will fix it…

Reply

Murali Kedari September 26, 2012 at 10:45 am

If u put scripts ,what u faced in ur project, then users can understand.

Reply

midhun November 12, 2012 at 8:31 am

Hi Seetaram,

Can you please let me know how you handle secured websites (ex:- websites with “https”) with an example.

Thanks in advance.
Midhun.G

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