Skip to content

Commit

Permalink
Add constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Dec 14, 2024
1 parent e2c20ee commit e476953
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.appium.java_client.HasBrowserCheck.NATIVE_CONTEXT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand All @@ -31,7 +32,7 @@ public class AndroidContextTest extends BaseAndroidTest {
}

@Test public void testGetContext() {
assertEquals("NATIVE_APP", driver.getContext());
assertEquals(NATIVE_CONTEXT, driver.getContext());
}

@Test public void testGetContextHandles() {
Expand All @@ -42,8 +43,8 @@ public class AndroidContextTest extends BaseAndroidTest {
driver.getContextHandles();
driver.context("WEBVIEW_io.appium.android.apis");
assertEquals(driver.getContext(), "WEBVIEW_io.appium.android.apis");
driver.context("NATIVE_APP");
assertEquals(driver.getContext(), "NATIVE_APP");
driver.context(NATIVE_CONTEXT);
assertEquals(driver.getContext(), NATIVE_CONTEXT);
}

@Test public void testContextError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.appium.java_client.HasBrowserCheck.NATIVE_CONTEXT;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static void startWebViewActivity() {
@BeforeEach
public void setUp() {

driver.context("NATIVE_APP");
driver.context(NATIVE_CONTEXT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.concurrent.CopyOnWriteArrayList;

import static io.appium.java_client.HasBrowserCheck.NATIVE_CONTEXT;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class IOSBiDiTest extends AppIOSTest {
Expand All @@ -31,7 +32,7 @@ public class IOSBiDiTest extends AppIOSTest {
@Disabled("Need to resolve compatibility issues")
public void listenForIosLogs() {
var logs = new CopyOnWriteArrayList<LogEntry>();
try (var logInspector = new LogInspector(driver)) {
try (var logInspector = new LogInspector(NATIVE_CONTEXT, driver)) {
logInspector.onLog(logs::add);
driver.getPageSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.appium.java_client.NoSuchContextException;
import org.junit.jupiter.api.Test;

import static io.appium.java_client.HasBrowserCheck.NATIVE_CONTEXT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -27,7 +28,7 @@
public class IOSContextTest extends BaseIOSWebViewTest {

@Test public void testGetContext() {
assertEquals("NATIVE_APP", driver.getContext());
assertEquals(NATIVE_CONTEXT, driver.getContext());
}

@Test public void testGetContextHandles() {
Expand All @@ -38,7 +39,7 @@ public class IOSContextTest extends BaseIOSWebViewTest {
driver.getContextHandles();
findAndSwitchToWebView();
assertThat(driver.getContext(), containsString("WEBVIEW"));
driver.context("NATIVE_APP");
driver.context(NATIVE_CONTEXT);
}

@Test public void testContextError() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/appium/java_client/HasBrowserCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import static java.util.Objects.requireNonNull;

public interface HasBrowserCheck extends ExecutesMethod, HasCapabilities {
String NATIVE_CONTEXT = "NATIVE_APP";

/**
* Validates if the driver is currently in a web browser context.
*
Expand All @@ -32,7 +34,7 @@ default boolean isBrowser() {
}
try {
var context = ((ContextAware) this).getContext();
return context != null && !context.toUpperCase().contains("NATIVE_APP");
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 @@ -28,12 +28,11 @@

import java.util.Optional;

import static io.appium.java_client.HasBrowserCheck.NATIVE_CONTEXT;
import static io.appium.java_client.pagefactory.bys.ContentType.HTML_OR_DEFAULT;
import static io.appium.java_client.pagefactory.bys.ContentType.NATIVE_MOBILE_SPECIFIC;

public final class WebDriverUnpackUtility {
private static final String NATIVE_APP_PATTERN = "NATIVE_APP";

private WebDriverUnpackUtility() {
}

Expand Down Expand Up @@ -109,7 +108,7 @@ public static ContentType getCurrentContentType(SearchContext context) {

var contextAware = unpackObjectFromSearchContext(context, ContextAware.class);
if (contextAware.map(ContextAware::getContext)
.filter(c -> c.toUpperCase().contains(NATIVE_APP_PATTERN)).isPresent()) {
.filter(c -> c.toUpperCase().contains(NATIVE_CONTEXT)).isPresent()) {
return NATIVE_MOBILE_SPECIFIC;
}

Expand Down

0 comments on commit e476953

Please sign in to comment.