Write First Test Case with TestNG

First Test Case Using TestNG: In this post, we are trying to learn how to create our first test case using TestNG. Before going through the application, verify whether TestNG is installed on your local machine, TestNG is installed or not.

If TestNG is not installed, we have written a detailed article about that, and by following that, you can install TestNG on your local machine on your own without taking others’ help.

First Test case with TestNG Guide

If TestNG is already installed on your local machine, then follow the below steps to create TestNG class:

  • You can create a testNG class by pressing the CTRL + N, selecting “TestNG Class” in the TestNG category, and Clicking Next [OR]. You can do the same by right-clicking on the project, Selecting TestNG, and then choosing the Create TestNG class.
  • If you have already set up your project and when you are creating the test cases, then by default, a few things are automatically pre-polluted, like source folder location and package name on the form. Enter a class name in the class name text box, and you will get the different annotations under that. and there you need to select “@BeforeMethod,” “@AfterMethod,” and click Finish.
  • After Clicking on the finish, it will display a newly created testNG class under that package. In that class, you can see three empty methods with three different annotations: @Test, @BeforeMethod & @AfterMethod.
  • Let us Use those three methods and write the script for different purposes, like
    @BeforeMethod: Launch Chrome and open the www.demo.softwaretestingo.com
    @Test: Get The Page Title
    @AfterMethod: Close The Browser
package com.softwaretestingo.testng;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirstTestCase 
{
	WebDriver driver;
	String title;
	
	@Test
	public void launchURL() 
	{
		driver.get("https://demo.softwaretestingo.com/");
		title=driver.getTitle();
		System.out.println("Page Title: "+title);
	}

	@BeforeMethod
	public void beforeMethod() 
	{
		driver=new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
	}

	@AfterMethod
	public void afterMethod() 
	{
		driver.close();
	}
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

2 thoughts on “Write First Test Case with TestNG”

  1. Hi Team,

    Can you provide the project for selenium. Where we know how to do scripts for project.
    And also priority in TestNG.
    Thank You!

    Reply

Leave a Comment