TestNG Annotations for Efficient Selenium Testing

TestNG Annotations for Efficient Selenium Testing

In modern software development, testing is a critical component that ensures the stability and reliability of applications. Selenium WebDriver is a commonly used tool for automating web browsers, allowing for repeatable and reliable web application testing. To improve the management and execution of test cases, TestNG—a testing framework inspired by JUnit and NUnit—offers annotations that provide powerful test case structuring and execution control capabilities.

Core TestNG Annotations

Here, we'll explore the core TestNG annotations with a practical example in the context of Selenium WebDriver tests.

  1. @BeforeSuite: Executes code before a suite of tests begins. Useful for setting up test data or configurations.
  2. @BeforeTest: Runs before any tests within a <test> tag in the TestNG XML file.
  3. @BeforeClass: Executed before the first method of the current class is invoked. It's often used to perform setup activities, such as launching a browser in Selenium.
  4. @BeforeMethod: Runs before each test method. Ideal for preparing test preconditions or refreshing a web page.
  5. @Test: The core annotation that signifies a method as a test case.
  6. @AfterMethod: Invoked after each test method to perform cleanup, such as logging or taking screenshots of failed tests.
  7. @AfterClass: Runs after all test methods in the current class are executed. It could be used to close a browser or write logs.
  8. @AfterTest: Executed after all the tests inside a <test> tag have run.
  9. @AfterSuite: Signifies the end of an entire suite of tests, suitable for teardown processes like disconnecting from databases.

Example with Selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.testng.annotations.*;

public class LoginTest {
    
    private WebDriver driver;

    // Setting up the WebDriver and opening the browser before running any tests
    @BeforeClass
    public void setUp() {
        // Set the path for the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        // Initialize a new instance of ChromeDriver
        driver = new ChromeDriver();
        // Navigate to the login page
        driver.get("https://example.com/login");
    }
    
    // Navigating to the login page before each test method
    @BeforeMethod
    public void navigateToLoginPage() {
        // Clicking on the home button to navigate to the login page
        driver.findElement(By.id("home")).click();
    }

    // Test case for successful login
    @Test
    public void successfulLoginTest() {
        // Entering valid username
        driver.findElement(By.id("username")).sendKeys("testuser");
        // Entering valid password
        driver.findElement(By.id("password")).sendKeys("testpass");
        // Clicking on the login button
        driver.findElement(By.id("login")).click();
        // Verifying if logout button is displayed after successful login
        Assert.assertTrue(driver.findElement(By.id("logout")).isDisplayed());
    }

    // Test case for failed login
    @Test
    public void failedLoginTest() {
        // Entering invalid username
        driver.findElement(By.id("username")).sendKeys("wronguser");
        // Entering invalid password
        driver.findElement(By.id("password")).sendKeys("wrongpass");
        // Clicking on the login button
        driver.findElement(By.id("login")).click();
        // Verifying if login error message is displayed after failed login
        Assert.assertTrue(driver.findElement(By.id("login-error")).isDisplayed());
    }

    // Cleaning up after each test method by deleting cookies
    @AfterMethod
    public void cleanUp() {
        driver.manage().deleteAllCookies();
    }

    // Quitting the WebDriver and closing the browser after all tests are executed
    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}        

By harnessing the power of TestNG annotations, Selenium testers can structure their tests efficiently, control the flow of test execution, and maintain readability. TestNG's flexibility allows for robust testing patterns, significantly improving test maintenance and scalability.

Sriyanka Patnaik

Client Partner

11 个月

While the emphasis on accuracy and efficiency is admirable, we at #TestEnsure strive beyond and beyond. To enhance your particular business procedures and advance your objectives, we'll test beyond functional needs. Utilize our in-depth knowledge of Infor and all other leading ERP technology providers to streamline complicated systems and synchronize across silos, teams, and regions: https://sailotech.com/test-automation.html #innovation #software #testing #softwaretesting #testautomation #automation #sailotech

要查看或添加评论,请登录

Azher Iqbal的更多文章

社区洞察

其他会员也浏览了