Issue
Trying to setup a mobile framework, and when using an element to click it is returning nullpointer and I seem to be overlooking why. mobileAction.click(androidSignIn);
is what is returning as null pointer
ActionsPage:
import io.appium.java_client.MobileBy;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.Dimension;
public class mobileActionsPage extends MobileBaseClass{
/**
* constructor with driver as argument
*
* @param driver
*/
@SuppressWarnings("static-access")
public mobileActionsPage(AppiumDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this); // initialize the objects
}
public void click(WebElement element) {
element.click();
}
}
BaseClass:
package mobilePages;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import io.appium.java_client.AppiumDriver;
public class MobileBaseClass {
public static Logger logg = Logger.getLogger(MobileBaseClass.class.getName());
protected static AppiumDriver driver;
/**
* This function will execute before each Test tag in testng.xml
*
* @param browser
* @throws MalformedURLException
* @throws Exception
*/
@BeforeMethod
@Parameters({ "mobileOS" })
public void setup(String mobileOS) throws MalformedURLException {
if(mobileOS.equalsIgnoreCase("android")) {
//creating android instance
DesiredCapabilities desiredCapabilities= new DesiredCapabilities();
desiredCapabilities.setCapability("deviceName","Pixel_4_API_31");
desiredCapabilities.setCapability("platformName","Android");
desiredCapabilities.setCapability("platformVersion","12");
//THIS WILL CHANGE - Setup app center download on execution
desiredCapabilities.setCapability("app","/usr/local/bin/connect_v5.0.10-249_2022-10-11_15.42.43 .apk");
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
}
}
/**
* This function will execute after each Test tag in testng.xml
*
* @param none
* @throws Exception
*/
@AfterMethod
public void teardown() {
logg.info("****** Test Finished: Beginning teardown ******");
logg.info("Closing browser");
//driver.quit();
}
}
LoginPage:
import java.time.Duration;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import webPages.ActionsPage;
import webPages.AssertPage;
import webPages.BaseClass;
public class LoginPage extends MobileBaseClass{
AppiumDriver driver;
mobileActionsPage mobileAction = new mobileActionsPage(driver);
/**
* constructor with driver as argument
*
* @param driver
*/
@SuppressWarnings("static-access")
public LoginPage(AppiumDriver driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this); // initialize the objects
}
//mobileActionsPage withAction = new mobileActionsPage(driver);
//page objects
// @FindBy(xpath = "//span[contains(text(),'Sign In')]")
// WebElement androidSignIn;
@FindBy(xpath = "//*[@text='Sign In']")
WebElement androidSignIn;
// String signInTitle = "//span[contains(text(),'Sign In')]";
//methods
/**
* Navigates to access page from homepage
*/
public void login() throws InterruptedException {
//withAssert.verifyStringExists(signInTitle, "Sign in button is displayed");
// new WebDriverWait(driver, Duration.ofSeconds(1000));
// logg.info("Waited for 10 seconds");
// WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'Sign In')]"));
// logg.info("Foudn locator");
//
// element.click();
// logg.info("clicking");
Thread.sleep(5000);
mobileAction.click(androidSignIn);
driver.findElement(By.xpath("//android.widget.EditText[1]")).sendKeys("String.com");
driver.findElement(By.xpath("//android.widget.EditText[2]")).sendKeys("String2xxx");
mobileAction.click(androidSignIn);
}
}
Test:
import mobilePages.LoginPage;
import mobilePages.MobileBaseClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
public class FirstAppiumTest extends MobileBaseClass {
LoginPage loginPage = new LoginPage(driver);
@Test
public void test1() throws Exception {
// locate the Text on the calculator by using By.name()
logg.info("Waiting for app to load");
loginPage.login();
}
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite">
<test name="mobileTests">
<parameter name="mobileOS" value="android" />
<classes>
<class name="mobileTests.FirstAppiumTest">
<methods>
<include name="test1" />
</methods>
</class>
</classes>
</test>
</suite>
Pom:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<testng.files.location>src/TestSuites/</testng.files.location>
<testng.file>testng.xml</testng.file>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<gridUrl></gridUrl>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${SuiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-to-slf4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.19.0</version>
</dependency>
</dependencies>
</project>
If I run the test using the following it works fine:
public void click(String xpath) { driver.findElement(By.xpath(xpath)).click(); }
However the same test using this fails using element.click();
Solution
In LoginPage, initialize mobileAction in the constructor after initializing the driver. You are initializing the mobileAction with the null driver.
Answered By - sameeksha sahib
Answer Checked By - Senaida (JavaFixing Volunteer)