我正在 Windows 10 机器上创建测试(ui 测试)。它们运行良好,但几天前我的老板告诉我我们需要在 linux 上运行我们的测试。我下载了 linux 驱动程序并在 System.setProperty("webdriver.chrome.driver", "chromedriver");
中进行了更改,但在尝试运行此测试后我得到了 java.lang。 ExceptionInInitializerError
(这是带有最新浏览器的最新驱动程序)。之后我更改了允许我运行测试的代码,但与驱动程序的连接是远程的。我不喜欢这种方式。也许你们中有人知道哪个驱动程序可以在 linux 上工作而无需在驱动程序初始化部分更改代码?
例如 Windows 驱动程序初始化:
private static WebDriver driver = new ChromeDriver();
private static WebDriverWait wait = new WebDriverWait(driver, 30);
@Given("^blah blah$")
public void some_method() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
}
linux 驱动程序初始化:
public abstract class InitDrivers{
private static DesiredCapabilities capability = DesiredCapabilities.chrome();
public static WebDriver driver;
static {
try {
driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capability);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public static WebDriverWait wait = new WebDriverWait(driver, 30);
public class CallDoctorTestStep extends InitDrivers{
@Given("^blah blah$")
public void some_method() throws MalformedURLException{
//System.setProperty("webdriver.chrome.driver","chromedriver.exe");
}
请参阅 Selenium NoSuchSession on linux 中的解决方案
最佳答案
java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
暗示在静态初始化器 中发生了意外异常。抛出此错误表示在评估静态初始化程序或静态变量的初始化程序期间发生异常。
如果静态初始化程序 block 中出现问题,则会引发ExceptionInInitializerError。下面的例子:
class Anton
{
static
{
// if something goes wrong ExceptionInInitializerError will be thrown
}
}
静态变量在静态 block 中初始化,可能会抛出这些错误。
问题:
在您的 Linux 驱动程序初始化 代码块中,最初您提到:
private static DesiredCapabilities capability = DesiredCapabilities.chrome();
然后调用 RemoteWebDriver 如下:
driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capability);
但在接下来的步骤中您再次尝试:
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
这一系列事件导致了错误。
解决方案:
因为您已经将 WebDriver 实例声明为:
public static WebDriver driver;
接下来,使用
System.setProperty()
:System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // <- remove the .exe part here following Linux style
现在您需要按如下方式初始化 RemoteWebDriver 实例:
driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capability);
由于 WebDriver 实例(
静态
)和 Web Browser 实例现在处于 Activity 状态,因此您不得在期间更改属性测试执行。
Note : You can find a detailed discussion in
exception in initializer error
关于在 Linux 上使用 chromedriver 和 chrome 时出现 java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003750/