testng code example

Example 1: what is testng

TESTNG: TestNextGeneration
    -> What is TestNG?
        - TestNG is a UNIT TESTING TOOL.
        - It was inspired by JUNIT, but a little bit more advanced than JUNIT.
        - TESTNG helps us create certain testing flow/structure using 
        its ANNOTATIONS.
-->  some of the annotation from TestNG are
    @Test
    @BeforeSuite
    @AfterSuite
    @BeforeTest
    @AfterTest
    @BeforeGroups
    @AfterGroups
    @BeforeClass
    @AfterClass
    @BeforeMethod
    @AfterMethod

Example 2: TestNG

TESTNG: TestNextGeneration
TestNG is a UNIT TESTING TOOL.
It was inspired by JUNIT, 
but a little bit more advanced than JUNIT.
TESTNG helps us create certain testing structure 
using its ANNOTATIONS.
@Test @BeforeSuite @AfterSuite@BeforeTest
@AfterTest @BeforeGroups @AfterGroups
@BeforeClass @AfterClass @BeforeMethod
@AfterMethod
JUnit - the most popular unit testing framework for Java.
TestNG - was created as improved alternative for JUnit.
Both of them, targeting unit testing.

Example 3: testng

package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class firsttestngfile {
    public String baseUrl = "http://demo.guru99.com/test/newtours/";
    String driverPath = "C:\\geckodriver.exe";
    public WebDriver driver ; 
     
  @Test
  public void verifyHomepageTitle() {
       
      System.out.println("launching firefox browser"); 
      System.setProperty("webdriver.gecko.driver", driverPath);
      driver = new FirefoxDriver();
      driver.get(baseUrl);
      String expectedTitle = "Welcome: Mercury Tours";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
      driver.close();
  }
}

Example 4: testng

package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class firsttestngfile {
    public String baseUrl = "http://demo.guru99.com/test/newtours/";
    String driverPath = "C:\\geckodriver.exe";
    public WebDriver driver ; 
     
  @Test
  public void verifyHomepageTitle() {
       
      System.out.println("launching firefox browser"); 
      System.setProperty("webdriver.gecko.driver", driverPath);
      driver = new FirefoxDriver();
      driver.get(baseUrl);
      String expectedTitle = "Welcome: Mercury Tours";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
      driver.close();
  }
}

Tags:

Java Example