0

I'm trying to write test class in android . But it throws me an error like below. It crashes at the line which starts " driver = new RemoteWebDriver(new URL" . How can I solve this ? I'm new in testing. Sorry if it is easy.

Test Class :

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

Error Output :

  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

UPDATED after try - catch block :

   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());

    }

Output :

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)
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34
  • put that part inside a try block... It's not able to connect to the url for whatever reason – Kushan Aug 10 '16 at 07:05
  • It doesnt give me a detailed error. – hkaraoglu Aug 10 '16 at 07:19
  • Sorry @Revenge mate there can be a hundred reasons why your socket isn't connecting... in catch block do a Log.e(tag,message,throwable exception) to give the detailed error stack.... – Kushan Aug 10 '16 at 08:15
  • As you can see, the root cause of your exception is `java.lang.RuntimeException: Stub!` which means that the logging for the apache framework can not be get up and running. If you dont use dependency management, try to find out which additional libs you need for it. I would guess you are missing a real implementation of logging framework – Henning Luther Aug 10 '16 at 20:05

2 Answers2

0

Don't have 50 reputation so can't comment and this might not be an answer! java.lang.ExceptionInInitializerError got me thinking if the issue could be that you are trying to initialise the URL inside the new object creation. You might want to try to initialise the URL and then passing it inside the object creation?

Or can the problem be inside RemoteWebDriver ? there might be some object that is not initialised there?

Documentations says:

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

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

ER H
  • 74
  • 2
  • 7
  • yea @ER H that's exactly what the error is... It happens when an exception occurs when trying to intialize an object – Kushan Aug 10 '16 at 08:13
  • Can be there a problem with 127.0.0.1 (localhost) ? – hkaraoglu Aug 10 '16 at 08:15
  • very possible.... try connecting in your browser. Also if it's an android device, it will not directly connect to localhost... you need to forward your port and give your PC's ip address instead of 127.0.0.1.... – Kushan Aug 10 '16 at 08:19
0

The issue is happening as your socket is not opening and throwing an Exception in your initializer block.

Reasons for a socket not connecting can be multiple, most common are:

1) Your port might be unreachable

2) Your server might be down

3) If it's an actual device, it has to be in the same network as your localhost. Also it will not be able to directly ping the localhost, it will have to be connected to your PC/laptop and you should provide the ipv4 address of your PC

eg: ipconfig shows your PC address: 192.168.149.8

Then your URL will be http://192.168.149.8:yourPortNo/yoururl

4) The port has to be forwarded in your modem/router to the IP of your PC for it to redirect requests from your mobile to your PC....

Here is a complete tutorial to setup your localhost for access over the whole world :

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

Follow it up to the point of port forwarding if you aren't interested in hosting the page.... Port forwarding will vary with different routers..

check out for reason why ur url fails : How can I access my localhost from my Android device?

Pretty sure that the issue is your initialization of the Remote web driver:

use it this way and try:

// 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");
Community
  • 1
  • 1
Kushan
  • 5,855
  • 3
  • 31
  • 45
  • Also you will have to change your ip everytime your pc's ip changes.... – Kushan Aug 10 '16 at 08:26
  • When I write 127.0.0.1 it is not reachable. Do I have to install a server like apache ? – hkaraoglu Aug 10 '16 at 08:36
  • yes, tomcat / apache/ xamp / wamp etc... As i said, forwarding your port to your PC's ip is the most imp step... followed by using that ip instead of 127.0.0.1 – Kushan Aug 10 '16 at 08:40
  • I installed xampp today. I can access server from emulator's browser. But it throws same error again when i run from android studio – hkaraoglu Aug 11 '16 at 06:58
  • are you running the app on the emulator or an actual device? – Kushan Aug 11 '16 at 06:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120662/discussion-between-kushan-and-revenge). – Kushan Aug 11 '16 at 07:01