Beruflich Dokumente
Kultur Dokumente
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Search
How to Use TestNG Framework for Creating Selenium Scripts TestNG Selenium Tutorial #12
1/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
2/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
There are many frameworks like Data Driven Framework, Keyword Driven Framework, and Hybrid
Framework which use Junit tool as test runner and which will help to start the batch execution and
reporting.
3/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
#1. @Test
@Test annotation is used to run a Junit test.
Example:
1
2
3
4
5
6
@Test
public void junitTest()
{
System.out.println("Running Junit test");
Assert.assertEquals(1,1);
}
Output:
Running before test
Running Junit test
Example of before annotation using two junit test method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output:
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
4/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Output:
Executed before class method
Running Junit test
Running second Junit test
#4. @After
This method executes after each test.
1
2
3
4
5
6
7
8
9
10
11
Output:
Running Junit test
Running after method
#5. @AfterClass
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
5/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Like @BeforeClass, @AfterClass executes once after executing all test methods. Like @BeforeClass
method, @AfterClass method has to be a static method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output:
Running Junit test
Running second Junit test
Running afterclass method
------------
Junit assertions are used to validate certain condition and stops execution of program if the conditions
are not satisfied.
#6. Parameterized Junit class:
Parameterized class is used to run same scenario with multiple dataset.
Below is the example to pass multiple parameters in a Junit test.
@Parameters annotation tag is used to pass multiple data. Here, we have taken 2*2 dimensional array
and the data can be visualized like below:
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
6/14
3/17/2015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
@RunWith(Parameterized.class)
public class Junttest {
public String name;
public int age;
public Junttest(String name,int age){
this.name=name;
this.age=age;
}
@Test
public void testMethod(){
System.out.println("Name is: "+name +" and age is: "+age);
}
@Parameters
public static Collection<Object[]> parameter(){
Object[][] pData=new Object[2][2];
pData[0][0]="Tom";
pData[0][1]=30;
pData[1][0]="Harry";
pData[1][1]=40;
return Arrays.asList(pData);
}
}
JUnit Assertions
JUnit assertEquals: This checks if the two values are equal and assertion fails if both values are not
equal.
This compares Boolean, int, String, float, long, char etc.
Syntax:
Assert.assertEqual(excepted value, actual value);
Example:
Assert.assertEqual(ABC,ABC); //Both the strings are equal and assertion will pass.
Assert.assertEqual(ABC,DEF); //Assertion will fail as both the strings are not equal.
Assert.assertEqual(Strings are not equal, ABC,DEF); //message will be thrown if equal
condition is not satisfied.
Below is the example of use of JUnit assertion in selenium:
1
2
3
String username=driver.findElement(By.id(username)).getText();
String password=driver.findElement(By.id(password)).getText();
Assert.assertEqual(Mismatch in both the string, username, password);
In above example assertion will fail as both the strings are not equal. One is text of username field and
other is the text of password field.
JUnit assertTrue: Returns true if the condition is true and assertion fails if the condition is false.
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
7/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Assert.assertTrue(message, condition);
Assert.assertTrue(Both the strings are not equal, (HelloWorld).equals(HelloWorld));
Here assertion will pass as both the strings match. It will print message if the assertion fails.
JUnit assertFalse: Returns true if the condition is false and assertion fails if the condition is true.
Assert.assertFalse(message, condition);
Assert.assertFalse(Both the strings are equal, (Hello).equals(HelloWorld));
There will not be any assertion error as the condition is false.
Conclusion:
Most of the programmers use Junit as it is easy and does not take much effort to test. A simple green
or red bar will show the actual result of the test. Junit makes life easy as it has its own set of libraries
and annotations. Here we have also described commonly used annotations used with selenium scripts
and framework.
More detail about framework and use of Junit annotations will be discussed in upcoming tutorial
which is dedicated exclusively for framework design using Junit. This tutorial will help us in
designing the framework using Junit.
Next Tutorial #12: In next tutorial we would discuss all about TestNG, its features and its
applications. TestNG is an advance framework designed in a way to leverage the benefits by both the
developers and testers.
1
Like
Share
2
Tweet
Recommended reading:
Usage of Selenium Select Class for Handling Dropdown Elements on a Web Page Selenium
Tutorial #13
How to Use TestNG Framework for Creating Selenium Scripts TestNG Selenium Tutorial
#12
Implementation of Our First WebDriver Script Selenium WebDriver Tutorial #10
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
8/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
5 comments
#1 Supriya
thanks for junit tutorial. can we have other frameworks info as well?
#2 Rahul
Hi Shruti, I have two questions:
1. Whenever I am running the @BeforeClass annotation, the second junit class runs before the
first class.
Instead of this :
Executed before class method
Running Junit test
Running second Junit test
I get this :
Executed before class method
Running second Junit test
Running Junit test
Why did it happen?
Secondly, I am getting an initialization error if I run the parameterized example.
#3 Bhavesh
Hi Testing Team,
can you explain Parameterized Junit class example in details with list of package that are used
in program.
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
9/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
#4 Hui
Thank you for the tutorial. It helps me a lot.
I ran the JunitTest and got the result like this:
Running before test
Running second Junit test
Running before test
Running Junit test
#5 surendra
Hi All ,
I Ran Jnuit frame frame is working.but i have one small doubt how to pass the import the
parameters Excel file in to junit.
Leave a Comment
Name
Mail
Website (Optional)
Submit
Get FREE
eBooks +
Blog
Updates
Enter Email:
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
10/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Recommended eBook
Learn & Master Software Testing Quickly from Experts Click here to know more.
Adv
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
11/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Latest Articles!
How to Figure Out What QA Job Best Suits You Based on Your Skills?
11 Ways You Know Youre a Tester..
Case Study: How to Eliminate Flaws of Waterfall and Agile Development Processes
Using a Hybrid Model
8 Proven Ways to Manage Stress as a Software Testing Professional
What is Software Testing All About? (Read This 10 Point Summary Carefully)
How to Perform White Box Testing Explained with a Simple Example
How to Plan and Manage Testing Projects Effectively (Tips)
Java Tutorials
Selenium Testing
Selenium Automation
START HERE!
Home
Get FREE Updates
Testing RESOURCES
QA Testing TRAINING
Premium eBook
FREE eBooks
ALL Articles
Contact Us
What Readers Say About Us
Your Feedback!
QA Forum
12/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Categories
Select Category
Follow Us!
Follow @VijayShinde
Follow
Like
6.3k
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
13/14
3/17/2015
Introduction to JUnit Framework and Its Usage in Selenium Script Selenium Tutorial #11 Software Testing Help
Search
http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/
14/14