0% found this document useful (0 votes)
66 views

Testng: The Next Generation Testing Framework

This document discusses TestNG, a testing framework for Java. It provides examples of using TestNG annotations like @BeforeTest, @Test, and @AfterTest to structure Selenium tests. Priority and enabled annotations are used to control test execution order and whether tests run. Dependencies between tests can also be defined using the @Depends annotation.

Uploaded by

Đại Nam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Testng: The Next Generation Testing Framework

This document discusses TestNG, a testing framework for Java. It provides examples of using TestNG annotations like @BeforeTest, @Test, and @AfterTest to structure Selenium tests. Priority and enabled annotations are used to control test execution order and whether tests run. Dependencies between tests can also be defined using the @Depends annotation.

Uploaded by

Đại Nam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

TestNG

The Next Generation Testing Framework


@Sang Bui
2 TestNG - Testing Framework

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Secenario1 {

public static void main(String[] args) throws InterruptedException {


String baseURL = "http://automationpractice.com/index.php";
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
driver.findElement(By.xpath("//*[@title='Contact Us']")).click();
WebElement contactUs = driver.findElement(By.xpath("//*[@class='page-heading bottom-
indent']"));

String expectedTitle1 = "CUSTOMER SERVICE - CONTACT US";


String expectedTitle2 = "My Store";

if (expectedTitle1.equals(contactUs.getText())) {
System.out.println(expectedTitle1 + " is the title for " + contactUs.getText());
} else {
System.out.println(expectedTitle1 + " is not the title for " + contactUs.getText());
}

Thread.sleep(5000);
driver.navigate().back();

if (expectedTitle2.equals(driver.getTitle())) {
System.out.println(expectedTitle2 + " is the title for " + driver.getTitle());
} else {
System.out.println(expectedTitle2 + " is not the title for " + driver.getTitle());
}

Thread.sleep(3000);
driver.quit();
}
}

Results
3 TestNG - Testing Framework

public class Secenario_Custom {

private WebDriver driver;


String baseURL = "http://automationpractice.com/index.php";

@BeforeTest
public void launchBrowser() {
// Set the Chrome driver path
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
// Load the website URL
driver.get(baseURL);
}

@Test
public void verifyContactFormTitle() {
// Click on the Contact Us button at the top page
driver.findElement(By.xpath("//*[@title='Contact Us']")).click();
// Verify the Contact form title
String expectedContactFormTitle = "CUSTOMER SERVICE - CONTACT US";
String actualContactFormTitle =
driver.findElement(By.xpath("//h1[@class='page-heading bottom-
indent']")).getText();

if (expectedContactFormTitle.equals(actualContactFormTitle)) {
System.out.println("The Contact form title is correct: " +
actualContactFormTitle);
} else {
System.out.println("The Contact form title expected is "
+ expectedContactFormTitle + " but found: " + actualContactFormTitle);
}
// Assert the test results
Assert.assertEquals(actualContactFormTitle, expectedContactFormTitle);
}

@Test
public void verifyHomeTitle() throws InterruptedException {
// Wait 5s for fully loaded
Thread.sleep(5000);
// Navigate back to the home page
driver.navigate().back();
// Verify the Homepage title
String expectedHomeTitle = "~ My Store Online ~";
String actualHomeTitle = driver.getTitle();

if (expectedHomeTitle.equals(actualHomeTitle)) {
System.out.println("The Homepage title is correct: " + actualHomeTitle);
} else {
System.out.println("The Homepage title expected is " + expectedHomeTitle
+ " but found: " + actualHomeTitle);
}
// Assert the test results
Assert.assertEquals(actualHomeTitle, expectedHomeTitle);
}

@AfterTest
public void closeBrowser() {
driver.quit();
}
}
4 TestNG - Testing Framework

public class Secenario_Custom {

private WebDriver driver;


String baseURL = "http://automationpractice.com/index.php";

@BeforeTest
public void launchBrowser() {
// Set the Chrome driver path
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
// Load the website URL
driver.get(baseURL);
}

@Test
public void verifyContactFormTitle() {
// Click on the Contact Us button at the top page
driver.findElement(By.xpath("//*[@title='Contact Us']")).click();
// Verify the Contact form title
String expectedContactFormTitle = "CUSTOMER SERVICE - CONTACT US";
String actualContactFormTitle =
driver.findElement(By.xpath("//h1[@class='page-heading bottom-
indent']")).getText();

if (expectedContactFormTitle.equals(actualContactFormTitle)) {
System.out.println("The Contact form title is correct: " +
actualContactFormTitle);
} else {
System.out.println("The Contact form title expected is "
+ expectedContactFormTitle + " but found: " + actualContactFormTitle);
}
// Assert the test results
Assert.assertEquals(actualContactFormTitle, expectedContactFormTitle);
}

@Test
public void verifyHomeTitle() throws InterruptedException {
// Wait 5s for fully loaded
Thread.sleep(5000);
// Navigate back to the home page
driver.navigate().back();
// Verify the Homepage title
String expectedHomeTitle = "~ My Store Online ~";
String actualHomeTitle = driver.getTitle();

if (expectedHomeTitle.equals(actualHomeTitle)) {
System.out.println("The Homepage title is correct: " + actualHomeTitle);
} else {
System.out.println("The Homepage title expected is " + expectedHomeTitle
+ " but found: " + actualHomeTitle);
}
// Assert the test results
Assert.assertEquals(actualHomeTitle, expectedHomeTitle);
}

@AfterTest
public void closeBrowser() {
driver.quit();
}
}
5 TestNG - Testing Framework
6 TestNG - Testing Framework

1. Priority tests
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class priority_tests {

private WebDriver driver;

@BeforeTest
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://vnpt.vn/en");
}

@Test(priority = 1)
public void verifyHomepageTitle() {
String expectedTitle = "VNPT > Home";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actualTitle);
System.out.println("The title is " + actualTitle);
}

@Test(priority = 0)
public void verifyURL() {
String expectedURL = "http://vnpt.vn/Default.aspx?alias=vnpt.vn/en";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(expectedURL, actualURL);
System.out.println("The URL is " + actualURL);
}

@AfterTest
public void closeBrowser() {
driver.quit();
}
}
7 TestNG - Testing Framework

2. Enable / Disable tests


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class enabled_tests {

private WebDriver driver;

@BeforeTest
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://vnpt.vn/en");
}

@Test(enabled = true)
public void verifyHomepageTitle() {
String expectedTitle = "VNPT > Home";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actualTitle);
System.out.println("The title is " + actualTitle);
}

@Test(enabled = false)
public void verifyURL() {
String expectedURL = "http://vnpt.vn/Default.aspx?alias=vnpt.vn/en";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(expectedURL, actualURL);
System.out.println("The URL is " + actualURL);
}

@AfterTest
public void closeBrowser() {
driver.quit();
}
}
8 TestNG - Testing Framework

3. Depends tests

package basic_testng;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class dependency_tests {

private WebDriver driver;


private String URL = "http://vnpt.vn/en";

@BeforeTest
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
}

@Test (dependsOnMethods = "verifyURL")


public void verifyHomepageTitle() {
String expectedTitle = "VNPT > Home";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
System.out.println("The title is " + actualTitle);
}

@Test (dependsOnMethods = "loadURL")


public void verifyURL() {
String expectedURL = "http://vnpt.vn/Default.aspx?alias=vnpt.vn/en";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
System.out.println("The URL is " + actualURL);
}

@Test
public void loadURL() {
driver.get(URL);
}

@AfterTest
public void closeBrowser() {
driver.quit();
}
}
9 TestNG - Testing Framework

4. Data Provider

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class dataprovider_tests {

@Test(dataProvider="getData")
public void verifyData(int ID, String Name) {
System.out.println("ID is " + ID);
System.out.println("The name is " + Name);
}

@DataProvider
public Object[][] getData()
{
Object[][] data = new Object[3][2];

// 1st row
data[0][0] = 001;
data[0][1] = "A";
// 2nd row
data[1][0] = 002;
data[1][1] = "B";
// 3rd row
data[2][0] = 003;
data[2][1] = "C";
return data;
}
}
10 TestNG - Testing Framework

5. Annotations
11 TestNG - Testing Framework

import org.testng.annotations.*;
public class annotations_tests {

@Test
public void testCase1() {
System.out.println("@Test: Test Case 1");
}

@Test
public void testCase2() {
System.out.println("@Test: Test Case 2");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("* Before (Every) @Test method");
}

@AfterMethod
public void afterMethod() {
System.out.println("* After (Every) @Test method");
}

@BeforeClass
public void beforeClass() {
System.out.println("Before the Class");
}

@AfterClass
public void afterClass() {
System.out.println("After the Class");
}

@BeforeTest
public void beforeTest() {
System.out.println("Before the Test");
}

@AfterTest
public void afterTest() {
System.out.println("After the Test");
}

@BeforeSuite
public void beforeSuite() {
System.out.println("Before Test Suite...!");
}

@AfterSuite
public void afterSuite() {
System.out.println("After Test Suite...!");
}
}
12 TestNG - Testing Framework
13 TestNG - Testing Framework

6. Group tests

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class group_tests {

private WebDriver driver;

@BeforeTest (alwaysRun = true)


public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://vnpt.vn/en");
}

@Test(groups = "title", priority = 4)


public void verifyHomepageTitle() {
String expectedTitle = "VNPT > Home";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actualTitle);
System.out.println("The title is " + actualTitle);
}

