First TestCase in TestNG – User friendly Tech help
We are already done with the introduction and installation of TestNG, today we are writing our first test case in TestNG.
n
nScenario:-How to execute our First Test Case using TestNG
n
nSolution:-
nStep 1:-Adding TestNG class
nA:-Press CTRL+N ->TestNG->Create TestNG Class
n
![]() |
Creating TestNG Class |
n
nB:-Set Class Name and select Annotations ->Finish
n
![]() |
Class name + Annotations |
n
Step 2:-Adding the code to the new Class
nA. @BeforeMethod:-Launch Browser and its settings.
nB. @Test:- Our Login Test Case
nC. @AfterMethod:-Quit Browser
n
![]() |
Default view of TestNG Class with Annotations |
n
Code:-
n
n
package srcTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class FirstTest {
private WebDriver driver; @Test
public void login() {
//Fill the UserName and Password fields
driver.findElement(By.id("userName")).sendKeys("uftHelp");
driver.findElement(By.id("password")).sendKeys("Password");
//Click the Sign in Button
driver.findElement(By.id("SignIn")).click();
//Clicking on the alert message driver.switchTo().alert().accept(); } @BeforeMethod
public void beforeMethod() {
driver = new FirefoxDriver();
//Adding Implicit wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize browser driver.manage().window().maximize();
//Open the Login Application
driver.get("http://www.ufthelp.com/p/testpage.html"); } @AfterMethod
public void afterMethod() {
//Closing the browser driver.quit(); }}
n
Step 3:-Running the Code
nRightClick on the TestCase ->Run As -> TestNG Test
nor
nCTRL+F11
n
nStep 4:-Observing the Run Results
nEclipse will have results in Console window, while TestNG result will be shown separately in “Results of Running class TestName”.
n
![]() |
Run Results of TestNG |
n
nStep 5:-Checking the HTML Reports
nMethod1:-We can go to the Project Directory ->test-output folder
n
![]() |
Open the test-output folder inside the Project Directory |
n
How to locate the Project Directory?
nRight click in the solution explorer ->Properties
n
n
![]() |
Path of project directory |
n
Method2:-In the solution explorer ->test-output->index.html (it will show the latest run results)
n
![]() |
index.html file in test-output folder in solution explorer |
n
![]() |
Opening the index.html file |
n
![]() |
||
Test Results – TestNGn
We can open the emailable-report.html from test-output folders. n
n |
n
Data Parameterization in TestNG