Android测试类失败,错误为java.lang.ExceptionInInitializerError

时间:2016-08-10 07:00:41

标签: android appium android-testing

我试图在android中编写测试类。但它给我一个像下面这样的错误。它在开始"的线路上崩溃了。 driver = new RemoteWebDriver(新网址" 。我该如何解决这个问题?我是测试新手。很抱歉,如果这很容易。

测试类:

public class ExampleUnitTest {

        WebDriver driver;

        @BeforeTest
        public void setUp() throws MalformedURLException {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("deviceName", "Sony Xperia Z - 4.3 - API 18 - 1080x1920");
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
            capabilities.setCapability(CapabilityType.VERSION, "4.3");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("appPackage", "com.android.calculator2");
            capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

        @Test
        public void Sum() {
            driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

            driver.findElement(By.name("2")).click();      
            driver.findElement(By.name("+")).click();       
            driver.findElement(By.name("5")).click(); 
            driver.findElement(By.name("=")).click();


            String result = driver.findElement(By.className("android.widget.EditText")).getText();
            System.out.println("Number sum result is : " + result);

        }

        @AfterTest
        public void End() {
            driver.quit();
        }
    }

错误输出:

  java.lang.ExceptionInInitializerError
        at org.apache.http.conn.ssl.SSLSocketFactory.<clinit>(SSLSocketFactory.java:151)
        at org.openqa.selenium.remote.HttpCommandExecutor.getClientConnectionManager(HttpCommandExecutor.java:94)
        at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:112)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:89)
        at com.moduletry.dev4.appiumsimpletest1.ExampleUnitTest.setUp(ExampleUnitTest.java:50)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.TestRunner.beforeRun(TestRunner.java:647)
        at org.testng.TestRunner.run(TestRunner.java:615)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
        at org.testng.SuiteRunner.run(SuiteRunner.java:259)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
        at org.testng.TestNG.run(TestNG.java:1018)
        at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
        at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
    Caused by: java.lang.RuntimeException: Stub!
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:14)
        at org.apache.http.conn.ssl.AbstractVerifier.<init>(AbstractVerifier.java:60)
        at org.apache.http.conn.ssl.AllowAllHostnameVerifier.<init>(AllowAllHostnameVerifier.java:43)
        at org.apache.http.conn.ssl.AllowAllHostnameVerifier.<clinit>(AllowAllHostnameVerifier.java:45)
        ... 31 more

尝试后更新 - 捕获阻止:

   try {
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }catch (ExceptionInInitializerError error){
        System.out.println(error.getCause());
        System.out.println(error.getMessage());
        System.out.println(error.getLocalizedMessage());
        System.out.println(error.getStackTrace().toString());

    }

输出

java.lang.RuntimeException: Stub!
null
null
[Ljava.lang.StackTraceElement;@7f2502bc

java.lang.NullPointerException
    at com.moduletry.dev4.appiumsimpletest1.ExampleUnitTest.setUp(ExampleUnitTest.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:647)
    at org.testng.TestRunner.run(TestRunner.java:615)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
    at org.testng.TestNG.run(TestNG.java:1018)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

2 个答案:

答案 0 :(得分:0)

不要有50个声誉,所以无法发表评论,这可能不是答案! java.lang.ExceptionInInitializerError让我想到问题可能是你试图在新对象创建中初始化URL。您可能想尝试初始化URL,然后将其传递到对象创建中?

或者问题可能在RemoteWebDriver中吗?可能有一些对象没有在那里初始化?

文件说:

  

表示静态初始化程序中发生意外异常。抛出ExceptionInInitializerError以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常。

https://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html

答案 1 :(得分:0)

问题正在发生,因为您的套接字未打开并在初始化程序块中抛出异常。

套接字未连接的原因可能是多个,最常见的是:

1)您的端口可能无法访问

2)您的服务器可能已关闭

3)如果它是一个实际设备,它必须与您的localhost位于同一网络中。此外,它无法直接ping本地主机,它必须连接到您的PC /笔记本电脑,你应该提供你的电脑的ipv4地址

例如:ipconfig显示您的PC地址:192.168.149.8

然后您的网址将为http://192.168.149.8:yourPortNo/yoururl

4)端口必须在您的调制解调器/路由器中转发到您的PC的IP,才能将请求从您的手机重定向到您的PC ....

以下是设置本地主机以供全世界访问的完整教程:

http://mehtadhaval.blogspot.in/2012/03/turn-your-pc-into-web-server-and-host.html

如果您对托管页面不感兴趣,请按照端口转发的要求进行操作....端口转发因路由器而异。

查看您的网址失败的原因:How can I access my localhost from my Android device?

非常确定问题是您的远程Web驱动程序的初始化:

以这种方式使用它并尝试:

// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
WebDriver driver = new RemoteWebDriver(capabilities);


// And now use it after getting the handler first...
driver.get("http://127.0.0.1:4723/wd/hub");