java.lang.ExceptionInInitializerError while workin

2019-08-14 06:00发布

I'm creating tests(ui tests) on windows 10 machine. They works well, but few days ago my boss told me that we need to run our tests on linux. I'm downloaded linux driver and change it in System.setProperty("webdriver.chrome.driver", "chromedriver"); but after trying to run this test i got java.lang.ExceptionInInitializerError(it was latest driver with latest browser). After it i changed my code that allow me to run test, but connection to driver is remote. I don't like this way. May be some one of you know which driver will work on linux without code change in driver initialization part?

E.g. windows driver initialization :

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 driver initialization :

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");
}

See solution in Selenium NoSuchSession on linux

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-14 06:26

java.lang.ExceptionInInitializerError

java.lang.ExceptionInInitializerError implies that an unexpected exception has occurred in a Static Initializer. This error is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

An ExceptionInInitializerError is raised if something goes wrong in the static initializer block. An example below :

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

Static variables are initialized in static blocks and can throw these errors.


Problem :

  • In your Linux Driver Initialization code block, initially you have mentioned :

    private static DesiredCapabilities capability = DesiredCapabilities.chrome();
    
  • Then invoked the RemoteWebDriver as follows :

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capability);
    
  • But in the following steps you have again tried to :

    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    

This sequence of events creates the error.

Solution :

  • As you have already declared the WebDriver instance as :

    public  static WebDriver driver;
    
  • Next, use System.setProperty() :

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // <- remove the .exe part here following Linux style
    
  • Now you need to initialize the RemoteWebDriver instance as follows :

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capability);
    
  • As the WebDriver instance (which is static) and Web Browser instance is active now you must not change the attributes during the Test Execution.

Note : You can find a detailed discussion in exception in initializer error

查看更多
登录 后发表回答