@Test(groups = "title", priority = 2)


public void verifyTitleLength() {
String actualTitle = driver.getTitle();
int TitleLength = actualTitle.length();
System.out.println("The title length: " + TitleLength);
}

@Test(groups = "URL", priority = 1)


public void verifyURLLength() {
String actualURL = driver.getCurrentUrl();
int URLLength = actualURL.length();
System.out.println("The URL length: " + URLLength);
}

@Test(groups = "URL", priority = 3)


public void verifyURL() {
String expectedURL = "http://vnpt.vn/Default.aspx?alias=vnpt.vn/en";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(expectedURL, actualURL);
System.out.println("The URL is " + actualURL);
}

@AfterTest (alwaysRun = true)


public void closeBrowser() {
driver.quit();
}
}
14 TestNG - Testing Framework

group_tests.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Group">
<groups>
<run>
<include name="URL" />
</run>
</groups>

<classes>
<class name="basic_testng.group_tests" />
</classes>

</test>
</suite>

7. Run test suite

testng.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="basic_testng.dataprovider_tests"/>
<class name="basic_testng.priority_tests"/>
<class name="basic_testng.enabled_tests"/>
<class name="basic_testng.Secenario_Custom"/>
<class name="basic_testng.dependency_tests"/>
<class name="basic_testng.annotations_tests"/>

