2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
嘗試使用 junit(j5 jupiter) 接口擴(kuò)展您的驅(qū)動(dòng)程序類并覆蓋之前/之后的方法,這是一個(gè)簡(jiǎn)單的示例,使用您的一些代碼:
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
public class Driver implements AfterTestExecutionCallback, BeforeTestExecutionCallback, BeforeAllCallback, AfterAllCallback {
protected WebDriver driver;
@Override
public void beforeAll(ExtensionContext context) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Selenium and drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://the-internet.herokuapp.com/");
driver.manage().window().maximize();
System.out.println("Set up running");
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
driver().quit();
}
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
//whatever steps you need before EACH test, like navigate to homepage etc...
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
// steps do to after each test, best practice is to clear everything:
driver.manage().deleteAllCookies();
driver.executeScript("window.sessionStorage.clear()");
driver.executeScript("window.localStorage.clear()");
}
}

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
看來(lái)您的 DriverSingleton 的驅(qū)動(dòng)程序從未被初始化,并且在setup()SeleniumSetup 類的方法中,SeleniumSetup 的驅(qū)動(dòng)程序在您每次運(yùn)行代碼時(shí)都會(huì)被初始化并使用,將 放在tearDown()末尾,setup()您的瀏覽器窗口將關(guān)閉。
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Selenium and drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://the-internet.herokuapp.com/");
driver.manage().window().maximize();
System.out.println("Set up running");
// <<------your test scenario should be placed here
tearDown();
添加回答
舉報(bào)