How to do TestNG test suite with Hello World test case – step by step explanation given below.

Prerequisite: Eclipse + Maven + Selenium + TestNG Setup
If you already have necessary software setup, jump to step 1. Else refer this link for details about software setup;
https://digitalappconsultancy.com/site/eclipse-selenium-java-testng-project-setup/

Software used are listed below;

Eclipse 4.6.x
JDK 1.8.x
Selenium Java WebDriver 3.4.0
Chrome Driver 2.29
TestNG 6.10

Finally the project should look as below;


 
Step 1: Create TestNG Suite xml

Create new file TestNGRegressionSuite.xml under src/main/resources folder.

TestNGRegressionSuite.xml provided below

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 
<suite name="RegressionSuite" verbose="1" >
  <test name="RegressionTestCases"   >
    <packages>
      <package name="com.regression" />
   </packages>
 </test>
</suite>

 
Step 2: Create TestNG ProductSearchTest.java test class

Create new TestNG test class and name it as ProductSearchTest.java. In this class setup the system property webdriver.chrome.driver value. Also open the home page of amazon.com website.

Create new test method called testProductSearch(). Find Search text box and enter iPhone as search key word.

Complete source code of class ProductSearchTest.java provided below
 

package com.regression;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

import static org.testng.Assert.assertNotNull;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;

public class ProductSearchTest {

  ChromeDriver driver = null;
    
  @BeforeClass
  public void beforeClass() {
      // Replace with chrome driver absolute path
      System.setProperty("webdriver.chrome.driver", "/Users/Niranjan/My Briefcase/My Development/eclipse_learning/Selenium/TestingProject/src/main/resources/webdrivers/chromedriver");
      
      driver = new ChromeDriver();
      driver.get("http://amazon.com");
  }

  @Test
  public void testProductSearch(){
      WebElement searchElement = driver.findElementById("twotabsearchtextbox");
      searchElement.sendKeys("iPhone");
      
      driver.findElementByClassName("nav-input").click();
      
      // Looking for search results page
      WebElement searchResults = driver.findElementByLinkText("Cell Phones & Accessories");
      
      // Verify search results page opened and the web element is Not null
      assertNotNull(searchResults);
  }
  
  
  @AfterClass
  public void afterClass() {
      driver.quit();
  }

}

 
Step 3: Execute TestNG suite xml

Now you can right click on suite xml (TestNGRegressionSuite.xml) and run as – TestNG Suite.

Refer YouTube tutorial with detailed explanation

Finally the project should look as below;

For further reading refer Java Selenium Programming Category posts.

Share this post

How to write TestNG Product Search selenium test case
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *