TestNG is an open-source test automation framework for Java. You can perform various kinds of tests such as unit test, end to end test, integration test, etc. using TestNG.
Here are some steps to create a TestNG test script:
- Create Java Project and name it with SampleTestNG
2. Add some libraries:
a. Add TestNG Library, with step: Click Right on Your Project then Choose Build Path → Choose Add Libraries…
b. Add Selenium Library, with step: Click Right on Your Project then Choose Build Path → Choose Configure Build Path → choose Libraries → choose Add External JARs… → click Apply and Close
- Selenium JAR file (https://www.selenium.dev/downloads/)
- Selenium Standalone server (https://www.selenium.dev/downloads/)
3. Add Package com.first.sample under src folder
4. Create TestNG test script, with step: in com.first.sample package → right click then choose New → choose Other.. → choose TestNG Class → create Class name “search” → click Finish
5. Write test search.java like below.
//search.java
package com.first.sample;import org.testng.Assert;import org.testng.annotations.Test;import org.openqa.selenium.By;import org.openqa.selenium.Keys;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;public class search {public String baseUrl = "https://google.com";String driverPath = "C:\\webdriver\\chromedriver.exe";public WebDriver driver ;@Testpublic void searching() {// launch the chrome browser and open baseUrlSystem.out.println("launching chrome browser");System.setProperty("webdriver.chrome.driver", driverPath);driver = new ChromeDriver();driver.get(baseUrl);// maximize the browser windowdriver.manage().window().maximize();// verify the web titleString expectedTitle = "Google";String actualTitle = driver.getTitle();Assert.assertEquals(actualTitle, expectedTitle);// search cheesedriver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);// close the web browserdriver.close();}}
6. Run Test, with step: Run As → TestNG Test
After running a TestNG project, a test-output folder automatically created in the project structure.
For test report, you can open index.html in test-ouput folder
7. Run All test
To run all your test scripts, you can create testng.xml file in your project, with step: select your project then right click → choose New → choose File and name it with testng.xml
You can runall test scripts based on package like below
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="example suite" verbose="1" ><test name="Regression suite" ><packages><package name="com.first.sample" /></packages></test></suite>
or you can runall test scripts based on class name like below.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" ><suite name="example suite" verbose="1" ><test name="Regression suite" ><classes><class name="com.first.example.search"/><class name="com.first.example.regexTest"/></classes></test></suite>
Note! Eclipse versions for TestNG: 2018–09 (4.9), Photon (4.8), Oxygen (4.7), Neon (4.6), Mars (4.5), Luna (4.4), Kepler (4.3), Juno (4.2, 3.8), Previous to Juno (<=4.1)
Thanks for reading.