Skip to content

Commit

Permalink
Added switching Appium app context
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Mar 27, 2023
1 parent 4291562 commit 296dba4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Testerra
*
* (C) 2022, Daniel Eckardt, T-Systems MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package eu.tsystems.mms.tic.testframework.appium;

import io.appium.java_client.AppiumDriver;

/**
* Created on 2023-03-27
*
* @author mgn
*/
public enum AppiumContext {

/**
* The context ({@link AppiumDriver#getContext()}) of the current view of the application is an operating system native.
*/
NATIVE_APP,
/**
* The context ({@link AppiumDriver#getContext()}) of the current view of the application is a (embedded) browser. {@see
* http://appium.io/docs/en/writing-running-appium/web/hybrid/index.html}
*/
WEBVIEW;

public static AppiumContext parse(String string) {
// there's only two NATIVE_APP contexts (NATIVE_APP and NATIVE_APP_INSTRUMENTED),
// but an infinite amount of WEBVIEW contexts, like WEBVIEW, SAFARI, CHROMIUM, WEBVIEW_1, WEBVIEW_2 etc.
// that's why we just assume that any context starting with NATIVE_APP is native and the rest WEBVIEW:
AppiumContext result = string == null || string.toLowerCase().startsWith("native_app") ? NATIVE_APP : WEBVIEW;
// LOGGER.debug(String.format("Parsed %s as %s", string, result));
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import eu.tsystems.mms.tic.testframework.appium.AppiumContext;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.mobile.driver.MobileOsChecker;
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;

import java.util.Arrays;
import java.util.Optional;
import java.util.Set;

/**
* Created on 2023-03-08
*
* @author mgn
*/
public class AppiumUtils implements Loggable {
public class AppiumUtils implements WebDriverManagerProvider, Loggable {

public String runCommand(WebDriver driver, String command, String... args) {
log().info("Shell command (native): {}, {}", command, Arrays.toString(args)); // ImmutableList does NOT work here...
Expand All @@ -53,4 +58,33 @@ public void launchIOSApp(WebDriver driver, String bundleId) {
}
}

/**
* Switch the Appium Context if available
*/
public void switchContext(WebDriver driver, AppiumContext desiredContext) {
Optional<AppiumDriver> appiumDriver = WEB_DRIVER_MANAGER.unwrapWebDriver(driver, AppiumDriver.class);
if (appiumDriver.isEmpty()) {
throw new RuntimeException("Current Webdriver is not an Appium driver.");
}
String currentContext = appiumDriver.get().getContext();
AppiumContext parsedInitialContext = AppiumContext.parse(currentContext);
log().info("Current context: {} ({})", currentContext, parsedInitialContext);

if (parsedInitialContext.equals(desiredContext)) {
log().info("Already in context {}", desiredContext);
return;
}

Set<String> contextHandles = appiumDriver.get().getContextHandles();
// LOGGER.info(String.format("Available contexts: %s", contextHandles));
contextHandles.stream()
.filter(contextHandle -> AppiumContext.parse(contextHandle).equals(desiredContext))
.findFirst()
.ifPresentOrElse(handle -> {
log().info("Switch to context {} ({})", handle, desiredContext);
appiumDriver.get().context(handle);
}, () -> log().error("Couldn't find a {} context in {}", desiredContext, contextHandles));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
package eu.tsystems.mms.tic.testframework.mobile.systemundertest.page;

import eu.tsystems.mms.tic.testframework.pageobjects.Check;
import eu.tsystems.mms.tic.testframework.pageobjects.GuiElement;
import eu.tsystems.mms.tic.testframework.pageobjects.Locate;
import eu.tsystems.mms.tic.testframework.pageobjects.factory.PageFactory;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

Expand All @@ -40,32 +38,29 @@
public class StartPage extends AbstractInternetPage {

@Check
private GuiElement linkLogin = new GuiElement(this.getWebDriver(), Locate.by(By.linkText("Form Authentication")));
private UiElement linkLogin = find(By.linkText("Form Authentication"));

@Check
private GuiElement linkTables = new GuiElement(this.getWebDriver(), Locate.by(By.linkText("Sortable Data Tables")));
private UiElement linkTables = find(By.linkText("Sortable Data Tables"));

/**
* Constructor for existing sessions.
*
* @param driver .
*/
public StartPage(WebDriver driver) {

super(driver);
}

public LoginPage goToLoginPage() {

this.linkLogin.scrollIntoView();
this.linkLogin.click();
return PageFactory.create(LoginPage.class, this.getWebDriver());
return createPage(LoginPage.class);
}

public TablePage goToTablePage() {

this.linkTables.scrollIntoView();
this.linkTables.click();
return PageFactory.create(TablePage.class, this.getWebDriver());
return createPage(TablePage.class);
}
}

0 comments on commit 296dba4

Please sign in to comment.