diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 3cf8912ff..bf54dcae9 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -6,6 +6,7 @@ on: - master paths: - 'docs/**' + - 'docs-snippets/**' - 'mkdocs.yml' - 'gradle/docs.gradle' - '.github/workflows/docs.yml' @@ -15,6 +16,7 @@ on: - master paths: - 'docs/**' + - 'docs-snippets/**' - 'mkdocs.yml' - 'gradle/docs.gradle' - '.github/workflows/docs.yml' diff --git a/.gitignore b/.gitignore index 1bd7dc0db..7a775756a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ classes/ # MkDocs / mike scratch output /site/ +/docs/api/ diff --git a/docs-snippets/build.gradle b/docs-snippets/build.gradle new file mode 100644 index 000000000..ab2a921b5 --- /dev/null +++ b/docs-snippets/build.gradle @@ -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'] +} diff --git a/docs-snippets/src/main/java/io/appium/java_client/docs/EventListenerSnippets.java b/docs-snippets/src/main/java/io/appium/java_client/docs/EventListenerSnippets.java new file mode 100644 index 000000000..02a90066b --- /dev/null +++ b/docs-snippets/src/main/java/io/appium/java_client/docs/EventListenerSnippets.java @@ -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 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] + } +} diff --git a/docs-snippets/src/main/java/io/appium/java_client/docs/SeleniumBridgeSnippets.java b/docs-snippets/src/main/java/io/appium/java_client/docs/SeleniumBridgeSnippets.java new file mode 100644 index 000000000..8807bee38 --- /dev/null +++ b/docs-snippets/src/main/java/io/appium/java_client/docs/SeleniumBridgeSnippets.java @@ -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] + } +} diff --git a/docs/Note-for-developers.md b/docs/Note-for-developers.md index 27e6906fb..a41a5f5c4 100644 --- a/docs/Note-for-developers.md +++ b/docs/Note-for-developers.md @@ -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 +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`. diff --git a/docs/The-event_firing.md b/docs/The-event_firing.md index 5c7570b3d..8c8093775 100644 --- a/docs/The-event_firing.md +++ b/docs/The-event_firing.md @@ -12,6 +12,9 @@ There are two main entities used to implement events firing logic: - [org.openqa.selenium.support.events.EventFiringDecorator](https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/support/events/EventFiringDecorator.java) class - [org.openqa.selenium.support.events.WebDriverListener](https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/support/events/WebDriverListener.java) interface +Both classes are part of the Selenium `selenium-support` artifact. The Appium Java client depends on +`selenium-api` only, so add `org.seleniumhq.selenium:selenium-support` to your project to use them. + ## WebDriverListener Classes that implement this interface are intended to be used with EventFiringDecorator. @@ -42,30 +45,7 @@ decorate the original WebDriver instance with this decorator and use the new Web created by the decorator instead of the original one: ```java -WebDriver original = new AndroidDriver(); -// it is expected that MyListener class implements WebDriverListener -// interface or its descendant -WebDriverListener listener = new MyListener(); -WebDriver decorated = new EventFiringDecorator(listener).decorate(original); -// the next call is going to fire: -// - beforeAnyCall -// - beforeAnyWebDriverCall -// - beforeGet -// - afterGet -// - afterAnyWebDriverCall -// - afterAnyCall -// events in the listener instence (in this order) -decorated.get("http://example.com/"); -// the next call is going to fire: -// - beforeAnyCall -// - beforeAnyWebDriverCall -// - beforeFindElement -// - afterFindElement -// - afterAnyWebDriverCall -// - afterAnyCall -// events in the listener instence (in this order) -WebElement header = decorated.findElement(By.tagName("h1")); -// if an error happens during any of these calls the the onError event is fired +--8<-- "EventListenerSnippets.java:event-firing-decorator" ``` The instance of WebDriver created by the decorator implements all the same interfaces @@ -85,68 +65,18 @@ because it can only use interface types for decorator objects. For example, the work: ```java -IOSDriver driver = new IOSDriver(new URL("http://doesnot.matter/"), new ImmutableCapabilities()) -{ - @Override - protected void startSession(Capabilities capabilities) - { - // Override in a sake of simplicity to avoid the actual session start - } -}; -WebDriverListener webDriverListener = new WebDriverListener() -{ -}; -IOSDriver decoratedDriver = (IOSDriver) new EventFiringDecorator(IOSDriver.class, webDriverListener).decorate( - driver); +--8<-- "EventListenerSnippets.java:class-type-cast" ``` -The last line throws `ClassCastException` because `decoratedDriver` is of type `IOSDriver`, -which is a class rather than an interface. +The last line throws `ClassCastException`: the object returned by the decorator is a proxy that implements the +interfaces of the driver (`WebDriver`, `HasCapabilities` and so on), but it is not a subclass of `IOSDriver`, +which is a class rather than an interface. See the issue [#1694](https://github.com/appium/java-client/issues/1694) for more details. In order to workaround this limitation a special proxy implementation has been created, which is capable of decorating class types: ```java -import io.appium.java_client.proxy.MethodCallListener; -import io.appium.java_client.proxy.NotImplementedException; - -import static io.appium.java_client.proxy.Helpers.createProxy; - -// ... - -MethodCallListener listener = new MethodCallListener() { - @Override - public void beforeCall(Object target, Method method, Object[] args) { - if (!method.getName().equals("get")) { - 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[] {new URL("http://localhost:4723/"), new XCUITestOptions()}, - new Class[] {URL.class, Capabilities.class}, - listener -); - -decoratedDriver.get("http://example.com/"); - -assertThat(acc.toString().trim()).isEqualTo( - String.join("\n", - "beforeCall get", - "afterCall get" - ) -); +--8<-- "EventListenerSnippets.java:create-proxy" ``` This proxy is not tied to WebDriver descendants and could be used to any classes that have @@ -157,57 +87,12 @@ Check [unit tests](https://github.com/appium/java-client/blob/master/src/test/ja #### ElementAwareWebDriverListener -A specialized MethodCallListener that listens to all method calls on a WebDriver instance and automatically wraps any returned RemoteWebElement (or list of elements) with a proxy. This enables your listener to intercept and react to method calls on both: +A specialized [`MethodCallListener`](api/io/appium/java_client/proxy/MethodCallListener.html) that listens to all method calls on a WebDriver instance and automatically wraps any returned `AppiumWebElement` (or list of elements) with a proxy. This enables your listener to intercept and react to method calls on both: - The driver itself (e.g., findElement, getTitle) - Any elements returned by the driver (e.g., click, isSelected on a WebElement) ```java -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.Helpers; -import io.appium.java_client.proxy.MethodCallListener; - - -// ... - -final 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[]{new URL("http://localhost:4723/"), new XCUITestOptions()}, - new Class[]{URL.class, Capabilities.class}, - listener -); - -WebElement element = decoratedDriver.findElement(By.id("button")); -element::click; - -List elements = decoratedDriver.findElements(By.id("button")); -elements.get(1).isSelected(); - -assertThat(acc.toString().trim()).isEqualTo( - String.join("\n", - "beforeCall findElement", - "beforeCall click", - "beforeCall getSessionId", - "beforeCall getCapabilities", - "beforeCall getCapabilities", - "beforeCall findElements", - "beforeCall isSelected", - "beforeCall getSessionId", - "beforeCall getCapabilities", - "beforeCall getCapabilities" - ) -); - +--8<-- "EventListenerSnippets.java:element-aware-listener" ``` diff --git a/docs/index.md b/docs/index.md index 81c86be8f..ef4a50a93 100644 --- a/docs/index.md +++ b/docs/index.md @@ -5,7 +5,7 @@ Java language bindings for writing [Appium](https://appium.io) tests that confor - Installation, compatibility matrix and quick start: see the [README](https://github.com/appium/java-client#readme). -- API reference: [Javadoc](https://www.javadoc.io/doc/io.appium/java-client). +- API: [Javadoc](https://www.javadoc.io/doc/io.appium/java-client). - Upgrading between major versions: see the **Migration** section. - Changes between versions: [CHANGELOG](https://github.com/appium/java-client/blob/master/CHANGELOG.md). diff --git a/docs/selenium-bridge.md b/docs/selenium-bridge.md index 9e776ef40..442872a13 100644 --- a/docs/selenium-bridge.md +++ b/docs/selenium-bridge.md @@ -14,16 +14,14 @@ dependencies { ## RemoteWebDriver -`SeleniumBridge.asRemoteWebDriver` returns a Selenium `RemoteWebDriver` that works in the session of the Appium +[`SeleniumBridge.asRemoteWebDriver`](api/io/appium/java_client/selenium/SeleniumBridge.html) returns a Selenium `RemoteWebDriver` that works in the session of the Appium driver. Both share the same session and the same HTTP client, so commands can be mixed freely. As long as the driver still has a session, the result is referenced, and its BiDi connection is not closed, the same instance is returned for the same Appium driver. Quitting either driver ends the session; a subsequent `asRemoteWebDriver` call evicts any remaining cached bridge and throws `IllegalStateException`. ```java -var driver = new AndroidDriver(serverUrl, options); -RemoteWebDriver selenium = SeleniumBridge.asRemoteWebDriver(driver); -WebDriver augmented = new Augmenter().augment(selenium); +--8<-- "SeleniumBridgeSnippets.java:remote-web-driver" ``` Elements are not interchangeable between the two drivers: an element that one driver found cannot be passed to @@ -39,14 +37,7 @@ credentials, SSL context and HTTP version of the `AppiumClientConfig` of the dri BiDi modules fail with a `BiDiException`. ```java -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<-- "SeleniumBridgeSnippets.java:bidi-logs" ``` The connection stays open until `closeBiDi()` or `quit()`, so release it when you are done with the BiDi modules. @@ -64,18 +55,7 @@ they are based on, are deprecated, as the Appium drivers deprecated the `mobile: connection of the session, so there is no need to know the host and the port of the Appium server. ```java -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<-- "SeleniumBridgeSnippets.java:syslog-listener" ``` The `type` of a log entry tells the source of the log. Filter by `syslog` (used for both the Android logcat diff --git a/gradle/docs-legacy-inline-code.txt b/gradle/docs-legacy-inline-code.txt new file mode 100644 index 000000000..3a5b651bb --- /dev/null +++ b/gradle/docs-legacy-inline-code.txt @@ -0,0 +1,6 @@ +# Pages that still contain multi-line ```java blocks that are not compiled snippets. +# Remove a page from this list when it is rewritten to use docs-snippets (Phase 3); never add new pages. +Advanced-By.md +Page-objects.md +The-starting-of-an-app-using-Appium-node-server-started-programmatically.md +v10-to-v11-migration-guide.md diff --git a/gradle/docs.gradle b/gradle/docs.gradle index fa59fecd4..9f9422ab7 100644 --- a/gradle/docs.gradle +++ b/gradle/docs.gradle @@ -16,15 +16,103 @@ def uvRun = { List command -> cmd + command } +// Javadoc of all published modules in one tree, generated into docs/api (git-ignored) so that every +// docs version carries the API reference of its own code. Add new modules to `apiModules`. +def apiModules = [project(':'), project(':selenium-bridge')] +tasks.register('docsApi', Javadoc) { + group = 'documentation' + description = 'Generates the merged Javadoc into docs/api (the API reference of the site).' + apiModules.each { dependsOn it.tasks.named('classes') } + source = files(apiModules.collect { it.sourceSets.main.allJava }) + classpath = files(apiModules.collect { it.sourceSets.main.compileClasspath }) + + files(apiModules.collect { it.sourceSets.main.output }) + destinationDir = file('docs/api') + title = 'Appium Java Client API' + options.addStringOption('encoding', 'UTF-8') + options.addBooleanOption('notimestamp', true) // keeps the docs-site history small and diffs stable + options.addBooleanOption('Xwerror', true) +} + +// Multi-line Java in the docs must be a compiled snippet (docs-snippets), so that API changes break the build +// instead of the examples. Pages that predate this rule are listed in gradle/docs-legacy-inline-code.txt. +tasks.register('docsCheckInlineCode') { + group = 'documentation' + description = 'Fails if a docs page outside the legacy list has an inline multi-line ```java block.' + def docsDir = file('docs') + def legacyFile = file('gradle/docs-legacy-inline-code.txt') + inputs.files(fileTree(docsDir) { include '**/*.md'; exclude 'api/**' }) + inputs.file(legacyFile) + doLast { + def legacy = legacyFile.readLines().collect { it.trim() }.findAll { it && !it.startsWith('#') } as Set + def violations = [] + docsDir.eachFileRecurse { f -> + if (!f.name.endsWith('.md') || legacy.contains(docsDir.toPath().relativize(f.toPath()).toString())) { + return + } + def inJava = false + def start = 0 + def body = [] + f.readLines().eachWithIndex { line, i -> + if (!inJava && line.trim().startsWith('```java')) { + inJava = true + start = i + 1 + body = [] + } else if (inJava && line.trim().startsWith('```')) { + inJava = false + def isInclude = body.size() == 1 && body[0].trim().startsWith('--8<--') + if (body.size() > 3 && !isInclude) { + violations << "${docsDir.toPath().relativize(f.toPath())}:${start}: ${body.size()} lines" + } + } else if (inJava) { + body << line + } + } + } + if (violations) { + throw new GradleException('Multi-line Java blocks must be included from docs-snippets ' + + '(--8<-- "File.java:section"):\n ' + violations.join('\n ')) + } + } +} + +// The site generator only validates links to pages, so links into the generated API reference are checked here +tasks.register('docsCheckApiLinks') { + group = 'documentation' + description = 'Fails if a docs page links to a Javadoc page (api/...) that was not generated.' + dependsOn 'docsApi' + def docsDir = file('docs') + inputs.files(fileTree(docsDir) { include '**/*.md'; exclude 'api/**' }) + doLast { + def broken = [] + docsDir.eachFileRecurse { f -> + if (!f.name.endsWith('.md')) { + return + } + f.eachLine { line, n -> + (line =~ /\]\((?:\.\.\/|\.\/)*(api\/[^)#\s]+)/).each { m -> + if (!new File(docsDir, m[1]).isFile()) { + broken << "${docsDir.toPath().relativize(f.toPath())}:${n}: ${m[1]}" + } + } + } + } + if (broken) { + throw new GradleException("Links to missing API pages:\n " + broken.join('\n ')) + } + } +} + tasks.register('docsServe', Exec) { group = 'documentation' description = 'Serves the docs with live reload at http://127.0.0.1:8000.' + dependsOn 'docsApi' commandLine uvRun(['zensical', 'serve', '-f', 'mkdocs.yml']) } tasks.register('docsBuild', Exec) { group = 'documentation' description = 'Builds the docs site into build/docs-site; broken links and nav entries fail the build.' + dependsOn 'docsApi' inputs.dir('docs') inputs.file('mkdocs.yml') outputs.dir(layout.buildDirectory.dir('docs-site')) // site_dir in mkdocs.yml @@ -33,14 +121,15 @@ tasks.register('docsBuild', Exec) { tasks.register('docsCheck') { group = 'documentation' - description = 'Validates the docs (strict build).' - dependsOn 'docsBuild' + description = 'Validates the docs: strict site build (with the API reference) and compiled snippets.' + dependsOn 'docsBuild', 'docsCheckApiLinks', 'docsCheckInlineCode', ':docs-snippets:compileJava' } // ./gradlew docsDeploy -PdocsVersion=dev [-PdocsAlias=latest] [-PdocsPush=false] [-PdocsBranch=docs-site] tasks.register('docsDeploy', Exec) { group = 'documentation' description = 'Publishes a docs version to the docs-site branch (-PdocsBranch) with mike.' + dependsOn 'docsApi' doFirst { def version = findProperty('docsVersion') if (!version) { diff --git a/mkdocs.yml b/mkdocs.yml index 98cfefabf..5f4a1b46e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -28,6 +28,7 @@ nav: - v9 to v10: v9-to-v10-migration-guide.md - v8 to v9: v8-to-v9-migration-guide.md - v7 to v8: v7-to-v8-migration-guide.md + - API: api/index.html - Contributing: - Development notes: Note-for-developers.md - Reporting an issue: How-to-report-an-issue.md @@ -54,7 +55,12 @@ markdown_extensions: - pymdownx.keys - pymdownx.mark - pymdownx.snippets: + # Compiled samples: docs-snippets/src/main/java/io/appium/java_client/docs/, included as + # --8<-- "File.java:section" + base_path: + - docs-snippets/src/main/java/io/appium/java_client/docs check_paths: true + dedent_subsections: true - pymdownx.superfences: custom_fences: - name: mermaid diff --git a/settings.gradle b/settings.gradle index 2ca4a909a..7a54c84a5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,3 +5,4 @@ pluginManagement { rootProject.name = 'java-client' include 'selenium-bridge' +include 'docs-snippets'