Automating UI Test Cases with TestNG Attributes

Now that TestNG is added to the project, it can be used to manage and execute test scripts more effectively.

For this exercise, take around 2 to 5 manual test cases and automate them using Selenium WebDriver without using the Page Object Model. Which means, write the script in one Java class, use TestNG Annotations. 

In TestNG, each test case is represented by a method annotated with @Test. To automate your manual test cases:

     1. Create a Java class.
     2. Use the @BeforeClass annotation to initialize the WebDriver before any test runs.
     3. Use the @AfterClass annotation to quit the WebDriver after all tests have run.
     4. Define each test case using the @Test annotation.

You can run the tests by executing the Java class directly or through a TestNG suite

Example Code: 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;
public class TestNGTest {
    WebDriver driver;
    WebDriverWait wait;
    // This method runs once before all test methods in the class
    @BeforeClass
    public void setUp() {
        // Set the system property for ChromeDriver
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        // Launch Chrome browser
        driver = new ChromeDriver();
        // Create an explicit wait with 10 seconds timeout
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        // Optional: Maximize browser window
        driver.manage().window().maximize();
    }
    // This is a test case for entering text into a textbox
    @Test(priority = 1)
    public void textboxInputTest() {
        // Navigate to the website
        driver.get("https://www.qafeast.com/");
        // Click the cookie accept button
        driver.findElement(By.id("gdpr-cookie-accept")).click();
        // Click the "Tools" menu item
        driver.findElement(By.cssSelector("li[class='tut_ fr_tls']")).click();
        // Wait until the "Demo Site" link becomes visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Demo Site")));
        // Click the "Demo Site" link
        driver.findElement(By.linkText("Demo Site")).click();
        // Wait until the "Textbox" header becomes visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("//h2[text()='Textbox']")));
        // Type "Hello World" into the editable textbox
        driver.findElement(By.id("editabletext")).sendKeys("Hello World");
    }
    // This is a test case for clicking a submit button and verifying the result
    @Test(priority = 2)
    public void submitButtonTest() {
        // Wait until the "Button" label becomes visible and click it
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[text()='Button']")));
        driver.findElement(By.xpath("//label[text()='Button']")).click();
        // Wait until the "Button" header becomes visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[text()='Button']")));
        // Click the submit button
        driver.findElement(By.cssSelector("[title='Click to submit the button']")).click();
        // Verify the success message is displayed
        String actualText = driver.findElement(By.id("msgbutton-1")).getText();
        Assert.assertEquals(actualText, "Submit button is clicked");
    }

// This method runs once after all test methods have finished
    @AfterClass
    public void tearDown() {
        // Close the browser if it is open
        if (driver != null) {
            driver.quit();
        }
    }
}

 

Related Tutorials

Related Questions






Read more