×
☰ See All Chapters

How to assert in selenium using Junit – Different assert methods

Assertions are used to validate the state of a value. As assert validates the variable and value, we can use this to determining pass or fail status of a test case in testing.

We use assertions from JUnit framework. From Junit framework, “assert”; checks if the element is on the page. If it is not available, then the test will stop at the step that failed. In our previous examples we used System.out.println() to print the value and check if correct value is assigned. We can avoid using this by using assertions. Below are the important assertion methods of Junit:

Important Assert methods

Method

Example

assertEquals

This method checks that two values are equal. If they are not, the method throws an AssertionError with the given message (if any).

int a = 10;

int b = 20;

int c = 10;

Assert.assertEquals("Values are not equal", a, c);

Assert.assertEquals("Values are not equal", a, b);

Assert.assertEquals(a, c);

Assert.assertEquals(a, b);

assertNotEquals

This method checks that two values not are equal. If they are equal, the method throws an AssertionError with the given message (if any).

int a = 10;

int b = 20;

int c = 10;

Assert.assertNotEquals(a, c);

Assert.assertNotEquals(a, b);

Assert.assertNotEquals("Values not equal", a, c);

Assert.assertNotEquals("Values not equal", a, b);

assertFalse

This method checks that a condition is false. If it isn’t, the method throws an AssertionError with the given message (if any).

int a = 10;

int b = 20;

int c = 30;

Assert.assertFalse("Value is smaller", b>a);

Assert.assertFalse(a>b);

assertTrue

This method checks that a condition is true. If it isn’t, the method throws an AssertionError with the given message (if any).

int a = 10;

int b = 20;

int c = 30;

Assert.assertTrue("Value is greater", b>a);

Assert.assertTrue(a>b);

assertNotNull

This method checks that the object is not null. If it is, the method throws an AssertionError with the given message (if any).

String s = "hello";

Assert.assertNotNull(s);

Assert.assertNotNull("object is null", s);

assertNull

This method checks that the object is null. If it isn’t, the method throws an AssertionError with the given message (if any).

String s = null;

Assert.assertNull(s);

Assert.assertNotNull("object is not null", s);

assertSame

This method checks that two objects refer to the same object using == operator. If they do not, the method throws an AssertionError with the given message (if any).

User user1 = new User("Manu");

User user2 = new User("Advith");

User user3 = user1;

Assert.assertSame(user1, user3);               

Assert.assertSame("Obejcts are same",user1, user2); //AssertionError: Obejcts are same expected same:<User@45ee12a7> was not:<User@330bedb4>

assertNotSame

This method checks that two objects refer to the different object using == operator. If they are same, the method throws an AssertionError with the given message (if any).

User user1 = new User("Manu");

User user2 = new User("Advith");

User user3 = user1;

Assert.assertNotSame("Same", user1, user3); //AssertionError: Same expected not same

Assert.assertNotSame("Same", user1, user2);

Now let us see one example on how to use Junit assert, how to configure Junit.

Step 1: Create project in eclipse

Open Eclipse IDE and create new java project "JunitExample". To learn to setup eclipse and to create project, please refer our  Selenium webdriver setup chapter.

assertions-in-selenium-0
 

Step 2: Download JUnit Java libraries

Open URL: https://search.maven.org/search?q=g:junit%20AND%20a:junit  , Click on Download icon and download jar. Or you can search from google with keyword “Junit jar download”.

assertions-in-selenium-1
 

Step 3: Configure JUnit Java libraries to eclipse project.

Right click on "JunitExample" project and select Build Path> Configure Build Path.

assertions-in-selenium-2
 

Switch to Libraries tab and click on "Add External JARs" button.

assertions-in-selenium-3
 

Navigate to the directory where you have downloaded the Junit jar file, select the junit-4.13.jar and click on "Open" button.

assertions-in-selenium-4
 

Now add Selenium jars, follow the Selenium Webdriver Example chapter to get the steps to add selenium jars.

After you add all the Selenium and junit jar files in your Libraries tab, click on Apply and Close button.

Now expand the Referenced Libraries from the project and check if all jars are added.

assertions-in-selenium-5
 

Step 4: Download ChromeDriver

Open URL: https://sites.google.com/a/chromium.org/chromedriver/downloads

Based on your chrome browser version select the ChromeDriver version

assertions-in-selenium-6
 

Based on your operating system, download the ChromeDriver, We have selected for Windows OS.

assertions-in-selenium-7
 
Extract the files from downloaded zipped file.
assertions-in-selenium-8
 

Please make note of this chromedriver.exe file path which we will use in our java code. Now our path is F:\My_Programs\Selenium\ChromeDriver\chromedriver.exe

Step 5: Create the automation script (Code)

Create the java class with the name “Example” and add the below code to it. Please refer to our previous chapter. We have added comments in the code to explain the steps clearly.

import org.junit.Assert;

import org.openqa.selenium.By;

import org.openqa.selenium.Dimension;

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                // Launch website

                driver.get("https://www.tools4testing.com/contents/selenium/testpages/get-elements-information-in-webdriver-testpage");

               

                String text = driver.findElement(By.id("welcomeMessage")).getText();

                Assert.assertEquals("Welcome to www.tools4testing.com", text);

               

                String tagName = driver.findElement(By.id("welcomeMessage")).getTagName();

                Assert.assertEquals("span", tagName);

               

                String width = driver.findElement(By.id("date")).getCssValue("width");

                Assert.assertEquals("290px", width);

               

                String classAttribute = driver.findElement(By.id("date")).getAttribute("class");

                Assert.assertEquals("datePicker", classAttribute);

               

                Dimension dimension = driver.findElement(By.id("yourname")).getSize();

                Assert.assertEquals(290, dimension.getWidth());

                Assert.assertEquals(38, dimension.getHeight());

               

                Point point = driver.findElement(By.id("yourname")).getLocation();

                Assert.assertEquals(354, point.getX());

                Assert.assertEquals(169, point.getY());

               

                String pageSource = driver.getPageSource();

                Assert.assertNotNull(pageSource);

                Assert.assertTrue(pageSource.startsWith("<html prefix=\"og: https://ogp.me/ns#\">"));

               

                System.out.println("-----------------DONE-----------------");

               

                //wait some time before closing

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                //close the driver

                driver.quit();

        }

}

 

assertions-in-selenium-9
 

Step 6: Execute/Run the program

Right click on the Eclipse code and select Run As > Java Application.

assertions-in-selenium-10
 
assertions-in-selenium-11
 

All Chapters
Author