This is the Java language bindings for writing Appium Tests that conform to WebDriver Protocol
Since version 8 Appium Java Client had several major changes, which might require to update your client code. Make sure to follow the v7 to v8 Migration Guide in order to streamline the migration process.
Add the following to pom.xml:
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${version.you.require}</version>
<scope>test</scope>
</dependency>
Add the following to build.gradle:
dependencies {
testImplementation 'io.appium:java-client:${version.you.require}'
}
Java client project is available to use even before it is officially published to maven central. Refer jitpack.io
Add the following to pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add the dependency:
<dependency>
<groupId>com.github.appium</groupId>
<artifactId>java-client</artifactId>
<version>latest commit ID from master branch</version>
</dependency>
Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.appium:java-client:latest commit id from master branch'
}
Appium Java Client | Selenium client |
---|---|
8.5.0 , 8.5.1 |
4.9.1 , 4.10.0 |
8.4.0 |
4.8.2 , 4.8.3 , 4.9.0 |
8.3.0 |
4.7.0 , 4.7.1 , 4.7.2 , 4.8.0 , 4.8.1 |
8.2.1 |
4.5.0 , 4.5.1 , 4.5.2 , 4.5.3 , 4.6.0 |
Selenium client does not follow Semantic Versioning, so breaking changes might be introduced even in patches, which requires Appium team to update Java client in response.
Appium Java Client declares Selenium dependencies using open version range which is handled in differently by different build tools. Sometimes users may want to pin used Selenium dependencies for various reasons. Follow the Transitive Dependencies Management article for more information about establishing a fixed Selenium version for your Java test framework.
Appium java client has dedicated classes to support the following Appium drivers:
- UiAutomator2 and Espresso: AndroidDriver
- XCUITest: IOSDriver
- Windows: WindowsDriver
- Safari: SafariDriver
- Gecko: GeckoDriver
- Mac2: Mac2Driver
To automate other platforms that are not listed above you could use AppiumDriver or its custom derivatives.
Appium java client is built on top of Selenium and implements same interfaces that the foundation
RemoteWebDriver
does. However, Selenium lib is mostly focused on web browsers automation while
Appium is universal and covers wide range of possible platforms, e.g. mobile and desktop
operating systems, IOT devices, etc. Thus, the foundation AppiumDriver
class in this package
extends RemoteWebDriver
with additional features, and makes it more flexible, so it is not so
strictly focused on web-browser related operations.
Appium java client provides a dedicated class to control Appium server execution. The class is AppiumDriverLocalService. It allows to run and verify the Appium server locally from your test framework code and provides several convenient shortcuts. The service could be used as below:
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
service.start();
try {
// do stuff with drivers
} finally {
service.stop();
}
You could customize the service behavior, for example, provide custom command line arguments or change paths to server executables using AppiumServiceBuilder
Note
AppiumDriverLocalService does not support the server management on non-local hosts
UiAutomator2Options options = new UiAutomator2Options()
.setUdid('123456')
.setApp("/home/myapp.apk");
AndroidDriver 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
);
try {
WebElement el = driver.findElement(AppiumBy.xpath, "//Button");
el.click();
driver.getPageSource();
} finally {
driver.quit();
}
XCUITestOptions options = new XCUITestOptions()
.setUdid('123456')
.setApp("/home/myapp.ipa");
IOSDriver driver = new IOSDriver(
// The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
new URL("http://127.0.0.1:4723"), options
);
try {
WebElement el = driver.findElement(AppiumBy.accessibilityId, "myId");
el.click();
driver.getPageSource();
} finally {
driver.quit();
}
BaseOptions options = new BaseOptions()
.setPlatformName("myplatform")
.setAutomationName("mydriver")
.amend("mycapability1", "capvalue1")
.amend("mycapability2", "capvalue2");
AppiumDriver driver = new AppiumDriver(
// The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
new URL("http://127.0.0.1:4723"), options
);
try {
WebElement el = driver.findElement(AppiumBy.className, "myClass");
el.click();
driver.getPageSource();
} finally {
driver.quit();
}
Check the corresponding driver's READMEs to know the list of capabilities and features it supports.
You could find much more code examples by checking client's unit and integration tests.
Appium Java client uses reflective access to private members of other modules
to ensure proper functionality of several features, like Page Object model.
If you get a runtime exception and InaccessibleObjectException
is present in
the stacktrace, and your Java runtime is at version 16 or higher, then consider following
Oracle's tutorial
and/or checking existing issues
for possible solutions. Basically, the idea there would be to explicitly allow
access for particular modules using --add-exports/--add-opens
command line arguments.
Another possible, but weakly advised solution, would be to downgrade Java to version 15 or lower.
Such issues are usually the case when Appium server is started directly from your framework code rather than run separately by a script or manually. Depending on the way the server process is started it may or may not inherit the currently active shell environment. That is why you may still receive errors about variables presence even though these variables ar actually defined for your command line interpreter. Again, there is no universal solution to that, as there are many ways to spin up a new server process. Consider checking the Appium Environment Troubleshooting document for more information on how to debug and fix process environment issues.
Visit CHANGELOG.md to see the full list of changes between versions.
Run a test using
gradle clean -Dtest.single=IOSAlertTest test