In this section, we will explore how to create test cases using TestNG, a popular testing framework for Java that is widely used in conjunction with Selenium. TestNG provides powerful features such as annotations, grouping, and parallel execution, which make it an excellent choice for managing and executing test cases.

Key Concepts

  1. TestNG Annotations: TestNG uses annotations to control the flow of test execution. Some of the most commonly used annotations include:

    • @Test: Marks a method as a test case.
    • @BeforeMethod: Executes before each test method.
    • @AfterMethod: Executes after each test method.
    • @BeforeClass: Executes once before any test methods in the current class.
    • @AfterClass: Executes once after all test methods in the current class.
  2. TestNG XML Configuration: TestNG allows you to define test suites and test cases in an XML file, which provides flexibility in organizing and executing tests.

  3. Assertions: TestNG provides a set of assertion methods to validate test results, such as assertEquals, assertTrue, and assertFalse.

Practical Example

Let's create a simple TestNG test case to demonstrate these concepts. We will write a test case to verify the title of a webpage using Selenium WebDriver.

Step-by-Step Guide

  1. Set Up Your Project: Ensure that you have TestNG and Selenium WebDriver set up in your project. You can add TestNG to your project using Maven by including the following dependency in your pom.xml:

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
    
  2. Create a Test Class: Create a new Java class for your test case.

    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 TitleVerificationTest {
        WebDriver driver;
    
        @BeforeMethod
        public void setUp() {
            // Set the path for the ChromeDriver
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
    
        @Test
        public void verifyTitle() {
            driver.get("https://www.example.com");
            String expectedTitle = "Example Domain";
            String actualTitle = driver.getTitle();
            Assert.assertEquals(actualTitle, expectedTitle, "Title does not match!");
        }
    
        @AfterMethod
        public void tearDown() {
            driver.quit();
        }
    }
    
  3. Explanation of the Code:

    • @BeforeMethod: Initializes the WebDriver and opens the browser before each test method.
    • @Test: Contains the test logic. In this case, it navigates to a webpage and verifies the title.
    • Assert.assertEquals: Compares the actual title with the expected title and fails the test if they do not match.
    • @AfterMethod: Closes the browser after each test method.
  4. Run the Test: You can run the test using your IDE's TestNG plugin or by executing the test suite defined in a TestNG XML file.

Practical Exercise

Exercise: Create a TestNG test case to verify the presence of a specific element on a webpage.

  1. Objective: Write a test case to check if a button with the ID submit-button is present on the page https://www.example.com.

  2. Solution:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    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 ElementPresenceTest {
        WebDriver driver;
    
        @BeforeMethod
        public void setUp() {
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
    
        @Test
        public void verifyButtonPresence() {
            driver.get("https://www.example.com");
            WebElement button = driver.findElement(By.id("submit-button"));
            Assert.assertNotNull(button, "Submit button is not present!");
        }
    
        @AfterMethod
        public void tearDown() {
            driver.quit();
        }
    }
    

Feedback and Tips:

  • Common Mistake: Forgetting to set the correct path for the WebDriver executable. Ensure the path is correctly set in System.setProperty.
  • Tip: Use driver.manage().timeouts().implicitlyWait() to handle dynamic content loading.

Conclusion

In this section, we learned how to create TestNG test cases using annotations and assertions. We also explored how to set up and execute a simple test case with Selenium WebDriver. Understanding these basics will help you build more complex test scenarios and integrate them into your test automation framework. In the next module, we will delve into data-driven testing with TestNG, which will allow you to run the same test with multiple sets of data.

Test Automation with Selenium

Module 1: Introduction to Test Automation

Module 2: Getting Started with Selenium

Module 3: Locating Web Elements

Module 4: Interacting with Web Elements

Module 5: Synchronization in Selenium

Module 6: Test Frameworks and Selenium

Module 7: Advanced Selenium Concepts

Module 8: Selenium Grid and Parallel Testing

Module 9: Continuous Integration and Selenium

Module 10: Best Practices and Troubleshooting

© Copyright 2024. All rights reserved