Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Writing First Program with TestNG - Testingpool

Writing First Program with TestNG

In the previous post, we have learnt about TestNG annotations. In this Post, we will be writing First Program with TestNG.

Prerequisite: TestNG should be installed with Eclipse as shown in this post.

Let’s understand this step by step.

TestNG Program

Step#1 In Eclipse, go to the your package –> TestNG –> Create TestNG Class.

Create TestNG class

 

Step#2 Next window will come with annotations where you can select required annotations. We have selected @BeforeMethod, @AfterMethod and given a class name as “FirstTest”.

Select annotations in testNG

 

Step#3 Click Finish. The class will be created with the selected annotation. Look at the code below. One good point to observe here is that, there is one annotation @Test, which is added automatically with the method name as f().

public class FirstTest {
  @Test
  public void f() {
  }
  @BeforeMethod
  public void beforeMethod() {
  }

  @AfterMethod
  public void afterMethod() {
  }

}

You can change the method names according to your requirements.

Step#4 We have changed the method names as given below. These method will perform according to the annotations set for them. As an example @BeforeMethod will be executed before all the methods in the class. @AfterMethod will be executed after all the methods in the class. @Test will be executed as part of test case.

For better understanding, we have given simple print statement. So, It will be easy for you to understand their working.

public class FirstTest {
	
  @Test
  public void driveCar() {
	  System.out.println("Drive the car");
  }
  
  @BeforeMethod
  public void startCar() {
	  System.out.println("Start the car");
  }

  @AfterMethod
  public void stopCar() {
	  System.out.println("Stop the car");
  }

}

How to run TestNg class?

Right Click on the Class –> Run As –> TestNG Test.

Run As TestNG

Output:

Start the car
Drive the car
Stop the car

As you can observe the output, first @BeforeMethod is executed, then @Test method, and in last @AfterMethod. So, in this way you can use the annotations.


Now, let’s see some practical implementation with an example. We will take Gmail Application as our demo AUT and perform Login functionality.

  • @BeforeMethod: Method will contain driver initialization code.
  • @AfterMethod: Clean up code like closing the driver after execution.
  • @Test: Gmail Login code.

Gmail login

Program for Gmail Login using TestNG:

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

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.AfterMethod;

public class FirstTest {
	public WebDriver driver;
	
  @Test
  public void Login() {
	  System.out.println("Login into Gmail");
	  //Enter username
	  driver.findElement(By.id("Email")).sendKeys("sharmashekhar86@gmail.com");
	  //Click Next button
	  driver.findElement(By.id("next")).click();
	  //Enter password
	  driver.findElement(By.id("Passwd")).sendKeys("Pas@123");
	  //Click SignIN
	  driver.findElement(By.id("signIn")).click();
  }
  
  @BeforeMethod
  public void startCar() {
	  System.out.println("Intializing FirefoxDriver");
	  driver = new FirefoxDriver();
	  //setting 30 seconds implicit wait
	  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  driver.manage().window().maximize();
	  //Open Gmail 
	  driver.get("https://accounts.google.com/");	  
  }

  @AfterMethod
  public void stopCar() {
	  //Close the browser
	  System.out.println("Close the browser");
	  driver.quit();
  }

}

Output: 

Intializing FirefoxDriver
Login into Gmail
Close the browser
PASSED: Login
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0 ===============================================

Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...