java.lang.ExceptionInInitializerError而与chromedrive

2019-09-26 19:37发布

我创建的窗口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");
}

请参阅解决方案 在Linux硒NoSuchSession

Answer 1:

java.lang.ExceptionInInitializerError

java.lang.ExceptionInInitializerError意味着发生在一个静态初始化意外的异常。 是抛出这个错误表示一个静态初始化或静态变量初始化的评估过程中发生了异常。

如果出现错误的静态初始化块中的ExceptionInInitializerError提高。 下面的例子:

class Anton
{
  static
  {
     // if something goes wrong ExceptionInInitializerError will be thrown
  }
}

静态变量在静态初始化块,可以把这些错误。


问题:

  • 在你的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的情况下(这是static )和Web浏览器实例是活动的,现在你一定没有测试执行过程中更改属性。

注意 :你可以找到详细的讨论exception in initializer error



文章来源: java.lang.ExceptionInInitializerError while working with chromedriver and chrome on linux