Software Testing Learning Hub

Selenium Lesson 21 – Creating First TestNG-Selenium Script

In last lesson we have seen overview and capabilities of TestNG. now we will actually use them and create working TestNG Test Script with Selenium.

you can also watch video posted earlier for better understanding.

 

Prerequisites : you should have Project created in Eclipse and TestNG plugin should be installed

Step1: Create TestNG class(Test case)

select the package in project explorer >Right click on it > New > Other

testng-step1

 

Step 2: Select TestNG Class

Select Test NG class from the list and Click on Next Button

testng-step2

 

Step3: Configure the TestNG Test Case

Enter Class Name, Select BeforeTest and AfterTest Annotations. and finally click on the Finish button.

Class Name:  it is test case name.

BeforeTest :  this annotation used with method and this method will be executed before each test method in your TestNG Class. lets say if you have 10 test methods ( test cases) in same class then BeforeTest Method will be executed 10 times before test method.

AfterTest: this annotation used with method and this method will be executed after each test method in your TestNG Class. lets say if you have 10 test methods ( test cases) in same class then AfterTest Method will be executed 10 times after test method.

testng-step3

 

you have successfully created the testng test case.

package TestNG;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class FirstTestNGClass {
 @Test
 public void f() {
 }
 @BeforeTest
 public void beforeTest() {
 }
 @AfterTest
 public void afterTest() {
 }
}

Now lets write Selenium code. We will click on link on Qeworks.com.

in beforeTest() we need to add code to invoke the firefox browser and launch Qeworks.com. method will look like this.

@BeforeTest
public void beforeTest() {
driver = new FirefoxDriver(); // create new instance of firefox driver
baseUrl = "http://qeworks.com/"; // define baseUrl variable
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // manage timeout
}

in Test() method we will add code to launch Qeworks and click on one of Selenium menu link.

@Test
public void f() {
driver.get(baseUrl + "/");
driver.findElement(By.Xpath("//*[@id='menu-item-5014']/a")).click();
}

in aftertest(), now we have close the opened firefox driver.

@AfterTest
public void afterTest() {
driver.quit();
}

So after including all variables at class level and combine all above methods, our code will look this this.

package TestNG;
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.BeforeTest;
import org.testng.annotations.AfterTest;

public class FirstTestNGClass {
 public static WebDriver driver;
 public static String baseUrl;
 @Test
 public void f() {
 driver.get(baseUrl + "/");
 driver.findElement(By.xpath("//*[@id='menu-item-5014']/a")).click();
 }
 @BeforeTest
 public void beforeTest() {
 driver = new FirefoxDriver(); // create new instance of firefox driver
 baseUrl = "http://qeworks.com/"; // define baseUrl variable
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // manage timeout
 }@AfterTest
 public void afterTest() {
 driver.quit();
 }

}