LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

What are the most common JUnit annotations in Selenium?

I wanted to know some of the most common JUnit annotations in Selenium, can anyone please suggest me some.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development. To execute Selenium WebDriver testing using JUnit we have to add JUnit annotations in your test.

Common JUnit Annotations In Selenium

@BeforeClass

@Before

@Test

@After

@AfterClass

@Ignore

JUnit annotations In Selenium For Specific Purposes

@ParameterizedTest

@RunWith

@RepeatedTest

@Parameters

Digging Deeper with Examples of JUnit Annotations In Selenium WebDriver

Now, we look at each annotation one by one and at the end, I’ll present you the entire compiled code representing all the annotations along with the console output, at the end of this section.

@BeforeClass

This annotation is used to initialize any object that we use in our running test case. Whenever, we initialize any object in BeforeClass method, it would be invoked only once. The primary purpose of @BeforeClass JUnit annotation is to execute some statements before all the test cases mentioned in the script.

@BeforeClass

public static void SetUpClass()

{

//Initialization code goes here

System.out.println("This is @BeforeClass annotation");

}

@Before

This annotation is used whenever we want to initialize any object for the time the method is being used. Suppose, we are having 5 test cases, the Before method will be called before each test method for 5 times. So, it would be invoked every time the test case is executed. This annotation is usually used for setting up the test environment.

@Before

public void SetUp()

{

// Setting up the test environment

System.out.println("This is @Before annotation");

}

@Test

This annotation tells JUnit that the public void method() to which it is attached can be run as a test case. This annotation includes the test method for an application that you want to test. A single automation test script may comprise numerous test methods in it.

@Test

public void Addition()

{

c= a+b;

assertEquals(15,c);

System.out.println("This is first @Test annotation method= " +c);

}

@Test

public void Multiplication()

{

c=a*b;

assertEquals(50,c);

System.out.println("This is second @Test annotation method= " +c);

}

@After

Whatever we have initialized in @Before annotation method, that initialization should be released in @After annotation method. So, this annotation is executed every time after each test method. The primary purpose of @After annotation is to teardown. Teardown is a process of deleting temporary data. Teardown can also be used to define the default values or to wipe the slate clean of the test environment.

@After

public void TearDown()

{

// Cleaning up the test environment

c= null;

System.out.println("This is @After annotation");

}

@AfterClass

Whatever we have initialized in @BeforeClass annotation method, that initialization should be released in @AfterClass annotation method. So, this annotation is executed once but it will be executed after all tests has finished executing.

@AfterClass

public static void TearDownClass()

{

//Release your resources here

System.out.println("This is @AfterClass annotation");

}

@Ignore

This annotation tells JUnit that this method shall not be executed. In scenarios, where our code module is not ready in a particular test case, we can temporarily put that code module in @Ignore annotation method to avoid test case failure. Native JUnit 4 offers powerful reporting to help you realize the count of tests that were ignored along with count of tests that run and count of tests that failed.

@Ignore

public void IgnoreMessage()

{

String info = "JUnit Annotation Blog" ;

assertEquals(info,"JUnit Annotation Blog");

System.out.println("This is @Ignore annotation");

}

Performing Cross Browser Testing with JUnit annotations On Selenium Grid

Cross Browser Testing, as the name suggests, is a practice to test your website or web-application on different browsers, browser versions and on different operating systems as well, making sure that the particular application works as expected in desired configurations without any compromise in quality. Primarily, with browser compatibility testing, we test the appearance and functionality of a website. The purpose of cross browser testing is to provide consistent behavior of any application on different browsers, devices and platforms. Selenium helps us to perform automated cross browser testing for pacing our go-to-market delivery.

Selenium comes under 4 variants –

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

Selenium Grid would be the best option for automated cross browser testing, as it empowers users to perform parallel testing in Selenium for which we would be using Java with JUnit framework. For this, we would be running our automated test script on LambdaTest, a cross browser testing tool to realize the use of common JUnit annotations in Selenium by performing cross browser testing.

Up Vote Down Vote 0 Votes
Posted 3 months ago