Activity: Creating a TestNG Test

  1. Analyze the GiganticStoreScript class we built in earlier in this chapter:
public class GiganticStoreScript {
public static void main(String[] args) {
checkGiganticStore();
}

private static void checkGiganticStore() {

WebDriver driver = new ChromeDriver();
HomePage homePage = new HomePage(driver);
homePage.open();
ProductsPage productsPage = homePage.goToProducts();

// Verify results in products page
String productsMainInfo = "Here's the stuff we sell";
if (productsMainInfo.equalsIgnoreCase(productsPage.getMainInfo())) {
System.out.println("Products main info is correct!");
} else {
System.out.println("Products main info is NOT correct!");
}
String productsSecondaryInfo = "We have everything... everything";
if (productsSecondaryInfo.equalsIgnoreCase(productsPage.getSecondaryInfo())) {
System.out.println("Products secondary info is correct!");
} else {
System.out.println("Products secondary info is NOT correct!"); }

// Go back home
homePage = productsPage.goToHome();

// Verify results in home page
String homePageMainInfo = "Our best selling product";
if (homePageMainInfo.equalsIgnoreCase(homePage. getMainInfo())) {
System.out.println("HomePage main info is correct!");
} else {
System.out.println("HomePage main info is NOT correct!");
}
String homePageSecondaryInfo = "Buy it before it's too late";
if (homePageSecondaryInfo.equalsIgnoreCase(homePage. getSecondaryInfo())) {
System.out.println("HomePage secondary info is correct!");
} else {
System.out.println("HomePage secondary info is NOT correct!");
}

// Close the page
homePage.close();
}
}
  1. In general, a test script should be concise and not do too many diļ¬€erent things. Therefore, identify sections from the previous automation script that can be extracted as a test. Sections such as the following one are good candidates:
// Verify results in products page
String productsMainInfo = "Here's the stuff we sell";
if (productsMainInfo.equalsIgnoreCase(productsPage.getMainInfo())) {
System.out.println("Products main info is correct!");
} else {
System.out.println("Products main info is NOT correct!"); }
String productsSecondaryInfo = "We have everything... everything";
if (productsSecondaryInfo.equalsIgnoreCase(productsPage.getSecondaryInfo())) {
System.out.println("Products secondary info is correct!");
} else {
System.out.println("Products secondary info is NOT correct!");
}
  1. Create a test method with the section you extracted from the automation script. Don't forget to create the WebDriver object and go to the browser to the desired URL:
@Test
public void verifyProductsPageInformation() {
WebDriver driver = new ChromeDriver();

HomePage homePage = new HomePage(driver);
homePage.open();
ProductsPage productsPage = homePage.goToProducts();

// Verify results in products page
String productsMainInfo = "Here's the stuff we sell";
if (productsMainInfo.equalsIgnoreCase(productsPage. getMainInfo())) {
System.out.println("Products main info is correct!");
} else {
System.out.println("Products main info is NOT correct!"); }
String productsSecondaryInfo = "We have everything... everything";
if (productsSecondaryInfo.equalsIgnoreCase(productsPage. getSecondaryInfo())) {
System.out.println("Products secondary info is correct!");
} else {
System.out.println("Products secondary info is NOT correct!");
}

driver.quit();
}
  1. To avoid code duplication and provide simplicity, use the @BeforeMethod and @AfterMethod annotations to simplify the WebDriver creation and teardown:
package com.beginningselenium.examples.tests;

import com.beginningselenium.examples.pageobjects.HomePage;
import com.beginningselenium.examples.pageobjects.ProductsPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GiganticStoreTest {

private WebDriver driver;

@BeforeMethod
public void setUpWebDriver() {
driver = new ChromeDriver();
}

@AfterMethod(alwaysRun = true)
public void teardownWebDriver() {
if (driver != null) {
driver.close();
}
}

@Test
public void verifyProductsPageInformation() {
HomePage homePage = new HomePage(driver);
homePage.open();
ProductsPage productsPage = homePage.goToProducts();

// Verify results in products page
String productsMainInfo = "Here's the stuff we sell";
if (productsMainInfo.equalsIgnoreCase(productsPage. getMainInfo())) {
System.out.println("Products main info is correct!");
} else {
System.out.println("Products main info is NOT correct!");
}
String productsSecondaryInfo = "We have everything... everything";
if (productsSecondaryInfo.equalsIgnoreCase(productsPage.getSecondaryInfo())) {
System.out.println("Products secondary info is correct!");
} else {
System.out.println("Products secondary info is NOT correct!");
}
}
}
  1. Convert the validations done with "if else" statements to use TestNG assertions:
  package com.beginningselenium.examples.tests;

import com.beginningselenium.examples.pageobjects.HomePage;
import com.beginningselenium.examples.pageobjects.ProductsPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GiganticStoreTest {

private WebDriver driver;

@BeforeMethod
public void setUpWebDriver() {
driver = new ChromeDriver();
}

@AfterMethod(alwaysRun = true)
public void teardownWebDriver() {
if (driver != null) {
driver.close();
}
}

@Test
public void verifyProductsPageInformation() {
HomePage homePage = new HomePage(driver);
homePage.open();
ProductsPage productsPage = homePage.goToProducts();

// Verify results in products page
String expectedProductsMainInfo = "Here's the stuff we sell";
Assert.assertEquals(productsPage.getMainInfo(), expectedProductsMainInfo,
"Product page main info was not the expected one.");
String expectedProductsSecondaryInfo = "We have everything... everything";
Assert.assertEquals(productsPage.getSecondaryInfo(), expectedProductsSecondaryInfo,
"Product page secondary info was not the expected one.");
}
}
  1. Apply the previous steps to the validation done for the home page:
  @Test public void verifyHomePageInformation() { HomePage homePage = new HomePage(driver);
homePage.open(); String expectedHomePageMainInfo = "Our best selling product";
Assert.assertEquals(homePage.getMainInfo(), expectedHomePageMainInfo,
"Home page main info was not the expected one."); String expectedHomePageSecondaryInfo =
"Buy it before it's too late"; Assert.assertEquals(homePage.getSecondaryInfo(),
expectedHomePageSecondaryInfo, "Home page secondary info was not the expected one."); }
  1. Run the tests by clicking on Run, next to the class name. You should see a similar output to the following:
Running the tests
Sample Output
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.16