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
-
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.
-
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.
-
Assertions: TestNG provides a set of assertion methods to validate test results, such as
assertEquals
,assertTrue
, andassertFalse
.
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
-
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>
-
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(); } }
-
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.
-
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.
-
Objective: Write a test case to check if a button with the ID
submit-button
is present on the pagehttps://www.example.com
. -
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
- What is Test Automation?
- Benefits of Test Automation
- Overview of Selenium
- Setting Up Your Environment
Module 2: Getting Started with Selenium
- Introduction to Selenium WebDriver
- Installing Selenium WebDriver
- First Selenium Script
- Understanding WebDriver Interface
Module 3: Locating Web Elements
- Introduction to Locators
- Using ID and Name Locators
- XPath and CSS Selectors
- Advanced Locator Strategies
Module 4: Interacting with Web Elements
- Performing Actions on Web Elements
- Handling Dropdowns and Checkboxes
- Working with Alerts and Pop-ups
- Managing Browser Windows and Frames
Module 5: Synchronization in Selenium
Module 6: Test Frameworks and Selenium
- Introduction to TestNG
- Setting Up TestNG with Selenium
- Creating TestNG Test Cases
- Data-Driven Testing with TestNG
Module 7: Advanced Selenium Concepts
Module 8: Selenium Grid and Parallel Testing
- Introduction to Selenium Grid
- Setting Up Selenium Grid
- Running Tests in Parallel
- Cross-Browser Testing
Module 9: Continuous Integration and Selenium
- Introduction to Continuous Integration
- Integrating Selenium with Jenkins
- Automating Test Execution
- Reporting and Logging