我正在编写简单的测试用例来自动化测试 Web 应用程序。在我的 VS Code 中安装了 Maven,我想一切都正确了(因为 mvn -version 返回 Apache Maven 3.8.7...)。创建项目并尝试为 WebDriver 编写某种设置。问题是它调用了错误,我不明白为什么。
完整代码:
package com.pocselenium;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import org.junit.jupiter.api.*;
import org.openqa.selenium.edge.*;
import org.openqa.selenium.remote.service.DriverFinder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
public class LogInTestCase {
WebDriver webDriver;
@BeforeEach
public void setUp()
{
System.setProperty("webdriver.edge.driver", "C:\\Users\\MyUser\\Documents\\GitCloneJava\\PocSelenium\\javapoc\\msedgedriver.exe");
EdgeOptions options = new EdgeOptions();
options.addArguments("user-data-dir=C:\\Users\\MyUser\\Documents\\GitCloneJava\\PocSelenium\\javapoc\\User Data");
options.setBrowserVersion("stable");
DriverFinder finder = new DriverFinder(ChromeDriverService.createDefaultService(), options);
File driverPath = new File(finder.getDriverPath());
File browserPath = new File(finder.getBrowserPath());
EdgeDriverService service = new EdgeDriverService.Builder().usingDriverExecutable(driverPath).build();
//EdgeDriverService service = new EdgeDriverService.Builder().withLogOutput(System.out).build(); //Output of error to the console
options.setBinary(browserPath);
webDriver = new EdgeDriver(service, options);
assertTrue(webDriver != null);
}
@Test
public void logInTest()
{
webDriver.get("https://www.google.com");
}
}
完全错误:
[ERROR] Errors:
[ERROR] LogInTestCase.setUp:35 » SessionNotCreated Could not start a new session. Response code 500. Message: session not created: Microsoft Edge failed to start: crashed.
(chrome not reachable)
(The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.)
Host info: host: 'HOSTNAME', ip: 'xx.xx.xx.xx'
Build info: version: '4.20.0', revision: '866c76ca80'
System info: os.name: 'Windows Server 2016', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.23'
Driver info: org.openqa.selenium.edge.EdgeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: MicrosoftEdge, browserVersion: stable, ms:edgeOptions: {args: [user-data-dir=C:\Users\MyUser...], binary: C:\Program Files (x86)\Micr..
., extensions: []}}]}]
P.S.:所使用技术的版本: Maven - Apache Maven 3.8.7 Java - openjdk 版本“21.0.3”2024-04-16 LTS OpenJDK 运行时环境 Microsoft-9388422(内部版本 21.0.3+9-LTS) OpenJDK 64位服务器VM Microsoft-9388422(构建21.0.3+9-LTS,混合模式,共享) 硒 - 4.20.0
我已经检查了 Edge 和 Driver 的版本,下载了几次新的 Driver,更改了 Driver 和 Edge 的路径,杀死了所有 Microsoft Edge 进程,试图逐步找出异常在哪里引发(我猜它有一些东西)处理类加载器)。
从 Selenium 4.6 开始,添加了 SeleniumManager,它会自动为您下载和配置适当的驱动程序。因此,您不再需要使用 DriverManager 或指定路径等。
您的代码可以减少为
public class LogInTestCase {
WebDriver webDriver;
@BeforeEach
public void setUp()
{
EdgeOptions options = new EdgeOptions();
options.addArguments("user-data-dir=C:\\Users\\MyUser\\Documents\\GitCloneJava\\PocSelenium\\javapoc\\User Data");
options.setBrowserVersion("stable");
webDriver = new EdgeDriver(options);
assertTrue(webDriver != null);
}
@Test
public void logInTest()
{
webDriver.get("https://www.google.com");
}
}