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(); } }
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!
You can search In GitHub where you can get some good projects