`

Selenium 11:Introduction to JUnit Framework and Its Usage in Selenium Script

 
阅读更多

http://www.softwaretestinghelp.com/selenium-junit-framework-selenium-tutorial-11/

 

This tutorial will give an insight about JUnit and its usage in selenium script. This is tutorial #11 in our comprehensive Selenium tutorials series.

Basically JUnit is an open source unit testing tool and used to test small/large units of code. To run the JUnit test you don’t have to create class object or define main method. JUnit provide assertion library which is used to evaluate the test result. Annotations of JUnit are used to run the test method. JUnit is also used to run the Automation suite having multiple test cases.

Adding JUnit library in Java project

First we will learn how to add JUnit library in your Java project:

Step #1: Right click on Java project->Build Path->Configure Build path

Step #2: Click Libraries->Add Library

Junit framework Selenium script

Step #3: Click on Junit.

Junit framework Selenium script 2

Step #4: Select Junit4->Finish

Junit framework Selenium script 3

Step #5: Click OK.

Junit framework Selenium script 4

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.

JUnit Annotations Used in Selenium scripts

There are many annotations available in Junit. Here we have described few annotations which are used very frequently in Selenium scripts and framework.

#1. @Test

@Test annotation is used to run a Junit test.

Example:

1 @Test
2 public void junitTest()
3 {
4 System.out.println("Running Junit test");
5 Assert.assertEquals(1,1);
6 }

How to Run a Junit test:

Navigate to run ->Run as Junit test

#2. @Before:

@Before annotation is used to run any specific test before each test.

1 public class Junttest {
2 @Before
3 public void beforeTest(){
4 System.out.println("Running before test");
5 }
6  
7 @Test
8 public void junitTest(){
9 System.out.println("Running Junit test");
10 }
11 }

Output:
Running before test
Running Junit test

Example of before annotation using two junit test method.

1 public class Junttest {
2 @Before
3 public void beforeTest(){
4 System.out.println("Running before test");
5 }
6  
7 @Test
8 public void junitTest(){
9 System.out.println("Running Junit test");
10 }
11  
12 @Test
13 public void secondJunitTest(){
14 System.out.println("Running second Junit test");
15 }
16 }

Output:
Running before test
Running Junit test
Running before test
Running second Junit test

Before running junitTest method beforeTest method will run. Similarly before running secondJuntiTest again beforeTest method will run and produces output like above.

#3. @BeforeClass

This method executes once before running all test. The method has to be a static method. Initialization of properties files, databases etc are done in beforeClass method.

1 public class Junttest {
2 @BeforeClass
3 public static void beforeClassTest(){
4 System.out.println("Executed before class method");
5 }
6  
7 @Test
8 public void junitTest(){
9 System.out.println("Running Junit test");
10 }
11  
12 @Test
13 public void secondJunitTest(){
14 System.out.println("Running second Junit test");
15 }
16 }

Output:
Executed before class method
Running Junit test
Running second Junit test

#4. @After

This method executes after each test.

1 public class Junttest {
2 @Test
3 public void junitTest(){
4 System.out.println("Running Junit test");
5 }
6  
7 @After
8 public void afterTest(){
9 System.out.println("Running after method");
10 }
11 }

Output:
Running Junit test
Running after method

#5. @AfterClass

Like @BeforeClass, @AfterClass executes once after executing all test methods. Like @BeforeClass method, @AfterClass method has to be a static method.

1 public class Junttest {
2  
3 @Test
4 public void junitTest(){
5 System.out.println("Running Junit test");
6 }
7  
8 @Test
9 public void secondJunitTest(){
10 System.out.println("Running second Junit test");
11 }
12  
13 @AfterClass
14 Public static void afterClassTest(){
15 System.out.println("Running afterclass method");
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:

Junit framework Selenium script 5

1 @RunWith(Parameterized.class)
2 public class Junttest {
3 public String name;
4 public int age;
5 public Junttest(String name,int age){
6 this.name=name;
7 this.age=age;
8 }
9  
10 @Test
11 public void testMethod(){
12 System.out.println("Name is: "+name +" and age is: "+age);
13 }
14  
15 @Parameters
16 public static Collection<Object[]> parameter(){
17 Object[][] pData=new Object[2][2];
18 pData[0][0]="Tom";
19 pData[0][1]=30;
20 pData[1][0]="Harry";
21 pData[1][1]=40;
22 return Arrays.asList(pData);
23 }
24 }

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 String username=driver.findElement(By.id(“username”)).getText();
2 String password=driver.findElement(By.id(“password”)).getText();
3 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.
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.

分享到:
评论

相关推荐

    基于Python的selenium操作:判断元素是否存在+判断元素是否可以点击.zip

    【Python的selenium操作:判断元素是否存在】 在Python的自动化测试中,Selenium是一个非常强大的工具,用于模拟用户与网页的交互。Selenium库提供了一系列API,使得我们可以控制浏览器进行各种操作,例如点击按钮...

    selenium framework design data driven testing

    Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage...

    selenium+junit自动化测试框架

    1.seleRrj是工程包,包含简单的自动化测试demo以及jar包,已经加入junit4支持;调试运行通过; 2.selenium-fireFox插件,安装在火狐浏览器里面 用于录制自动化脚本和学习自动化测试; 3.我用的火狐版本是v21.0版本,...

    Selenium终极自动化测试环境搭建(一):Selenium+Eclipse+Junit+TestNG.docx

    在 Eclipse 中,需要创建一个新的 Java 项目,添加 Junit 和 Selenium 相关的依赖项,然后编写测试用例来执行自动化测试。 Selenium 终极自动化测试环境搭建需要完成以上七个步骤,包括安装 JDK、下载 Eclipse 和 ...

    Selenium+Eclipse+Junit+TestNG.docx

    Selenium+Eclipse+Junit+TestNG 自动化测试框架搭建指南 本文将详细介绍如何使用 Selenium+Eclipse+Junit+TestNG 搭建自动化测试框架,包括安装 JDK、Eclipse、Selenium IDE、Selenium RC、IEDriverServer、...

    selenium开发:谷歌浏览器和驱动配套版本

    在进行自动化测试时,Selenium 是一款非常强大的工具,它允许开发者编写脚本来模拟用户对网页的操作。在本文中,我们将深入探讨Selenium与谷歌浏览器(Chrome)以及对应的Chrome驱动(ChromeDriver)的配合使用,...

    selenium-jupiter:Selenium的JUnit 5扩展

    Selenium-Jupiter是一个扩展,旨在简化JUnit 5测试中对Selenium的使用。 该库是开源的,根据的条款。 目录 动机 Selenium-Jupiter允许以简单的方式从测试执行Selenium。 为此, Selenium-Jupiter充分利用了几个...

    selenium私房菜系列

    Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....

    selenium+junit+hudson自动化测试初探

    3. **Eclipse**:创建Java项目,导入Selenium和JUnit库,配置JUnit插件和feed4junit,以支持参数化测试。 **编写和执行测试脚本** 在Eclipse中,使用JUnit编写测试类,集成Selenium WebDriver,通过IDE录制的脚本...

    junit与selenium集成使用手册

    ### JUnit与Selenium集成使用手册 #### 一、引言 随着软件开发技术的不断发展,自动化测试成为了提高软件质量的重要手段之一。JUnit 和 Selenium 是两款广泛使用的自动化测试工具,JUnit 主要用于 Java 应用程序的...

    selenium+java+junit的使用.zip

    selenium+java+junit的使用,可以提供给正在学习软件测试的学弟学妹们借鉴和参考!

    终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python

    终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python 本文旨在指导读者搭建一个终极自动化测试环境,利用 Selenium+Eclipse+Junit+TestNG+Python 实现自动化测试。下面是详细的搭建过程: 一、安装 JDK...

    java软件工程:系统测试报告文档-测试工具:JMeter、Junit、Selenium

    1、项目简介 1.1项目业务功能介绍 主要业务功能介绍(通过流程图/功能结构图进行阐述) 1.2术语及主要名称介绍 ...4.4 自动化测试Selenium(Chrome+Java) 5、测试报告及分析 5.1测试报告 5.2缺陷报告 5.3分析总结

    《python3网络爬虫开发实战》学习笔记::selenium——xpath:Unable to locate element

    selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...

    firefox+JUnit+Selenium

    标题 "Firefox + JUnit + Selenium" 暗示了我们讨论的主题是关于使用Firefox浏览器、JUnit测试框架和Selenium自动化测试工具的集成测试方案。这三个组件在IT领域,特别是软件测试中发挥着重要的作用。 首先,Fire...

    Selenium需要的jar包(selenium-client-driver;selenium-server;junit4.0).rar.rar

    Selenium是一款广泛应用于Web应用程序自动化测试的开源框架。它提供了多种编程语言的API,包括Java、Python、C#等,使得测试人员可以编写脚本来模拟用户在浏览器中的各种操作,如点击、输入、导航等。Selenium的核心...

Global site tag (gtag.js) - Google Analytics