我正在尝试在android中编写测试类。但是它给了我一个如下的错误。它在"driver = new RemoteWebDriver(new URL)"开头的行崩溃。我怎么解决这个问题?我是测试新手。对不起,如果这很容易。
测试类: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 - catch块之后更新:
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)
我没有50的声望,所以不能评论,这可能不是一个答案!java.lang.ExceptionInInitializerError让我想到,如果问题可能是你试图在新对象创建中初始化URL。您可能想要尝试初始化URL,然后在对象创建中传递它?
或者问题在RemoteWebDriver内部?可能有一些对象没有初始化?
文档说明:
https://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html表示静态初始化器中发生了意外异常。抛出ExceptionInInitializerError,指示在计算静态初始化项或静态变量初始化项期间发生异常。
问题发生在您的套接字未打开并在初始化块中抛出异常时。
套接字未连接的原因可以有多种,最常见的是:
1)您的端口可能无法访问
2)您的服务器可能已经关闭
3)如果它是一个实际的设备,它必须与你的本地主机在同一个网络中。它也不能直接ping本地主机,它必须连接到你的PC/笔记本电脑,你应该提供你的PC的ipv4地址
例如:ipconfig显示你的PC地址:192.168.149.8
那么你的URL将是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如果您对托管页面....不感兴趣,请继续执行端口转发端口转发会因路由器的不同而不同。
检查url失败的原因:我如何从我的Android设备访问我的localhost ?
非常确定问题是您的远程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");