
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why to Use Selenium with TestNG
TestNG is a powerful testing framework, an enhanced version of JUnit which was in use for a long time before TestNG came into existence. NG stands for 'Next Generation'.
TestNG framework provides the following features ?
Annotations help us organize the tests easily.
Flexible test configuration.
Multiple test cases can be grouped more easily.
Parallelization of tests can be achieved using TestNG.
Support for data? driven testing.
Inbuilt reporting as well as supported by Extent Reporting and other reporting tools. Selenium by default doesn't generate any report.
Easily configured for Cross Browser testing.
TestNG framework can easily integrate with Maven, Jenkins, Gradle, Ant or any other build tool as well as CI/CD tool.
TestNG handles some uncaught exceptions as well. Whenever these exceptions occur TestNG fails the test step and the same can be verified in report as well.
All TestNG annotations can be used in Selenium. Annotations are easy to read and understand.
Selenium allows to interact with webpages. It is an interface not a testing framework. To run any test or code only in selenium we must use java main method. TestNG provides us a framework that runs selenium code without using java main method. Apart from this, better code maintainability, reporting, flexible test configurations are additional advantages of using TestNG along with Selenium.
Approach/Algorithm to Solve this Problem
Step 1: Make sure Selenium, TestNG and initial set up of Firefox driver is set up in system properly.
Step 2: Create a TestNG class and write selenium code as mentioned in program code.
Step 3: Run the TestNGClass file.
Step 4: Write the same code without TestNG and compare the differences.
Example
The following code to create a TestNG class with Selenium code:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestNGClass { WebDriver driver = new FirefoxDriver(); @BeforeTest public void launchApp() { // Puts an Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch website driver.navigate().to("http://www.calculator.net"); driver.manage().window().maximize(); } @Test public void calculatePercent() { // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Print a Log In message to the screen System.out.println(" The Result is " + result); if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void terminatetest() { driver.close(); } }
Output
[TestNG] Running: C://Users/************** The Result is 5 The Result is Pass PASSED: calulatePercent =============================================== Suite1 Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 ===============================================
Selenium Code without TestNG
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class SeleniumWithoutTestNG { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); // Puts an Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch website driver.navigate().to("http://www.calculator.net"); driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent //Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent //Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Print a Log In message to the screen System.out.println(" The Result is " + result); if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } driver.close(); } }