Test suites (testng.xml) in Selenium Java - Build an Automation Script
package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class LoginTest { WebDriver driver; @BeforeClass public void setup() { driver = new ChromeDriver(); driver.get("https://example.com/login"); } @Test public void testValidLogin() { driver.findElement(By.id("email")).sendKeys("user@example.com"); driver.findElement(By.id("password")).sendKeys("Password123"); driver.findElement(By.id("loginButton")).click(); String currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("dashboard"), "User should be redirected to dashboard"); } @AfterClass public void teardown() { driver.quit(); } } package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class SearchTest { WebDriver driver; @BeforeClass public void setup() { driver = new ChromeDriver(); driver.get("https://example.com"); } @Test public void testSearchFunctionality() { driver.findElement(By.id("searchBox")).sendKeys("Selenium"); driver.findElement(By.id("searchButton")).click(); String resultsText = driver.findElement(By.id("results")).getText(); Assert.assertTrue(resultsText.contains("Selenium"), "Search results should contain 'Selenium'"); } @AfterClass public void teardown() { driver.quit(); } } /* testng.xml file content */ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="ExampleSuite"> <test name="LoginAndSearchTests"> <classes> <class name="tests.LoginTest"/> <class name="tests.SearchTest"/> </classes> </test> </suite>
The solution includes two test classes: LoginTest and SearchTest. Each class uses Selenium WebDriver to open a browser and perform actions on the web page.
Each test class has a @BeforeClass method to set up the browser and navigate to the starting URL, a @Test method to perform the test and assert expected behavior, and an @AfterClass method to close the browser.
The testng.xml file defines a suite named ExampleSuite with one test named LoginAndSearchTests. It includes both test classes by their fully qualified names.
Running the testng.xml file with TestNG will execute both test classes in the order listed, verifying that all tests pass.
Now add data-driven testing to LoginTest with 3 different sets of valid credentials
Practice
testng.xml file in Selenium testing?Solution
Step 1: Understand the role of
Thetestng.xmltestng.xmlfile is used to define which tests to run and how to organize them.Step 2: Identify the main purpose
It groups test classes and methods so they can be run together as a suite.Final Answer:
To group and organize test classes and methods to run together -> Option CQuick Check:
Test suite grouping = To group and organize test classes and methods to run together [OK]
- Confusing testng.xml with test code files
- Thinking testng.xml stores test data
- Assuming testng.xml generates reports
testng.xml?Solution
Step 1: Recall the correct XML structure for TestNG suites
The root element is<suite>, inside it we define<test>, then<classes>and<class>.Step 2: Match the options with correct structure
correctly follows this order and syntax.Final Answer:
<suite name="MySuite"><test name="Test1"><classes><class name="TestClass"/></classes></test></suite> -> Option AQuick Check:
Suite > Test > Classes > Class = [OK]
- Swapping <suite> and <test> tags
- Placing <class> directly under <suite>
- Using wrong tag names or attributes
testng.xml snippet, what tests will run?<suite name="Suite1" parallel="tests" thread-count="2">
<test name="TestA">
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
<test name="TestB">
<classes>
<class name="tests.PaymentTest"/>
</classes>
</test>
</suite>Solution
Step 1: Analyze the
The attributeparallelattribute in <suite>parallel="tests"means each<test>runs in parallel threads.Step 2: Identify the tests defined
There are two tests: TestA with LoginTest class and TestB with PaymentTest class.Final Answer:
Both LoginTest and PaymentTest run in parallel as separate tests -> Option BQuick Check:
parallel="tests" runs tests simultaneously = Both LoginTest and PaymentTest run in parallel as separate tests [OK]
- Assuming tests run sequentially despite parallel attribute
- Thinking <methods> tag is mandatory to run tests
- Confusing parallel="tests" with parallel="classes"
testng.xml file but tests are not running:<suite name="Suite1">
<test name="Test1">
<class name="TestClass">
</class>
</test>
</suite>What is the likely problem?
Solution
Step 1: Check the
In TestNG XML,<class>tag syntax<class>should be self-closing like<class name="TestClass"/>or empty without content.Step 2: Identify the error in the given XML
The given XML uses separate open and close tags for<class>which is invalid and causes tests not to run.Final Answer:
The <class> tag should be self-closing or empty, not with separate open and close tags -> Option DQuick Check:
Use self-closing <class> tag = The<class>tag should be self-closing or empty, not with separate open and close tags [OK]
- Adding parallel attribute thinking it fixes syntax
- Forgetting package name but that doesn't stop tests
- Assuming thread-count is mandatory
LoginTest and PaymentTest in parallel but limit total threads to 1 in testng.xml. Which configuration achieves this?Solution
Step 1: Understand parallel and thread-count attributes
Settingparallel="classes"runs test classes in parallel, andthread-count="1"limits to one thread, so classes run one at a time.Step 2: Check options for matching configuration
sets parallel to classes and thread-count to 1, matching the requirement.Final Answer:
Option A configuration runs classes in parallel but limits threads to 1 -> Option AQuick Check:
parallel="classes" + thread-count=1 = [OK]
- Using parallel="tests" with thread-count > 1 runs multiple threads
- Omitting parallel attribute runs tests sequentially
- Confusing parallel="methods" with classes
Ready to prove it?
Pass the test and this pattern moves unknown
