Skip to content
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

Refactor API for switching context in mobile app #2308

Merged
merged 1 commit into from
May 24, 2023
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.
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 @@ -69,16 +69,16 @@ public static void back() {
Selenide.back();
}

public static void setContext(String contextName) {
new SelenideAppiumTargetLocator().setContext(contextName);
public static SelenideAppiumTargetLocator switchTo() {
return new SelenideAppiumTargetLocator(WebDriverRunner.driver());
}

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

public static String getCurrentContext() {
return new SelenideAppiumTargetLocator().getCurrentContext();
return switchTo().getCurrentContext();
}

@CheckReturnValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package com.codeborne.selenide.appium;

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

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Set;

@ParametersAreNonnullByDefault
public class SelenideAppiumTargetLocator {
private final Driver driver;

public void setContext(String contextName) {
SelenideAppiumTargetLocator(Driver driver) {
this.driver = driver;
}

public void context(String contextName) {
SelenideLogger.run("set context", contextName, () -> {
(WebdriverUnwrapper.cast(WebDriverRunner.getWebDriver(), ContextAware.class))
WebdriverUnwrapper.cast(driver, 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))
return WebdriverUnwrapper.cast(driver, ContextAware.class)
.map(ContextAware::getContextHandles)
.orElseThrow(() -> new UnsupportedOperationException("Cannot get contexts from mobile driver"));
}

public String getCurrentContext() {
return (WebdriverUnwrapper.cast(WebDriverRunner.getWebDriver(), ContextAware.class))
return WebdriverUnwrapper.cast(driver, ContextAware.class)
.map(ContextAware::getContext)
.orElseThrow(() -> new UnsupportedOperationException("Cannot get contexts from mobile driver"));
.orElseThrow(() -> new UnsupportedOperationException("Cannot get current context from mobile driver"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static com.codeborne.selenide.appium.SelenideAppium.getContextHandles;
import static com.codeborne.selenide.appium.SelenideAppium.getCurrentContext;
import static com.codeborne.selenide.appium.SelenideAppium.openAndroidDeepLink;
import static com.codeborne.selenide.appium.SelenideAppium.setContext;
import static com.codeborne.selenide.appium.SelenideAppium.switchTo;
import static org.assertj.core.api.Assertions.assertThat;

class ContextTest extends BaseSwagLabsAndroidTest {
Expand All @@ -27,7 +27,7 @@ void contexts() {
$(AppiumBy.accessibilityId("URL input field")).shouldBe(visible).setValue("www.google.com");
$(AppiumBy.accessibilityId("Go To Site button")).shouldBe(visible).click();
Stopwatch.sleepAtLeast(4000);
setContext("WEBVIEW_com.saucelabs.mydemoapp.rn");
switchTo().context("WEBVIEW_com.saucelabs.mydemoapp.rn");

assertThat(getContextHandles())
.hasSize(2)
Expand Down