我的测试脚本使用的是黄瓜。 在运行任何方案时,都会启动第一个浏览器并离开那里,打开另一个浏览器,并使用该浏览器运行测试。测试完成后,仅关闭以后的浏览器。我进行了调试,该钩子是通过size = 2启动的(我认为这是主要问题)。
[hooks size = 2 1
这是我的钩子
package step_definitions;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import helpers.CommonActions;
import helpers.Constant;
import helpers.Log;
import io.github.bonigarcia.wdm.ChromeDriverManager;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class Hooks {
protected static WebDriver driver;
@Before
public WebDriver setupDriver() {
Log.info("Opening browser...");
DesiredCapabilities capability = new DesiredCapabilities();
capability.setJavascriptEnabled(true);
String browser = System.getProperty("webdriver");
if (browser == null || browser.equalsIgnoreCase("") || browser.equalsIgnoreCase("InternetExplorer")) {
//InternetExplorerDriverManager.getInstance().arch32().setup();
ChromeDriverManager.getInstance().arch64().setup();
driver = new ChromeDriver(capability);
} else {
// Create Remote driver
try {
//capability = DesiredCapabilities.internetExplorer();
capability = DesiredCapabilities.chrome();
//capability.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
driver = new RemoteWebDriver(new URL(Constant.REMOTE_DRIVER_URL), capability);
} catch (MalformedURLException e) {
Log.info("Cannot connect to remote driver.");
e.printStackTrace();
}
}
openHomePage();
return driver;
}
@After
/**
* Embed a screenshot in test report if test is marked as failed
*/
public void embedScreenshot(Scenario scenario) {
if (scenario.isFailed()) {
try {
scenario.write("Current Page URL is " + driver.getCurrentUrl());
zoomBrowser("50");
//byte[] screenshot = getScreenshotAs(OutputType.BYTES);
byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
zoomBrowser("100");
} catch (WebDriverException somePlatformsDontSupportScreenshots) {
System.err.println(somePlatformsDontSupportScreenshots.getMessage());
}
}
if (driver != null) {
driver.manage().deleteAllCookies();
driver.close();
driver.quit();
}
}
private void openHomePage() {
driver.navigate().to(Constant.HOMEPAGE_FRONTEND);
if(CommonActions.getCurrentOperation().contains("Mac")){
driver.manage().window().fullscreen();
}else{
driver.manage().window().maximize();
}
}
public void zoomBrowser(String percent) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.body.style.zoom='" + percent + "%'");
}
}