How To Integrate JUnit Framework Into Selenium Scripts

JUnit is a unit test tool which is open source also. You can use it to test from small units of code to large java functions. JUnit provide assertion library used to examine the result of the test. Junit Annotations are used to test java method. You can also use it to run the automation suite which have a number of test cases.

You can learn following tips in this article.
1. How to add JUnit java library into project.
2. Introduction to JUnit Assertions.

How to add JUnit java library into project.

First of all you need to create a java project using eclipse. You can click here to learn How to create java project in eclipse if you do not know how to do that.

After create the project, open the project’s properties dialog by following steps.
1. Right click your Java project.
2. Click Properties in the popup window.
3. Click “Java Build Path” in the left panel.
4. Click “Libraries” tab in right panel.
5. Click “Add Library…” button in right panel.
add Junit framework java library into java project

6. Choose JUnit in the popup dialog.
add Junit framework java library into java project choose JUnit library

7. Click Next and choose JUnit4. Then click Finish.
add Junit framework java library into java project choose JUnit4

Introduction to JUnit Assertions.

Assertions can be used to verify specific condition, The test method will stop executing when the condition is unsatisfied.

1. assertEquals: This is used to check whether two variable’s value is equal or not, if not equal then failed. You can use this method to compare Boolean, String, int, float, char, long etc.

Syntax and examples:

 Assert.assertEquals("wanted value", "real value");
 //Assertion pass because the two string are same.
 Assert.assertEquals("dev2qa.com","dev2qa.com"); 
 //Assertion fail because the two string are not same.
 Assert.assertEquals("dev2qa.com","google.com"); 
 //message thrown when the two string are is not same.
 Assert.assertEquals("Assertion fails because string's value is not equal.", "dev2qa.com", "java.com");

Code example to use JUnit assertion in java code for selenium testing:

//Get username by id using webdriver.

 String username = fireFoxDriver.findElement(By.id(“username”)).getText();

 //Get password by id using webdriver.

 String password = fireFoxDriver.findElement(By.id(“password”)).getText();

 //If username is not equal to password's value then assertion fail.

 Assert.assertEquals("User name is not equal to password", username, password);

If the two compared string’s value is not equal then an exception will be thrown in the java console.
JUnit string value not equal assertions fail exception

READ :   Convert Excel To JSON In Java Example

2. assertTrue: Return true when the condition parameter’s value is true, assertion fails when the condition parameter’s value is false.

 //Assertion fail because 1 do not equal to 2.
 Assert.assertTrue("1 is not equal to 2.", 1==2);
 //Return true because the string's value is same.
 Assert.assertTrue("Both the strings are not equal", ("dev2qa.com").equals("dev2qa.com"));

3. assertFalse: Return true when the condition parameter’s value  is false, assertion fails when the condition parameter’s value  is true.

 //Assertion pass because 1 do not equal to 2.
 Assert.assertFalse("1 is not equal to 2.", 1==2);
 //Assertion fail because the string's value is same.
 Assert.assertFalse("Both the strings are not equal", ("dev2qa.com").equals("dev2qa.com"));
(Visited 114 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.