I have a similar issue and need help to understand if my code needs any restructuring. The issue is for the second test SeleniumDragnDrop, the browser does not close.
parent class
------------
package selenium.setup;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
public class SeleniumBaseTest {
protected final static ThreadLocal<WebDriver> webDriver = new ThreadLocal<>
();
@BeforeClass
@Parameters({"browser","baseURL"})
public void Setup(String browser, String baseURL) throws Exception{
webDriver.set(BrowserManager.doBrowserSetup(browser));
System.out.println("Before Test Thread ID:
"+Thread.currentThread().getId());
//get URL
webDriver.get().get(baseURL);
webDriver.get().manage().window().maximize();
webDriver.get().manage().deleteAllCookies();
webDriver.get().manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
}
@AfterClass
public void tearDown(){
webDriver.get().quit();
System.out.println("After Test Thread ID:
"+Thread.currentThread().getId());
webDriver.remove();
}
}`
Child class
-----------
`package selenium.trials;
import java.time.Duration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.ITestContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import selenium.setup.SeleniumBaseTest;
import selenium.utils.WebElementUtility;
public class SeleniumDragnDrop extends SeleniumBaseTest{
Logger logger;
WebDriver driver;
@BeforeTest(alwaysRun=true)
public void beforeTest(ITestContext testContext){
logger = LogManager.getLogger(this.getClass());
driver = webDriver.get();
logger.info("In dragNDrop beforeTest: Thread ID:
"+Thread.currentThread().getId());
}
@Test
public void dragNDrop() throws InterruptedException {
logger.info("In dragNDrop Thread ID: "+Thread.currentThread().getId());
WebElementUtility.clickOn(driver,driver.findElement(By.linkText("Drag and
Drop")), Duration.ofSeconds(15));
WebElement source = driver.findElement(By.id("column-a"));
WebElement dest = driver.findElement(By.id("column-b"));
Actions action = new Actions(driver);
action.dragAndDrop(source, dest).build().perform();
logger.info("After dragNDrop Thread ID: "+Thread.currentThread().getId());
}
@AfterTest
public void tearDown() {
logger.info("In dragNDrop tearDown : Thread ID:
"+Thread.currentThread().getId());
}
}`
package selenium.trials;
import java.awt.AWTException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import selenium.setup.SeleniumBaseTest;
import selenium.utils.WebElementUtility;
public class SeleniumAlerts extends SeleniumBaseTest{
Logger logger;
WebDriver webDriver;
@BeforeMethod
public void beforeTest(ITestContext testContext){
logger = LogManager.getLogger(this.getClass());
//webDriver = driver.get();
logger.info("In testAlerts beforeMethod: Thread ID: "+Thread.currentThread().getId());
logger.info(" Driver " + driver.get().getCurrentUrl());
}
@Test
public void testAlerts() throws InterruptedException {
logger = LogManager.getLogger(this.getClass());
logger.info("In testAlerts Thread ID: "+Thread.currentThread().getId());
Thread.sleep(2000);
//driver = webDriver.get();
try {
WebElementUtility.clickOn(driver.get(),driver.get().findElement(By.linkText("JavaScript Alerts")), Duration.ofSeconds(15));
}catch(org.openqa.selenium.StaleElementReferenceException ex)
{
System.out.println(" current URL " + driver.get().getCurrentUrl());
WebElementUtility.clickOn(driver.get(),driver.get().findElement(By.linkText("JavaScript Alerts")), Duration.ofSeconds(15));
}
WebElement ele = driver.get().findElement(By.xpath("//button[text()='Click for JS Alert']"));
WebElementUtility.clickOnAlert(driver.get(),ele, Duration.ofSeconds(20),true);
WebElementUtility.clickOnAlert(driver.get(),driver.get().findElement(By.xpath("//button[text()='Click for JS Confirm']")), Duration.ofSeconds(20),false);
clickOnPrompt(driver.get(),driver.get().findElement(By.xpath("//button[text()='Click for JS Prompt']")), Duration.ofSeconds(20),false);
logger.info("In testAlerts Thread ID: "+Thread.currentThread().getId());
//driver.get().quit();
}
@AfterMethod
public void setDown() {
logger.info("In testAlerts setDown : Thread ID: "+Thread.currentThread().getId());
//driver.get().quit();
}
testng.xml
---------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="classes">
<parameter name="browser" value="Chrome"/>
<parameter name="baseURL" value="https://the-internet.herokuapp.com"/>
<test name="SeleniumTrials" preserve-order="true">
<classes>
<class name="selenium.trials.SeleniumDragnDrop"/>
<class name="selenium.trials.SeleniumAlerts"/>
</classes>
</test>
</suite>
log4j.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %-5logger{36} - %msg%n"/>
</Console>
<RollingFile name="File" fileName="${basePath}/selenium.log" filePattern="${basePath}/automation-%d{yyyy-MM">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %-5logger{36} - %msg%n"/>
<SizeBasedTriggeringPolicy size="50000"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
我有一个使用驱动程序类实例化 WebDriver 的父类和具有 @Test 方法的子类。
问题是第二次测试 SeleniumDragnDrop 时,浏览器未关闭。 Debuggnig 向我显示 @AfterClass 没有被调用。从这个测试的 logger.info 也没有被记录这一事实也可以看出。下面记录跟踪。 @BeforeClass 被调用
17:23:40.558 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - Before Class: Setup Thread ID: 23
课后不会出现任何记录。查看此日志跟踪..
Log -
---
17:23:0.015 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - Before Class: Setup Thread ID: 23
17:23:36.591 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - Before Class: Setup Thread ID: 24
17:23:40.558 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - Before Class: Setup Thread ID: 23
17:24:15.498 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - In testAlerts beforeMethod: Thread ID: 24
17:24:15.518 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - Driver https://the-internet.herokuapp.com/
17:24:19.681 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - In testAlerts Thread ID: 24
17:24:23.108 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - In dragNDrop beforeMethod: Thread ID: 23
17:24:23.112 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - In dragNDrop Thread ID: 23
17:24:28.017 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - After dragNDrop Thread ID: 23
17:24:28.019 [TestNG-test=SeleniumTrials-1] INFO selenium.trials.SeleniumDragnDrop - In dragNDrop setDown : Thread ID: 23
17:24:40.756 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - In testAlerts Thread ID: 24
17:24:40.760 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - In testAlerts setDown : Thread ID: 24
17:24:55.176 [TestNG-test=SeleniumTrials-2] INFO selenium.trials.SeleniumAlerts - After Class: Setup Thread ID: 24
问题是测试 SeleniumDragnDrop 有 @AfterTest 方法,该方法正在关闭会话。因此 @AfterClass 没有被调用。删除@AfterTest后,它工作正常