我知道这里还有其他一些类似的问题,但我通读了它们,但无法解决我的问题。我对 JUNIT 注释等也不太熟悉,所以这也让我感到困惑。这是我所拥有的,我只想在成功(或不成功)测试后关闭 Firefox 窗口。
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestWorkGaps extends TestCase {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() {
driver = (WebDriver) new FirefoxDriver();
baseUrl = "https://dev.XXX.com";
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
@Test
public void test() throws InterruptedException {
driver.get(baseUrl);
success = core.TestCore.checkMain(driver);
if (!success) {
fail("Main page did not load correctly.");
}
//various other tasks
success = core.LoginLogout.logout(driver);
if (!success) {
fail("Not able to logout.");
}
}
@After
public void closeWindow()
{
driver.close();
driver.quit();
}
}
提前谢谢你们,你们是最棒的。如果相关,我可以提供我的 pom.xml。
最佳答案
您正在混合新旧 JUnit;这就是它不起作用的原因。您有两种可能的方法来解决此问题:
- 旧方法:删除注释并将
closeWindow()
重命名为tearDown()
。这将覆盖TestCase
中相应的函数。 - 新方法:不扩展
TestCase
。然后将使用注释并调用closeWindow()
方法。
您的 setUp()
函数已经覆盖了 TestCase
中的相应函数,这就是该部分起作用的原因。然而,closeWindow()
对于 TestCase
来说是未知的,并且如果扩展 TestCase
,JUnit 似乎会使用不同的运行器。
关于java - Selenium WebDriver 测试后无法关闭 Firefox 窗口 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943101/