我正在使用所需的 chrome 功能来启动我的 chrome 浏览器并执行我的 E2E 测试,但收到以下警告:
WARNING: Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [accessKey, testName, unexpectedAlertBehaviour]; Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/
May 03, 2023 12:05:28 AM org.openqa.selenium.remote.ProtocolHandshake createSession
WARNING: Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [accessKey, testName, unexpectedAlertBehaviour]; Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/
为了解决这个警告消息,我在 ChromeOptions 的帮助下更新了我的代码,但在这样做之后我在执行测试时遇到了性能问题。最初测试需要 4 分钟,但现在相同的测试需要超过 15 分钟。
我不确定我还需要做什么。有人能帮助我吗?这是到目前为止我得到的代码:
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.thucydides.core.environment.SystemEnvironmentVariables;
import net.thucydides.core.util.EnvironmentVariables;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@AllArgsConstructor
public class ChromeDriver implements WrappedWebDriver {
private static final Logger logger = LoggerFactory.getLogger(ChromeDriver.class);
static String accessKey = System.getenv().get("access_key");
@NotNull
private static ChromeOptions getChromeOptions() {
final ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("googlegeolocationaccess.enabled", false);
prefs.put("profile.default_content_setting_values.geolocation", 2); // 1:allow 2:block
prefs.put("profile.default_content_setting_values.notifications", 1);
prefs.put("profile.managed_default_content_settings", 1);
prefs.put("download.default_directory", System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "TestData" + File.separator + "clsp" + File.separator + "Downloads");
chromeOptions.setExperimentalOption("prefs", prefs);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.addArguments(
"--ignore-certificate-errors",
"--disable-download-notification",
"--no-sandbox",
"--disable-site-isolation-trials",
"--enable-strict-powerful-feature-restrictions",
"--disable-geolocation",
"--disable-gpu",
"--disable-dev-shm-usage"
);
if (SystemUtils.IS_OS_LINUX) {
chromeOptions.addArguments(
"--headless",
"--allow-running-insecure-content",
// "--disable-web-security",
"--window-size=1920,1080"
);
} else if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_WINDOWS) {
chromeOptions.addArguments(
"--incognito",
"--start-fullscreen"
);
}
return chromeOptions;
}
@Override
public WebDriver newDriver() {
RemoteWebDriver driver = null;
final ChromeOptions chromeOptions = new ChromeOptions();
EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();
boolean remote = Boolean.parseBoolean(environmentVariables.getProperty("remote"));
String serverAddress = environmentVariables.getProperty("experitest.server");
ChromeDriverService service;
try {
binarySetup();
service = new ChromeDriverService.Builder()
.usingAnyFreePort()
.build();
service.start();
chromeOptions.setCapability(CapabilityType.BROWSER_VERSION, "112");
chromeOptions.setCapability(ChromeOptions.CAPABILITY, getChromeOptions());
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
chromeOptions.setCapability("", "Test Execution");
chromeOptions.setCapability("accessKey", XXXX);
chromeOptions.setCapability("acceptInsecureCerts", true);
driver = new RemoteWebDriver(new URL(serverAddress), chromeOptions);
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
} catch (Exception e) {
logger.error("Error due to " + e.getMessage());
throw new UnreachableBrowserException(e.getMessage());
}
return driver;
}
private void binarySetup() {
try {
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_WINDOWS || SystemUtils.IS_OS_LINUX) {
logger.info("driver binary setup started...");
ChromeDriverManager.getInstance(DriverManagerType.CHROME).setup();
WebDriverManager.chromedriver().setup();
} else {
System.getenv();
}
} catch (Exception e) {
logger.info("Driver binary not setup correctly...");
e.printStackTrace();
}
}
@Override
public boolean takesScreenshots() {
return true;
}
}
现在铬
--headless=new
,这是新的无头模式面临性能问题:
铬问题1
这可能也是您的问题。一种解决方案是使用旧的无头模式,但这不会在您的测试中下载或复制文本。例如,如果您正在运行与外部文件交互的测试,这就是一个问题。