如何使用TestNG和Selenium每次在新窗口运行不同Java类的测试用例
Got it, let's get your 3 test classes running in separate browser windows. Here's the step-by-step fix:
1. Update Your TestNG XML Configuration
Right now, your suite uses parallel="methods", which runs every individual test method in parallel. To run entire test classes in parallel (each in their own window), switch to parallel="classes". Set thread-count to at least 3 (matching the number of your test classes) so all classes can run simultaneously.
Here's the modified XML:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Fanfight Test" thread-count="3" parallel="classes"> <listeners> <listener class-name="com.fanfight.test_case.ListenerClass"></listener> </listeners> <test name="User Login"> <!-- Add your test classes here --> <classes> <class name="com.fanfight.test_case.Class1"/> <class name="com.fanfight.test_case.Class2"/> <class name="com.fanfight.test_case.Class3"/> </classes> </test> </suite>
2. Use Per-Class WebDriver Instances
Critical: Don’t share a static WebDriver across classes. Instead, initialize a unique driver for each test class in the @BeforeClass method, and clean it up in @AfterClass. This ensures each class gets its own fresh browser window.
Example test class structure:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class Class1 { private WebDriver driver; @BeforeClass public void setUp() { // Initialize driver for this class driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test public void testMethod1() { driver.get("https://your-url.com"); // Test logic here } // ... 14 more test methods ... @AfterClass public void tearDown() { // Quit driver after all methods in this class finish if (driver != null) { driver.quit(); } } }
3. Ensure Your Listener Works with Per-Class Drivers
If your ListenerClass interacts with WebDriver, make sure it accesses the driver instance tied to the current test class/thread. Avoid static driver references here. You can retrieve the test class instance from ITestResult to get its driver:
import org.testng.ITestResult; import org.testng.TestListenerAdapter; public class ListenerClass extends TestListenerAdapter { @Override public void onTestStart(ITestResult result) { // Get the test class instance Object testClass = result.getInstance(); // Assuming your test classes extend a base class with getDriver() if (testClass instanceof YourBaseTestClass) { WebDriver driver = ((YourBaseTestClass) testClass).getDriver(); // Use driver for listener logic (e.g., take screenshot on failure) } } }
What This Does
When you run the suite:
- Each of your 3 test classes will launch its own browser window.
- All 15 methods in a single class will run sequentially in that same window (preserving session state if needed).
- The 3 classes will run in parallel (thanks to
parallel="classes"andthread-count="3").
内容的提问来源于stack exchange,提问作者shailav agrawal

