web analytics
Automate TestNG in Selenium

What is the TesNG Framework?

TestNG is an open-source automation framework for Java. TestNG contains more features than Junit, which makes it a more robust framework. TestNG was created by Cedric Beust and is mostly used by developers and testers.

What is TestNG in selenium?

TestNG provides features like annotations, data-driven testing, test sequencing, and parallel testing which makes tests execute more effectively and efficiently.

The benefits are :

  • Running the test in parallel.
  • TestNG annotations like BeforeSuite AfterSuite, BeforeTest, AfterTest, BeforeMethod and AfterMethod.
  • Data-driven testing can be done with multiple data.
  • Report and log features are much better in the TestNG Framework. 

Why use TestNG with selenium?

  • Cross-browser testing.
  • Easily understand annotations.
  • XML files can be used to group test cases.
  • Many test cases and their status of pass-fail skipped can be included in the report.
  • Proper reporting.

Installing TestNG

  1. Install IntelliJ or Eclipse IDE.
  2. Open IntelliJ and click on a new project.
  3. Give a name, select a language build system, and click Create.

TestNG 1

4. Now search on Google Maven Repository and search for TestNG dependency.

TestNG2

5. Download the file and go to File > project structure > modules > dependencies > plus sign in IntelliJ and select the downloaded file.

image4

6. Click on the checkbox and then apply.

Annotations:

1. @Test: This annotation: his annotation is used to indicate that a method is a test method. TestNG will run the methods annotated with @Test.

@Test public void myTestMethod() { // Test logic goes here } 

2. @BeforeSuite: This annotation indicates that the method will be run before all tests in a suite.

@BeforeSuite public void setUp() { // Setup logic goes here } 

3. @AfterSuite: This annotation indicates that the method will be run after all tests in a suite.

@AfterSuite public void tearDown() { // Teardown logic goes here } 

4. @BeforeClass: This annotation indicates that the method will be run before the first test method in the current class.

@BeforeClass public void beforeClass() { // Before class setup logic goes here } 

5. @AfterClass: This annotation indicates that the method will be run after all the test methods in the current class.

@AfterClass public void afterClass() { // After class teardown logic goes here } 

6. @BeforeTest: This annotation indicates that the method will be run before any test method belonging to the classes inside the <test> tag is run.

@BeforeTest public void beforeTest() { // Before test setup logic goes here } 

7. @AfterTest: This annotation indicates that the method will be run after all the test methods belonging to the classes inside the <test> tag have run.

@AfterTest public void afterTest() { // After test teardown logic goes here } 

These annotations provide a way to control the execution order of your test methods and to set up and tear down resources before and after tests. They are an integral part of the TestNG framework and help in organizing and managing your test suite effectively.

import org.testng.annotations.*;

public class Example {
    @BeforeSuite
    public void beforeSuite(){
        System.out.println(“This is before suite”);
    }
    @BeforeTest
    public void beforeTest(){
        System.out.println(“This is before test”);
    }
    @BeforeClass
    public void beforeClass(){
        System.out.println(“This is before class”);
    }
    @BeforeMethod
    public void beforeMethod(){
        System.out.println(“This is before method”);
    }
    @Test(priority = 1, description = “Test_001_First method description”, enabled = true)
    public void testOne(){
        System.out.println(“This is first test”);
    }
    @Test(priority = 2, description = “Test_002_Second method description”, enabled = true)
    public void testTwo(){
        System.out.println(“This is second test”);
    }
    @AfterMethod
    public void afterMethod(){
        System.out.println(“This is after method”);
    }
    @AfterClass
    public void afterClass(){
        System.out.println(“This is after class”);
    }
    @AfterTest
    public void afterTest(){
        System.out.println(“This is after Test”);
    }
    @AfterSuite
    public void AfterSuite(){
        System.out.println(“This is after suite”);
    }
}

The above class output is given below.

image2

TestNG Assertions

  1. assertEquals: Verifies that two values are equal.

Assert.assertEquals(actual, expected); 

2. assertNotEquals: Verifies that two values are not equal.

Assert.assertNotEquals(actual, expected); 

3. assertTrue: Verifies that the given condition is true.

Assert.assertTrue(condition); 

4. assertFalse: Verifies that the given condition is false.

Assert.assertFalse(condition); 

5. assertNull: Verifies that the given object reference is null.

Assert.assertNull(object); 

6. assertNotNull: Verifies that the given object reference is not null.

Assert.assertNotNull(object); 

7. assertSame: Verifies that two object references refer to the same object.

Assert.assertSame(actual, expected); 

8. assertNotSame: Verifies that two object references do not refer to the same object.

Assert.assertNotSame(actual, expected); 

9. fail: Forces a test to fail with a specified message.

Assert.fail(“Test failure message”); 

These assertions are typically used within test methods annotated with @Test to validate the expected behavior of the code being tested. If an assertion fails, TestNG will mark the test as failed, and the failure details will be reported in the test results.

What is Parameterization in TestNG?

Parameterization in TestNG refers to the ability to run the same test method with different sets of data. This allows you to test a piece of functionality with various inputs or conditions, increasing the coverage and effectiveness of your tests. TestNG provides built-in support for parameterization, making it easy to execute tests with different input values.

Conclusion:

TestNG is a widely used automation framework by testers and developers and is good for reporting and programming. Cross-browser Testing can also be performed. It is better to choose TestNG over Junit.

Related Post

Leave a Comment