Skip to content

fix: Fix Selenium 4.34.0+ compatibility #2298

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

Merged
merged 2 commits into from
Jun 2, 2025
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ dependencies {
### Compatibility Matrix
Appium Java Client | Selenium client
----------------------------------------------------------------------------------------------------|-----------------------------
`9.4.0` | `4.26.0`, `4.27.0`, `4.28.0`
- | `4.34.0`
`9.4.0` | `4.26.0`, `4.27.0`, `4.28.0`, `4.28.1`, `4.29.0`, `4.30.0`, `4.31.0`, `4.32.0`, `4.33.0`
`9.2.1`(known issues: appium/java-client#2145, appium/java-client#2146), `9.2.2`, `9.2.3`, `9.3.0` | `4.19.0`, `4.19.1`, `4.20.0`, `4.21.0`, `4.22.0`, `4.23.0`, `4.23.1`, `4.24.0`, `4.25.0`, `4.26.0`, `4.27.0`
`9.1.0`, `9.2.0` | `4.17.0`, `4.18.0`, `4.18.1`
`9.0.0` | `4.14.1`, `4.15.0`, `4.16.0` (partially [corrupted](https://github.com/SeleniumHQ/selenium/issues/13256)), `4.16.1`
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/appium/java_client/HasBrowserCheck.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.appium.java_client;

import io.appium.java_client.internal.CapabilityHelpers;
import org.openqa.selenium.ContextAware;
import io.appium.java_client.remote.SupportsContextSwitching;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
Expand Down Expand Up @@ -29,11 +29,11 @@ default boolean isBrowser() {
// ignore
}
}
if (!(this instanceof ContextAware)) {
if (!(this instanceof SupportsContextSwitching)) {
return false;
}
try {
var context = ((ContextAware) this).getContext();
var context = ((SupportsContextSwitching) this).getContext();
return context != null && !context.toUpperCase().contains(NATIVE_CONTEXT);
} catch (WebDriverException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import io.appium.java_client.HasBrowserCheck;
import io.appium.java_client.pagefactory.bys.ContentType;
import io.appium.java_client.remote.SupportsContextSwitching;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.ContextAware;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WrapsDriver;
Expand Down Expand Up @@ -92,21 +92,22 @@ public static WebDriver unpackWebDriverFromSearchContext(@Nullable SearchContext
* {@link WebDriver} or {@link org.openqa.selenium.WebElement} or some other
* user's extension/implementation.
* Note: if you want to use your own implementation then it should
* implement {@link ContextAware} or {@link WrapsDriver} or {@link HasBrowserCheck}
* implement {@link SupportsContextSwitching} or {@link WrapsDriver} or {@link HasBrowserCheck}
* @return current content type. It depends on current context. If current context is
* NATIVE_APP it will return {@link ContentType#NATIVE_MOBILE_SPECIFIC}.
* {@link ContentType#HTML_OR_DEFAULT} will be returned if the current context is WEB_VIEW.
* {@link ContentType#HTML_OR_DEFAULT} also will be returned if the given
* {@link SearchContext} instance doesn't implement {@link ContextAware} and {@link WrapsDriver}
* {@link SearchContext} instance doesn't implement {@link SupportsContextSwitching} and
* {@link WrapsDriver}
*/
public static ContentType getCurrentContentType(SearchContext context) {
var browserCheckHolder = unpackObjectFromSearchContext(context, HasBrowserCheck.class);
if (browserCheckHolder.filter(hbc -> !hbc.isBrowser()).isPresent()) {
return NATIVE_MOBILE_SPECIFIC;
}

var contextAware = unpackObjectFromSearchContext(context, ContextAware.class);
if (contextAware.map(ContextAware::getContext)
var contextAware = unpackObjectFromSearchContext(context, SupportsContextSwitching.class);
if (contextAware.map(SupportsContextSwitching::getContext)
.filter(c -> c.toUpperCase().contains(NATIVE_CONTEXT)).isPresent()) {
return NATIVE_MOBILE_SPECIFIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.appium.java_client.MobileCommand;
import io.appium.java_client.NoSuchContextException;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.ContextAware;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.Response;
Expand All @@ -32,7 +31,7 @@

import static java.util.Objects.requireNonNull;

public interface SupportsContextSwitching extends WebDriver, ContextAware, ExecutesMethod {
public interface SupportsContextSwitching extends WebDriver, ExecutesMethod {
/**
* Switches to the given context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ContextAware;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.JavascriptExecutor;
Expand All @@ -39,27 +38,14 @@
import java.util.Map;
import java.util.Set;

public class EmptyWebDriver implements WebDriver, ContextAware,
JavascriptExecutor, HasCapabilities, TakesScreenshot {
public class EmptyWebDriver implements WebDriver, JavascriptExecutor, HasCapabilities, TakesScreenshot {
Copy link
Contributor

Choose a reason for hiding this comment

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

should it instead implement SupportsContextSwitching ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed the corresponding methods, they were not used

public EmptyWebDriver() {
}

private static List<StubWebElement> createStubList() {
return List.of(new StubWebElement(), new StubWebElement());
}

public WebDriver context(String name) {
return null;
}

public Set<String> getContextHandles() {
return null;
}

public String getContext() {
return "";
}

public void get(String url) {
}

Expand Down