Testng: The Next Generation Testing Framework
Testng: The Next Generation Testing Framework
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
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
@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
@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;
@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
@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;
@BeforeTest
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
}
@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;
@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;
group_tests.xml
<classes>
<class name="basic_testng.group_tests" />
</classes>
</test>
</suite>
testng.xml
</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”.
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