Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- master
paths:
- 'docs/**'
- 'docs-snippets/**'
- 'mkdocs.yml'
- 'gradle/docs.gradle'
- '.github/workflows/docs.yml'
Expand All @@ -15,6 +16,7 @@ on:
- master
paths:
- 'docs/**'
- 'docs-snippets/**'
- 'mkdocs.yml'
- 'gradle/docs.gradle'
- '.github/workflows/docs.yml'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ classes/

# MkDocs / mike scratch output
/site/
/docs/api/
26 changes: 26 additions & 0 deletions docs-snippets/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Compile-only code samples that the docs include (pymdownx.snippets). Nothing here is published or run:
// an API change that breaks an example fails the build of the PR that made it.
plugins {
id 'java-library'
}

evaluationDependsOn(':')

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

def seleniumVersion = rootProject.ext.seleniumVersion

dependencies {
implementation project(':')
implementation project(':selenium-bridge')
// EventFiringDecorator and the other selenium-support classes the docs mention
implementation "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
}

tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.compilerArgs += ['-Xlint:deprecation', '-Xlint:removal']
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package io.appium.java_client.docs;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.proxy.ElementAwareWebDriverListener;
import io.appium.java_client.proxy.MethodCallListener;
import io.appium.java_client.proxy.NotImplementedException;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.EventFiringDecorator;
import org.openqa.selenium.support.events.WebDriverListener;

import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.util.List;

import static io.appium.java_client.proxy.Helpers.createProxy;

