Hi I’m a uni student doing my dissertation on Appium. I’m fairly new to it and I have been struggling to get a test to pass for months. When I run the following test, the driver isn’t initialised and is set to null. I don’t know what I’m doing wrong.
AppiumTest.java:
package com.example.singleactionapp;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.WebElement;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.net.URL;
public class AppiumTest {
public static AndroidDriver driver;
@BeforeEach
public void setup () throws Exception {
UiAutomator2Options options = new UiAutomator2Options()
.setUdid("emulator-5554")
.setDeviceName("Pixel 7 API 34")
.setApp("/SingleActionApp.apk");
driver = new AndroidDriver(
// The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
new URL("http://127.0.0.1:4723"), options
);
}
@Test
public void ButtonTest () {
System.out.println("In Test Case ButtonTest");
System.out.println("Driver:" + driver);
WebElement button = driver.findElement(AppiumBy.id("buttonID"));
button.click();
WebElement message = driver.findElement(AppiumBy.id("messageID"));
Assert.assertEquals(message.getText(), "Button has been pressed 1 time");
}
@AfterEach
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
I’ve attached the build.gradle file below as well just in case.
I only get the default information after starting the appium server. To my understanding, there is absolutely no connection between the test file and the server…
[Appium] Welcome to Appium v2.5.1
...
[Appium] AndroidUiautomator2Driver has been successfully loaded in 2.968s
[Appium] XCUITestDriver has been successfully loaded in 2.968s
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
[Appium] http://127.0.0.1:4723/ (only accessible from the same host)
[Appium] http://10.5.31.155:4723/
[Appium] http://10.5.61.34:4723/
[Appium] http://169.254.203.235:4723/
[Appium] Available drivers:
[Appium] - [email protected] (automationName 'UiAutomator2')
[Appium] - [email protected] (automationName 'XCUITest')
[Appium] Available plugins:
[Appium] - [email protected]
tried replace new URL("http://127.0.0.1:4723") → new URL("http://localhost:4723") ?
[Appium] Welcome to Appium v2.5.4
...
[Appium] EspressoDriver has been successfully loaded in 5.018s
[Appium] AndroidUiautomator2Driver has been successfully loaded in 5.019s
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
[Appium] http://127.0.0.1:4723/ (only accessible from the same host)
[Appium] http://10.121.6.29:4723/
[Appium] Available drivers:
[Appium] - [email protected] (automationName 'UiAutomator2')
[Appium] - [email protected] (automationName 'Espresso')
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
I tried replacing 127.0.0.1 to localhost but that didn’t make a difference
I updated UIAutomator2 and got the same message when running http://127.0.0.1:4723/status - thank you for that!
I have Android 14, API 34 already installed.
I’ve just added my code again because I made a few adjustments:
package com.example.singleactionapp;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebElement;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.net.URL;
public class AppiumTest {
public AndroidDriver driver;
@BeforeAll
public void setup () throws Exception {
UiAutomator2Options options = new UiAutomator2Options()
.setUdid("emulator-5554")
.setDeviceName("Pixel 7 API 34")
.setApp("/AndroidStudioProjects/SingleActionApp.apk");
driver = new AndroidDriver(
// The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
new URL("http://localhost:4723"), options
);
}
@Test
public void ButtonTest () {
System.out.println("In Test Case ButtonTest");
System.out.println("Driver:" + driver);
WebElement button = driver.findElement(AppiumBy.id("buttonID"));
button.click();
WebElement message = driver.findElement(AppiumBy.id("messageID"));
Assert.assertEquals(message.getText(), "Button has been pressed 1 time");
}
@AfterAll
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
and this is the error message I keep getting:
java.lang.NullPointerException: Attempt to invoke virtual method 'org.openqa.selenium.WebElement io.appium.java_client.android.AndroidDriver.findElement(org.openqa.selenium.By)' on a null object reference
at com.example.singleactionapp.AppiumTest.ButtonTest(AppiumTest.java:35)
The print statement System.out.println("Driver:" + driver); prints out Driver:null