Setup Class
public class Setup {
private static AndroidDriver driver = initializeAppiumServer();
public static AndroidDriver initializeAppiumServer() {
if (driver == null || driver.toString().contains("(null)")) {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "HXT7Nwewewe9");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
desiredCapabilities.setCapability("appPackage", "com.abc.abc");
desiredCapabilities.setCapability("appActivity",
"com.abc.abc.ui.activity.SplashActivity");
desiredCapabilities.setCapability("noReset", true);
try {
driver = new AndroidDriver(new URL("http://0.2.0.3:7725/wd/hub"), desiredCapabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println("Initializing Appium Server");
return driver;
} else {
return driver;
}
}
}
Test class
public class LoginTest {
private LoginPage loginpage;
private DashboardPage dashboardPage;
public LoginTest() {
loginpage = new LoginPage();
dashboardPage = new DashboardPage();
}
@Given("^Launch app$")
public void Launchapp() {
loginpage.launchapp();
}
@Then("^Get logged in$")
public void Getloggedin() {
loginpage.Login();
dashboardPage.navigate(); //calling from Dashboard page
}
LoginPage Class
public class LoginPage{
private AndroidDriver driver;
public void launchapp() {
driver = new Setup().initializeAppiumServer();
}
public void Login() {
driver.findElement(Usernametxt).sendKeys("ABC");
driver.findElement(Passwordtxt).sendKeys("ABC")
driver.findElement(Loginbtn).click();
}
DashboardPage Class
public class DashboardPage {
private AndroidDriver driver;
// Where fails
public void navigate() {
By settingsbtn = By.id("Button");
driver.findElement(settingsbtn).click();
}
}
When I run I'm getting java.lang.NullPointerException for DashboardPage navigate()function, But all works fine if i add the function to the login page. Is this something to do with the driver? How to solve this?
-
Can you paste the stacktrace of the NPE?João Farias– João Farias2020年01月23日 13:10:01 +00:00Commented Jan 23, 2020 at 13:10
1 Answer 1
1. you are not passing any driver instance to the Dashboard DashboardPage
2. You are trying to access local variable 'driver' which is not initialized yet
Solution:
Add :
public void launchapp() {
driver = new Setup().initializeAppiumServer();
}
Or pass the already initialized driver to the dashboard:
in your dashboard class also or pass the driver object to dashboard class before trying to find element.
Correct approach
Test Class::
public class LoginTest {
private LoginPage loginpage;
private DashboardPage dashboardPage;
private AndroidDriver driver;
public LoginTest() {
driver= Setup().initializeAppiumServer()
loginpage = new LoginPage(driver);
dashboardPage = new DashboardPage(driver);
}
@Given("^Launch app$")
public void Launchapp() {
loginpage.launchapp();
}
@Then("^Get logged in$")
public void Getloggedin() {
loginpage.Login();
dashboardPage.navigate(); //calling from Dashboard page
}
Login Class
public class LoginPage{
private AndroidDriver driver;
public LoginPage(driverInst) {
driver = driverInst;
}
public void launchapp() {
driver.launchApp();
}
public void Login() {
driver.findElement(Usernametxt).sendKeys("ABC");
driver.findElement(Passwordtxt).sendKeys("ABC")
driver.findElement(Loginbtn).click();
}
Dashboard class:
public class DashboardPage {
private AndroidDriver driver;
public DashboardPage(driverInst) {
driver = driverInst;
}
// Where fails
public void navigate() {
By settingsbtn = By.id("Button");
driver.findElement(settingsbtn).click();
}
}
Explore related questions
See similar questions with these tags.