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
Step #3: Click on Junit.
Step #4: Select Junit4->Finish
Step #5: Click OK.
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:
2 |
public void junitTest() |
4 |
System.out.println("Running Junit test"); |
5 |
Assert.assertEquals(1,1); |
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 { |
3 |
public void beforeTest(){ |
4 |
System.out.println("Running before test"); |
8 |
public void junitTest(){ |
9 |
System.out.println("Running Junit test"); |
Output:
Running before test
Running Junit test
Example of before annotation using two junit test method.
1 |
public class Junttest {
|
3 |
public void beforeTest(){
|
4 |
System.out.println( "Running before test" );
|
8 |
public void junitTest(){
|
9 |
System.out.println( "Running Junit test" );
|
13 |
public void secondJunitTest(){
|
14 |
System.out.println( "Running second Junit test" );
|
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 {
|
3 |
public static void beforeClassTest(){
|
4 |
System.out.println( "Executed before class method" );
|
8 |
public void junitTest(){
|
9 |
System.out.println( "Running Junit test" );
|
13 |
public void secondJunitTest(){
|
14 |
System.out.println( "Running second Junit test" );
|
Output:
Executed before class method
Running Junit test
Running second Junit test
#4. @After
This method executes after each test.
1 |
public class Junttest {
|
3 |
public void junitTest(){
|
4 |
System.out.println( "Running Junit test" );
|
8 |
public void afterTest(){
|
9 |
System.out.println( "Running after method" );
|
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 {
|
4 |
public void junitTest(){
|
5 |
System.out.println( "Running Junit test" );
|
9 |
public void secondJunitTest(){
|
10 |
System.out.println( "Running second Junit test" );
|
14 |
Public static void afterClassTest(){
|
15 |
System.out.println( "Running afterclass method" );
|
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:
1 |
@RunWith (Parameterized. class )
|
2 |
public class Junttest {
|
5 |
public Junttest(String name, int age){
|
11 |
public void testMethod(){
|
12 |
System.out.println( "Name is: " +name + " and age is: " +age);
|
16 |
public static Collection<Object[]> parameter(){
|
17 |
Object[][] pData= new Object[ 2 ][ 2 ];
|
22 |
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 |
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操作:判断元素是否存在】 在Python的自动化测试中,Selenium是一个非常强大的工具,用于模拟用户与网页的交互。Selenium库提供了一系列API,使得我们可以控制浏览器进行各种操作,例如点击按钮...
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...
1.seleRrj是工程包,包含简单的自动化测试demo以及jar包,已经加入junit4支持;调试运行通过; 2.selenium-fireFox插件,安装在火狐浏览器里面 用于录制自动化脚本和学习自动化测试; 3.我用的火狐版本是v21.0版本,...
在 Eclipse 中,需要创建一个新的 Java 项目,添加 Junit 和 Selenium 相关的依赖项,然后编写测试用例来执行自动化测试。 Selenium 终极自动化测试环境搭建需要完成以上七个步骤,包括安装 JDK、下载 Eclipse 和 ...
Selenium+Eclipse+Junit+TestNG 自动化测试框架搭建指南 本文将详细介绍如何使用 Selenium+Eclipse+Junit+TestNG 搭建自动化测试框架,包括安装 JDK、Eclipse、Selenium IDE、Selenium RC、IEDriverServer、...
在进行自动化测试时,Selenium 是一款非常强大的工具,它允许开发者编写脚本来模拟用户对网页的操作。在本文中,我们将深入探讨Selenium与谷歌浏览器(Chrome)以及对应的Chrome驱动(ChromeDriver)的配合使用,...
Selenium-Jupiter是一个扩展,旨在简化JUnit 5测试中对Selenium的使用。 该库是开源的,根据的条款。 目录 动机 Selenium-Jupiter允许以简单的方式从测试执行Selenium。 为此, Selenium-Jupiter充分利用了几个...
Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....
3. **Eclipse**:创建Java项目,导入Selenium和JUnit库,配置JUnit插件和feed4junit,以支持参数化测试。 **编写和执行测试脚本** 在Eclipse中,使用JUnit编写测试类,集成Selenium WebDriver,通过IDE录制的脚本...
### JUnit与Selenium集成使用手册 #### 一、引言 随着软件开发技术的不断发展,自动化测试成为了提高软件质量的重要手段之一。JUnit 和 Selenium 是两款广泛使用的自动化测试工具,JUnit 主要用于 Java 应用程序的...
selenium+java+junit的使用,可以提供给正在学习软件测试的学弟学妹们借鉴和参考!
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python 本文旨在指导读者搭建一个终极自动化测试环境,利用 Selenium+Eclipse+Junit+TestNG+Python 实现自动化测试。下面是详细的搭建过程: 一、安装 JDK...
1、项目简介 1.1项目业务功能介绍 主要业务功能介绍(通过流程图/功能结构图进行阐述) 1.2术语及主要名称介绍 ...4.4 自动化测试Selenium(Chrome+Java) 5、测试报告及分析 5.1测试报告 5.2缺陷报告 5.3分析总结
selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...
标题 "Firefox + JUnit + Selenium" 暗示了我们讨论的主题是关于使用Firefox浏览器、JUnit测试框架和Selenium自动化测试工具的集成测试方案。这三个组件在IT领域,特别是软件测试中发挥着重要的作用。 首先,Fire...
Selenium是一款广泛应用于Web应用程序自动化测试的开源框架。它提供了多种编程语言的API,包括Java、Python、C#等,使得测试人员可以编写脚本来模拟用户在浏览器中的各种操作,如点击、输入、导航等。Selenium的核心...