TestNG Annotations for Efficient Selenium Testing
Azher Iqbal
Sr. SQAE | Automation Specialist | Java | .NET | Selenium WebDriver | Cypress | BDD | PlayWritght | API Testing | Azure DevOps | Appium
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.
领英推荐
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.
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