ragesh-ragav 1993 ragesh-ragav 1993 - 1 month ago 28
Java Question

java.lang.NullPointerException in appium

This is my appium java code, after starting the test its stopping in app login method and says java.lang.NullPointerException. I tried all my possible solutions, but its not at all working please help to solve this problem

public class FirstAutomate {
private WebDriver driver;

@Test

public void setup() throws Exception {


File app = new File("C:\\sdk\\platform-tools\\Myapp.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
//capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "5.0.2");
capabilities.setCapability("deviceName","G3 Beat");
capabilities.setCapability("appPackage", "com.skooly.app");
capabilities.setCapability("appActivity","com.skooly.app.screens.prehomescreens.activities.SplashScreen");
capabilities.setCapability("platformName","Android");

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,MobilePlatform.ANDROID);
capabilities.setCapability("app", app.getAbsolutePath());
try{
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

}catch(MalformedURLException e)
{
e.printStackTrace();

}

// TODO Auto-generated method stub

}
@Test

public void AppLogin() throws InterruptedException {
//Tapping button
driver.findElement(By.id("loginButtonTextView")).click();
}
}

Answer

With JUnit, each of your tests, annotated with @Test, is run independently from any other. That's why you cannot set up dome data ("driver") in one @Test and expect it to be there in another @Test.

To do initialization work before each test, use the @Before annotation, as in

@Before public void setup() throws Exception { ...

This will make setup() run and set up your driver before every actual test, i.e. AppLogin() in your case. See for instance JUnit before and test.