</classes>
</test>
</suite>
15 TestNG - Testing Framework
16 TestNG - Testing Framework

Project Structure
17 TestNG - Testing Framework
18 TestNG - Testing Framework
19 TestNG - Testing Framework

Project Structure
(POM: Page Object Model)
20 TestNG - Testing Framework
21 TestNG - Testing Framework

TestNG setup:
1) Launch the Eclipse IDE and from Help menu, click “Install New Software”.

2) You will see a dialog window, click “Add” button.


22 TestNG - Testing Framework

3) Type name as you wish, lets take “TestNG” and type


“http://beust.com/eclipse/” as location. Click OK.

4) You come back to the previous window but this time you must see TestNG
option in the available software list. Just Click TestNG and press “Next”
button.
23 TestNG - Testing Framework

5) Click “I accept the terms of the license agreement” then click Finish.

6) You may or may not encounter a Security warning, if in case you do just
click OK.

7) Click Next again on the succeeding dialog box until it prompts you to Restart
the Eclipse.
8) You are all done now, just Click Yes.
24 TestNG - Testing Framework

9) Proceed with your workplace.


10) After restart, verify if TestNG was indeed successfully installed. Right click on
you project and see if TestNG is displayed in the opened menu.
25 TestNG - Testing Framework

11) New TestNG project:


To create the new TestNG project, click on Menu, select New > Java Project.
Right click on the project, select Properties > Java Build Path > Libraries
Click on Add Library button at the right side and select “TestNG” from the list,
Click on Next & OK to finish.

You might also like