How to use Assert Statment using Selenium

Posted By : Dinesh Dhiman | 28-Apr-2015

We use Assert statments while testing to verify the output of test case against a predefined set of Test Data .

We use Assert statments for:-

  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

Note : -  If you use assert in your tests then the test will be aborted if the assert fails. Next test case will start executing in case you are running a test suite.

 

There are some assert statements

testNG

 

1. Assert.assertEquals(actual, expected, message);

       2. Assert.assertNotEquals(actual1, actual2, message);

      3. Assert.assertTrue(condition);

       4. Assert.assertSame(actual, expected, message);

      5.  Assert.assertNotSame(actual, expected);

       6. Assert.assertNotNull(object, message);

        

jUnit

       1. Assert.assertArrayEquals(expecteds, actuals);

       2. Assert.assertEquals( expected, actual);

       3. Assert.assertFalse(condition);

       4. Assert.assertNotEquals(first, second);

       5. Assert.assertTrue(condition);

       6. Assert.assertNull(message, object);

 

How to use asserts statements in our testcases

  1. Make a java project

  2. Add all selenium jar files

  3. Make your test case using testNG class or u can also use jUnit for testcases

             (I will demonstrate that example using testNG class)

 

 package com.oodles;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class AssertTestCases {
             String url=”any url ”;
	WebDriver firefoxDriver = new FirefoxDriver();
	@Test
	public void assertExample() {
     Assert.assertEquals("here you enter the title of you web application ", firefoxDriver.getTitle());
		firefoxDriver.findElement(By.linkText("Login")).click();
                         WebElement login =firefoxDriver.findElement(By.id("login-form"));
		if (Assert.assertTrue(login.isDisplayed())){
		firefoxDriver.findElement(By.name("email")).sendKeys("your email id ");
		firefoxDriver.findElement(By.id("login_password")).sendKeys(" your password ");
		login.submit();
                          Assert.assertEquals(msg.getText(), "Please enter a valid email id.");
		}
			}
	@BeforeTest
	public void beforeTest() {
		 firefoxDriver.get(url);
	}
	@AfterTest
	public void afterTest() {
		firefoxDriver.close();
	}
}


 
Thanks
 
Related Tags

About Author

Author Image
Dinesh Dhiman

Dinesh is good in java and grails with having extra knowledge in AngularJS , HTML ,SQL , Selenium

Request for Proposal

Name is required

Comment is required

Sending message..