Selenium – Use Assertions to Verify the Test Output

by seetaram on July 10, 2011

In Software Testing we need to verify the output of test case against a predefined set of Test Data. Selenium provides various Selenese commands to achieve this.  Let us try to explore some of the situations we come across in Test Automation.

  1. Verify whether an object is visible (such as a button, link, etc)
  2. Verify whether a checkbox is checked or unchecked.
  3. Check if an object is present on the screen
  4. Check if an Editbox is editable (i.e. we can key in values into edit box)
  5. Check if a particular value is selected in the dropdown box

Selenium provides several different APIs for automating the Assertions.

I have created a sample web site and uploaded to Google docs.  Download link

Download the sample web site and unzip into a folder.  I will be using the same web site in my sample Selenium code.

Following are some of the Selenium assertions:

  1. assertAlertPresent – checks whether an alert is present
  2. assertAllButtons – compares the buttons present on the screen with the provided values in the test
  3. assertAllLinks – compares all the links present on the screen with the one’s given in the test
  4. assertChecked – verifies whether the particular checkbox is checked
  5. assertEditable – verifies the Edit box whether it is possible to key in values into the edit box
  6. assertSelectedValue – compares the given value with the selected value in the specified dropdown

Let us try to use these assertions in the Selenium Test by recording a test.  After recording we will export them into JUnit format and compare them to the recorded test in the table format in Selenium IDE.

RecordedAssertions

In the above picture we can see the exact commands used in the Selenium IDE for assertions whereas in the below code which is exported in JUnit format from the Selenium IDE, only assertTrue and assertEquals are present.  Here, other selenium methods such as selenium.getSelectedLabel("city"), selenium.isTextPresent("Name of the Customer"), and selenium.getAlert() are used for assertions.

package com.selftechy.assertions;

/*
 * Author - Seetaram Hegde
 */
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;

public class SeleniumAssertions extends SeleneseTestCase {
	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.sqajobs.com/");
		selenium.start();
		selenium.windowMaximize();
	}

	@Test
	public void testAssertions() throws Exception {
		//change the below directory path to the one where you have unzipped the sample website files 
		selenium.open("file:///F:/Helios-Workspace/Sample%20Website/CreateAccount.htm");
		selenium.type("FirstName", "seetaram");
		selenium.type("Lname", "hegde");
		selenium.type("street", "HSR Layout");
		selenium.click("male");
		selenium.click("kannada");
		selenium.click("english");
		selenium.click("hindi");
		selenium.click("Save");
		assertEquals("New Account Created", selenium.getAlert());
		selenium.click("link=Click to View Customer Information");
		selenium.waitForPageToLoad("30000");
		Thread.sleep(200);
		assertTrue(selenium.isTextPresent("Below table contains the information about the Customer's transactions"));
		assertTrue(selenium.isTextPresent("Name of the Customer"));
		selenium.click("link=Go to Account Creation Screen");
		selenium.waitForPageToLoad("30000");
		Thread.sleep(200);
		assertEquals("Bangalore", selenium.getSelectedLabel("city"));
	}

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

Use the following Selenium APIs for assertions:

  1. selenium.isTextPresent() – To verify whether the specified text is present
  2. selenium.getAlert() – To close the Javascript alert
  3. selenium.getSelectedLabel() – To get the selected label (dropdown option) from the dropdown
  4. selenium.chooseOkOnNextConfirmation() – To click the OK button on the Javascript alert
  5. selenium.chooseCancelOnNextConfirmation() – To click the Cancel button on the Javascript alert
  6. selenium.doubleClick(“Locator”) – double click an object
  7. selenium.isChecked("locator") – Verify whether a checkbox is checked or not
  8. selenium.isEditable("locator") – Verify whether an Editbox is editable or not
  9. selenium.isElementPresent("locator") – This is a very much useful Selenium method.  Whenever there is a need to wait for some time to load the page then we can use this API in conjunction with Thread.sleep()
  10. selenium.isVisible() – To verify whether the object is visible

There are some more Selenium APIs available but above are the one’s which are most regularly used.

{ 11 comments… read them below or add one }

Anju August 27, 2011 at 2:16 pm

Hi seetaram,

I read thru ur blog. Its was really useful for me to start with Selenium automation. In this particular blog you have mentioned about the ‘Selenium APIs for assertions’. But all these have changed for the web driver i think.

Could you pls list the assertions to be used while using web driver.

Thanks,
Anja

Reply

seetaram August 30, 2011 at 5:36 am

You can write your own assertions.. e.g. assertNotNull, assertElementPresent, etc. Use APIs such as getText, getValue, isEnabled, isSelected, etc.

Reply

Chaithra June 2, 2012 at 6:55 am

Hi,

This blog is worth and really helpful. I appriciate and anticipate still more articles on Selenium.

Thanks & Regards,
Chaithra

Reply

Rajesh November 30, 2012 at 4:21 pm

Hi Seetharam,
Your blog is good and useful.Can you give an example for assertAllButtons
Also is there any way to validate the options from a dropdown with a given set of values?

Reply

Rajesh November 30, 2012 at 4:28 pm

Also i am not able to use assertAllButtons ? do i need to import any extra jar to make use of this?

Reply

prince December 20, 2012 at 10:46 am

hai when i run the above code with junit am getting an error in the main funtion which is below the annoatation @test can u please help me..

Reply

shameer February 7, 2013 at 10:03 am

Thanks seetaram,
can please convert this into phpunit
thanks

Reply

seetaram March 7, 2013 at 6:11 am

You can definitely do that. I had to work on a POC where the client wanted us to develop the scripts in PHP. Using PHPUnit, use PHP Webdriver I do not remember exactly from where I have downloaded (remember like it was Git) and there you need to run the selenium server unlike java client for web driver where you need not to run any server

Reply

Vaibhav August 11, 2013 at 12:13 pm

Hi Seetaram,

Being at begineers level I have few queries

1)assertEquals(“New Account Created”, selenium.getAlert()); > Here does this mean ‘if’ there is a alert popup it was closed or this is the syntax?
2)assertTrue(selenium.isTextPresent(“Name of the Customer”));
Can this be used for method call if Ye and how.Any other example for method call will be appreciated.

Reply

seetaram August 20, 2013 at 9:22 am

Try this in a JUnit test. This is pretty simple!!

Cheers
seetaram

Reply

firoz shaik March 1, 2014 at 5:51 am

Hi Seetha Ram, God Bless you and your family. You are doing a wonderful job for us. Thanks for a very much detail explanation.

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