0

I am getting 'Null pointer error' while executing script. But app is getting launched properly on Geny motion but fails to perform further action like clicking on 'Skip user' button. Below is the code I have written for it

public class mainProgram {
    public static AndroidDriver driver;

  @Test
  public void skipUser() { 
  driver.findElement(By.id("com.hp.pregnancy.lite:id/btn_join_later")).click();
  }

  @BeforeMethod
  public void beforeMethod() {
      try {
            File app = new File("D:\\Automation builds\\Testbuild.apk");   
            DesiredCapabilities caps = new DesiredCapabilities();     
            caps.setCapability("deviceName", "Google Nexus 6P - 7.1.0 - API 25 - 1440x2560");
            caps.setCapability("udid", "192.168.250.101:5555"); //Give Device ID of your mobile phone
            caps.setCapability("platformName", "android");
            caps.setCapability("platformVersion", "7.1.0");
            caps.setCapability("appPackage", "com.hp.pregnancy.lite");
            caps.setCapability("appActivity", "com.hp.pregnancy.lite.onboarding.SplashScreenActivity");
            caps.setCapability("noReset", "true");
            caps.setCapability("autoAcceptAlerts", "true");
            caps.setCapability("autoDismissAlerts", "true");
        //  caps.setCapability("fullReset",false);
            caps.setCapability("app", app.getAbsolutePath()); 

        Androiddriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

            System.out.println("Pregnancy+ application launched successfully on Genymotion");
            Thread.sleep(5000);
            } catch (Exception e) {
                System.out.println("Step failed - Unable to setup with the predefined Capabilities");
            }
  }

  @AfterMethod
  public void afterMethod() {
      System.out.println("afterMethod");
  }
}
3
  • at which line you are getting NullPointerException? Commented Mar 15, 2019 at 10:01
  • Please show the console error or error snippet Commented Mar 15, 2019 at 10:01
  • ** NullPointerException** error is gone now but new error org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed is getting displayed...I have updated the code after correction. Could you please help with the latest error Commented Mar 15, 2019 at 12:01

1 Answer 1

1

You have used two drivers in your code. One is at the global level which you have declared as public static and one you have used and initialised in your @BeforeMethod.
So, as the driver inside the @BeforeMethod is initialised by using AndroidDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps); it is working fine and opening the app. But after that, your code goes to the @Test where the driver you are using is the global one and that driver is not initialised and that's why you are getting NullPointerException

To solve this problem, please use the same AndroidDriver driver in both the @BeforeMethod and @Test

So, you just need to use driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps); (which would be initialising the global driver and would be used in your @Test) in your @BeforeMethod instead of making another AndroidDriver driver there, like you were doing by using AndroidDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

Sign up to request clarification or add additional context in comments.

9 Comments

Could please mention how it should be done? As I am new to it. I have updated it but not working
@AressQa Just use driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps); after your line of code caps.setCapability("app", app.getAbsolutePath()); and it would work as everything else is fine in your code
Thanks for quick reply. The error is gone but new error occurred.
@AressQa What is the error that you are getting now ?
org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.