0

I am getting NullPointerException everytime I try to run below code at code line element.click()

Note : It passes if I comment the last line of code. Also similar question was asked here but it didn't help.

AndroidDriver driver;
@Test
public void TestAppium() throws Exception{

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "MotoG5s Plus");
    capabilities.setCapability("platformVersion", "7.1.1");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("noReset", "true");

    File file = new File("D:\\Appium Workspace\\AppiumDemo\\apk\\MakeMyTrip Flights Hotels Cabs IRCTC Rail Bookings_v7.3.2_apkpure.com.apk");

    capabilities.setCapability("app", file.getAbsolutePath());

    driver = new AndroidDriver(new URL("http://10.80.196.55:4723/wd/hub"), capabilities);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    Thread.sleep(10000);
    WebElement element = driver.findElementById("com.makemytrip:id/my_profile_icon");
    element.click();

}

Also my driver onject is not null as you can see from below screenshot

enter image description here

Also the element is not null as you can see in screenshot below :-

enter image description here

I have even put the thread.sleep() in case it is because of loading. The ID given in findelementbyid() method is correct. And also it launches the app and then it

It Launches the app and then it throws below error message :-

java.lang.NullPointerException
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
at Appiumcapabilities.TestAppium(Appiumcapabilities.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)

I am using selenium-java-3.9.1 and appium server 1.7.1, testNG Windows 10, appium-java-client version 4.1.0

Pankaj Devrani
  • 510
  • 1
  • 10
  • 28
  • Separate the find code line and click line. Suspect from the stacktrace the NPE is coming from the click part. – Grasshopper Jul 17 '18 at 11:12
  • @Grasshopper You were right. The problem is with click() method I did separated it and even the WebElement is also not null. I am adding screenshot of WebElement object in my question. Please have a look at it. I am not able to understand what is happening – Pankaj Devrani Jul 17 '18 at 11:20
  • Try using .tap() instead of .click() – Bill Hileman Jul 17 '18 at 14:02
  • @BillHileman I am not able to see .tap() method. There is only .click() method present. – Pankaj Devrani Jul 18 '18 at 08:23
  • Please provide HTML source to examine this element, tnx – Kovacic Jul 18 '18 at 10:26
  • You might not be seeing the .Tap method because you're using WebElement instead of AndroidElement or MobileElement. Your driver should be declared with one of those types inside ankle brackets, i.e. AndroidDriver – Bill Hileman Jul 18 '18 at 13:04
  • @BillHileman I also thought this might be the issue, but tested and didn't receive this error – Kovacic Jul 18 '18 at 13:41
  • @BillHileman I tried what you suggested but I am getting this error after doing AndroidDriver org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.android.AndroidElement – Pankaj Devrani Jul 19 '18 at 06:16
  • @Kovacic Can you tell me which Selenium version, Appium Version and Appium java client you are using? – Pankaj Devrani Jul 19 '18 at 06:17
  • Appium client (6.1.0). Selenium (3.13.0), Appium server (1.6.2) – Kovacic Jul 19 '18 at 07:13

3 Answers3

1

Do not set selenium dependency explicitly as appium-java-client dependency already has it in-built: you are running into library incompatibility issue.

If it is necessary to change the version of Selenium then you can configure pom.xml like following:

<dependency>
  <groupId>io.appium</groupId>
  <artifactId>java-client</artifactId>
  <version>${version.you.require}</version>
  <scope>test</test>
  <exclusions>
    <exclusion>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>${selenium.version.you.require}</version>
</dependency>

Same thing can be done with Gradle:

repositories {
    jcenter()
    maven {
        url "http://repo.maven.apache.org/maven2"
    }
}

dependencies {
   ...
   testCompile group: 'io.appium', name: 'java-client', version: requiredVersion {
       exclude module: 'selenium-java'
   }
   
   testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', 
   version: requiredSeleniumVersion
   ...
}   
dmle
  • 3,498
  • 1
  • 14
  • 22
  • I did use appuim-desktop to find element using inspector. And it is giving me the same ID which I have given in my code. But still it doesn't click on it and gives the error mesage – Pankaj Devrani Jul 17 '18 at 11:35
  • Turns out you were right. Somehow it has to do with appium java-client version and selenium version incompatibility. can you please add this point as well, so that I can accept it as an answer. – Pankaj Devrani Jul 19 '18 at 07:49
  • Good to know point 1 was right! I updated answer to have more details on how to configure dependencies in case you still want specific selenium-java library. – dmle Jul 19 '18 at 21:19
0

As per your code block it seems you are using Java as the Selenium Language Binding Art. Now, as per the Java Docs the supported methods of a WebElement are:

Where as findElementById() is associated with the JavaScript Selenium Language Binding Art.

Solution

If you desire to work with the id attribute of an element, you need to replace the line:

WebElement element = driver.findElementById("com.makemytrip:id/my_profile_icon");

By:

WebElement element = driver.findElement(By.id("com.makemytrip:id/my_profile_icon"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Maybe issue to this problem is that it can't find Your package or activity, try putting these desired capsabilities:

appActivity = Activity name for the Android activity you want to wait for (eg. "MainActivity, .Settings")

appPackag = Java package of the Android app you want to run (eg. "com.example.android.myApp, com.android.settings")

appWaitActivity = Activity name for the Android activity you want to wait for (eg. Splash)

It might be that You did everything OK, but wrong activity is shown.

Good practice is to put appPackage, and appActivity.

Hope this helps,

P.S.: and please also implement solution/answer from @dmle, it also could cause this kind of issues.

Kovacic
  • 1,473
  • 6
  • 21