class EventListenerSnippets {
// The listener the docs refer to; it overrides only the events it needs
static class MyListener implements WebDriverListener {
@Override
public void beforeGet(WebDriver driver, String url) {
System.out.println("Opening " + url);
}
}

static void eventFiringDecorator(URL serverUrl, UiAutomator2Options options) {
// --8<-- [start:event-firing-decorator]
WebDriver original = new AndroidDriver(serverUrl, options);
// MyListener implements WebDriverListener or its descendant
WebDriverListener listener = new MyListener();
WebDriver decorated = new EventFiringDecorator<>(listener).decorate(original);
// Fires, in this order: beforeAnyCall, beforeAnyWebDriverCall, beforeGet,
// afterGet, afterAnyWebDriverCall, afterAnyCall
decorated.get("http://example.com/");
// Fires, in this order: beforeAnyCall, beforeAnyWebDriverCall, beforeFindElement,
// afterFindElement, afterAnyWebDriverCall, afterAnyCall
WebElement header = decorated.findElement(By.tagName("h1"));
// If any of these calls throws, the onError event is fired
// --8<-- [end:event-firing-decorator]
}

static void classTypeDoesNotWork() throws Exception {
// --8<-- [start:class-type-cast]
IOSDriver driver = new IOSDriver(URI.create("http://doesnot.matter/").toURL(), new ImmutableCapabilities()) {
@Override
protected void startSession(Capabilities capabilities) {
// Skip the actual session start to keep the example short
}
};
WebDriverListener webDriverListener = new WebDriverListener() {
};
// Throws ClassCastException: the decorated object is a proxy of the WebDriver interfaces, not an IOSDriver
IOSDriver decoratedDriver = (IOSDriver) new EventFiringDecorator<>(webDriverListener).decorate(driver);
// --8<-- [end:class-type-cast]
}

static void createProxyWithListener() throws Exception {
// --8<-- [start:create-proxy]
StringBuilder acc = new StringBuilder();
MethodCallListener listener = new MethodCallListener() {
@Override
public void beforeCall(Object target, Method method, Object[] args) {
if (!method.getName().equals("get")) {
// The callback is skipped for this method
throw new NotImplementedException();
}
acc.append("beforeCall ").append(method.getName()).append("\n");
}

@Override
public void afterCall(Object target, Method method, Object[] args, Object result) {
if (!method.getName().equals("get")) {
throw new NotImplementedException();
}
acc.append("afterCall ").append(method.getName()).append("\n");
}
};

IOSDriver decoratedDriver = createProxy(
IOSDriver.class,
new Object[] {URI.create("http://localhost:4723/").toURL(), new XCUITestOptions()},
new Class[] {URL.class, Capabilities.class},
listener
);

decoratedDriver.get("http://example.com/");
// acc now contains "beforeCall get" and "afterCall get", one per line
// --8<-- [end:create-proxy]
}

static void elementAwareListener() throws Exception {
// --8<-- [start:element-aware-listener]
StringBuilder acc = new StringBuilder();
var listener = new ElementAwareWebDriverListener() {
@Override
public void beforeCall(Object target, Method method, Object[] args) {
acc.append("beforeCall ").append(method.getName()).append("\n");
}
};

IOSDriver decoratedDriver = createProxy(
IOSDriver.class,
new Object[] {URI.create("http://localhost:4723/").toURL(), new XCUITestOptions()},
new Class[] {URL.class, Capabilities.class},
listener
);

WebElement element = decoratedDriver.findElement(By.id("button"));
element.click();

List<WebElement> elements = decoratedDriver.findElements(By.id("button"));
elements.get(1).isSelected();
// The listener also saw the calls on the elements (click, isSelected), not only on the driver
// --8<-- [end:element-aware-listener]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.appium.java_client.docs;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.selenium.SeleniumBridge;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.module.LogInspector;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

class SeleniumBridgeSnippets {
static void remoteWebDriver(URL serverUrl, UiAutomator2Options options) {
// --8<-- [start:remote-web-driver]
var driver = new AndroidDriver(serverUrl, options);
RemoteWebDriver selenium = SeleniumBridge.asRemoteWebDriver(driver);
WebDriver augmented = new Augmenter().augment(selenium);
// --8<-- [end:remote-web-driver]
}

static void bidiLogs(URL serverUrl, UiAutomator2Options options) {
// --8<-- [start:bidi-logs]
var driver = new AndroidDriver(serverUrl, options.enableBiDi());
var selenium = SeleniumBridge.asRemoteWebDriver(driver);
try (var logInspector = new LogInspector(selenium)) {
logInspector.onGenericLog(entry -> System.out.println(entry.getText()));
driver.getPageSource();
} finally {
selenium.closeBiDi();
}
// --8<-- [end:bidi-logs]
}

static void migrateFromLogsBroadcast(URL serverUrl, UiAutomator2Options options) {
// --8<-- [start:syslog-listener]
var driver = new AndroidDriver(serverUrl, options.enableBiDi());
var selenium = SeleniumBridge.asRemoteWebDriver(driver);
try (var logInspector = new LogInspector(selenium)) {
logInspector.onGenericLog(entry -> {
if ("syslog".equals(entry.getType())) {
System.out.println(entry.getText());
}
});
// ...
} finally {
selenium.closeBiDi();
}
// --8<-- [end:syslog-listener]
}
}
20 changes: 20 additions & 0 deletions docs/Note-for-developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ cache (and fetches Python if needed), so there is no virtual environment to mana
Pull requests that touch the docs run this check.
- Use `-PdocsPython=3.13` (or a path) to pick the Python interpreter, `-PdocsUv=/path/to/uv` if `uv` is not on the PATH.
- Link to sources and other repository files with absolute `https://github.com/appium/java-client/blob/master/...` URLs.

## Code samples

Multi-line Java examples are compiled, so that an API change breaks the build instead of the docs:

1. Put the example in a class under `docs-snippets/src/main/java/io/appium/java_client/docs/` and wrap the lines
to show in section markers: `// --8<-- [start:name]` and `// --8<-- [end:name]` (the markers and the indentation
are removed in the page).
1. Include it in a page: a `java` fence containing the single line `--8<-- "MyClass.java:name"`.
1. `./gradlew docsCheck` compiles the samples and fails for a new multi-line `java` block that is not an include.
The pages that still have inline blocks are listed in `gradle/docs-legacy-inline-code.txt`; remove a page from
the list when you rewrite it, and do not add new ones.

## API reference

`./gradlew docsApi` generates the merged Javadoc of the published modules into `docs/api` (not committed); the site
build and `docsServe` run it first, so every docs version contains the Javadoc of its own code. Link to it with a
relative URL, for example ``[`AppiumDriver`](api/io/appium/java_client/AppiumDriver.html)``. `docsCheck` fails if

@eglitise eglitise Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these links should lead to HTML. Zensical recommends linking to the Markdown file (https://zensical.org/docs/authoring/markdown/#linking-between-pages) though I'm not sure such files would exist for the API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc is generated HTML (docs/api is built by docsApi and git-ignored), so there are no Markdown sources to link to. The Markdown-link recommendation only applies between Markdown pages, and strict mode would not resolve .md targets for the API. That is why docsCheckApiLinks validates these .html links against the generated tree instead. Keeping them as relative .html links.

such a link points to a page that does not exist. A new published module has to be added to `apiModules` in
`gradle/docs.gradle`.
Loading
Loading