First TestCase in TestNG

We are already done with the introduction and installation of TestNG, today we are writing our first test case in TestNG.

Scenario:-How to execute our First Test Case using TestNG


Solution:-

Step 1:-Adding TestNG class
A:-Press CTRL+N ->TestNG->Create TestNG Class

Creating TestNG Class
Creating TestNG Class

B:-Set Class Name and select Annotations ->Finish


Selecting Annotation while creating TestNG Class
Class name + Annotations
Step 2:-Adding the code to the new Class
A. @BeforeMethod:-Launch Browser and its settings.
B. @Test:- Our Login Test Case
C. @AfterMethod:-Quit Browser



View of TestNG class with Annotations
Default view of TestNG Class with Annotations
Code:-



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();
   }

}

Step 3:-Running the Code

RightClick on the TestCase ->Run As -> TestNG Test
or
CTRL+F11

Step 4:-Observing the Run Results

Eclipse will have results in Console window, while TestNG result will be shown separately in "Results of Running class TestName".

Result of running class using TestNG
Run Results of TestNG

Step 5:-Checking the HTML Reports

Method1:-We can go to the Project Directory ->test-output folder 



test-output folder in TestNG
Open the test-output folder inside the Project Directory
How to locate the Project Directory? 
Right click in the solution explorer ->Properties



Project directory path in eclipse
Path of project directory 
Method2:-In the solution explorer ->test-output->index.html (it will show the latest run results)




index.html file in test-output folder in eclipse
index.html file in test-output folder in solution explorer
Open index.html in browser
Opening the index.html file

Test results in TestNG
Test Results - TestNG



We can open the emailable-report.html from test-output folders.

Emailable view of the test results in TestNG
Emailable view of the results in TestNG