Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

added wrapper methods for getContextHandles and setContext #149

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.Set;

import static com.codeborne.selenide.WebDriverRunner.driver;

Expand Down Expand Up @@ -68,6 +69,14 @@ public static void back() {
Selenide.back();
}

public static void setContext(String contextName) {
new SelenideAppiumTargetLocator().setContext(contextName);
}
amuthansakthivel marked this conversation as resolved.
Show resolved Hide resolved

public static Set<String> getContextHandles() {
return new SelenideAppiumTargetLocator().getContextHandles();
}

@CheckReturnValue
@Nonnull
public static SelenideAppiumElement $x(String xpathExpression) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.codeborne.selenide.appium;

import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.logevents.SelenideLogger;
import org.openqa.selenium.ContextAware;

import java.util.Set;

public class SelenideAppiumTargetLocator {
amuthansakthivel marked this conversation as resolved.
Show resolved Hide resolved

public void setContext(String contextName) {
SelenideLogger.run("set context", contextName, () -> {
(WebdriverUnwrapper.cast(WebDriverRunner.getWebDriver(), ContextAware.class))
.map(contextAware -> contextAware.context(contextName))
.orElseThrow(() -> new UnsupportedOperationException("Context not found" + contextName));
});
}

public Set<String> getContextHandles() {
return (WebdriverUnwrapper.cast(WebDriverRunner.getWebDriver(), ContextAware.class))
.map(ContextAware::getContextHandles)
.orElseThrow(() -> new UnsupportedOperationException("Cannot get contexts from mobile driver"));
}


}
24 changes: 24 additions & 0 deletions src/test/java/integration/android/ContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package integration.android;

import com.codeborne.selenide.appium.SelenideAppium;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.WebDriverRunner.closeWebDriver;
import static org.assertj.core.api.Assertions.assertThat;

class ContextTest extends BaseSwagLabsAndroidTest {

@BeforeEach
void setUp() {
closeWebDriver();
}

@Test
void contexts() {
SelenideAppium.openAndroidDeepLink("mydemoapprn://webview", "com.saucelabs.mydemoapp.rn");
SelenideAppium.setContext("NATIVE_APP");
Copy link
Member

Choose a reason for hiding this comment

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

It would be great to test switching between different contexts.
Does this app has some other contexts in addition to NATIVE_APP?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asolntsev - I have altered the test now to validate switching context. I have also added another method to get the current context

assertThat(SelenideAppium.getContextHandles())
.anyMatch(context -> context.contentEquals("NATIVE_APP"));
}
}