Repository navigation
docs: compile the code samples and publish the API reference #2521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mykola-mokhnach
wants to merge
2
commits into
master
Choose a base branch
from
docs/snippets-and-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
−153
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ classes/ | |
|
|
||
| # MkDocs / mike scratch output | ||
| /site/ | ||
| /docs/api/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] | ||
| } |
124 changes: 124 additions & 0 deletions
124
docs-snippets/src/main/java/io/appium/java_client/docs/EventListenerSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
docs-snippets/src/main/java/io/appium/java_client/docs/SeleniumBridgeSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/apiis built bydocsApiand 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.mdtargets for the API. That is whydocsCheckApiLinksvalidates these.htmllinks against the generated tree instead. Keeping them as relative.htmllinks.