0
0
Selenium Javatesting~15 mins

Test suites (testng.xml) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Run multiple test classes using TestNG suite XML
Preconditions (3)
Step 1: Create a testng.xml file that includes both LoginTest and SearchTest classes
Step 2: Run the testng.xml suite file using TestNG
Step 3: Observe that both test classes run in the order defined in the XML
Step 4: Verify that all tests in both classes pass
✅ Expected Result: Both LoginTest and SearchTest classes run successfully when executing the testng.xml suite file, and all tests pass.
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify that tests in LoginTest pass
Verify that tests in SearchTest pass
Best Practices:
Use descriptive testng.xml with clear suite and test names
Use fully qualified class names in testng.xml
Keep test classes independent
Use TestNG annotations properly
Run suite via TestNG runner
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Using relative class names without package in testng.xml
Not closing the browser in @AfterClass
Running test classes individually instead of via testng.xml suite
Hardcoding URLs or test data inside test methods without flexibility
Bonus Challenge

Now add data-driven testing to LoginTest with 3 different sets of valid credentials

Show Hint