From 50badf50c47f55b7ae07ca22cdb5b9c0b55e980b Mon Sep 17 00:00:00 2001 From: Teodora Jovcheska Date: Mon, 15 Apr 2024 13:29:10 +0200 Subject: [PATCH 1/5] Added method for switching app context --- src/main/java/testUI/TestUIDriver.java | 44 +++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/testUI/TestUIDriver.java b/src/main/java/testUI/TestUIDriver.java index c7834f3..dbc9ba1 100644 --- a/src/main/java/testUI/TestUIDriver.java +++ b/src/main/java/testUI/TestUIDriver.java @@ -12,10 +12,7 @@ import testUI.elements.TestUI; import testUI.elements.UIElement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static testUI.UIUtils.*; @@ -225,4 +222,43 @@ public static DesiredCapabilities getDesiredCapabilities() { public static UIElement getTestUIDriver() { return TestUI.E(""); } + + public static void switchApplicationContext(String context) { + AppiumDriver driver = getDriver(); + + if (driver instanceof AndroidDriver) { + AndroidDriver androidDriver = (AndroidDriver) driver; + Set contextNames = androidDriver.getContextHandles(); + + boolean contextFound = false; + for (String contextName : contextNames) { + if (contextName.contains(context)) { + androidDriver.context(contextName); + System.out.println("Switched to context: " + contextName); + contextFound = true; + break; + } + } + if (!contextFound) { + System.out.println("Provided context is not available"); + } + } else if (driver instanceof IOSDriver) { + IOSDriver iosDriver = (IOSDriver) driver; + Set contextNames = iosDriver.getContextHandles(); + boolean contextFound = false; + for (String contextName : contextNames) { + if (contextName.contains(context)) { + iosDriver.context(contextName); + System.out.println("Switched to context: " + contextName); + contextFound = true; + break; + } + } + if (!contextFound) { + System.out.println("Provided context is not available"); + } + } else { + System.out.println("Unsupported driver type."); + } + } } From 67d10fde769580b3e798dafc7f4546e7d59c9a9d Mon Sep 17 00:00:00 2001 From: Teodora Jovcheska Date: Mon, 15 Apr 2024 15:31:00 +0200 Subject: [PATCH 2/5] Updated Readme --- README.md | 8 ++++++++ src/test/java/TestRunners/TestAndroidLocal.java | 1 + 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 71a2816..369be9e 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,14 @@ public class Test { } ``` +## Changing application context +In certain scenarios, applications may switch contexts, which can be necessary for automation purposes. +Utilize the following function to switch the application context: + +```java +changeAppContext(""); // Pass the application context to switch to +``` + ## Elements Same element can be defined for all the platforms, i.e. same element definition diff --git a/src/test/java/TestRunners/TestAndroidLocal.java b/src/test/java/TestRunners/TestAndroidLocal.java index 8934b20..a9ba178 100644 --- a/src/test/java/TestRunners/TestAndroidLocal.java +++ b/src/test/java/TestRunners/TestAndroidLocal.java @@ -8,6 +8,7 @@ import testUI.TestUIDriver; import static testUI.Configuration.ANDROID_PLATFORM; +import static testUI.Configuration.driver; import static testUI.TestUIServer.stop; import static testUI.UIOpen.open; import static testUI.UIUtils.executeJs; From 86903f3f738296edcbbe532b27f360aa3d75e544 Mon Sep 17 00:00:00 2001 From: Teodora Jovcheska Date: Mon, 15 Apr 2024 15:35:53 +0200 Subject: [PATCH 3/5] Remove unnecessary import --- src/test/java/TestRunners/TestAndroidLocal.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/TestRunners/TestAndroidLocal.java b/src/test/java/TestRunners/TestAndroidLocal.java index a9ba178..8934b20 100644 --- a/src/test/java/TestRunners/TestAndroidLocal.java +++ b/src/test/java/TestRunners/TestAndroidLocal.java @@ -8,7 +8,6 @@ import testUI.TestUIDriver; import static testUI.Configuration.ANDROID_PLATFORM; -import static testUI.Configuration.driver; import static testUI.TestUIServer.stop; import static testUI.UIOpen.open; import static testUI.UIUtils.executeJs; From 667c36e130ae80a81fecc7150ab825583b69ef68 Mon Sep 17 00:00:00 2001 From: Teodora Jovcheska Date: Wed, 17 Apr 2024 10:25:44 +0200 Subject: [PATCH 4/5] Use putLogInfo instead of sout --- src/main/java/testUI/TestUIDriver.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/testUI/TestUIDriver.java b/src/main/java/testUI/TestUIDriver.java index dbc9ba1..0704ba1 100644 --- a/src/main/java/testUI/TestUIDriver.java +++ b/src/main/java/testUI/TestUIDriver.java @@ -15,6 +15,7 @@ import java.util.*; import static testUI.UIUtils.*; +import static testUI.Utils.Logger.putLogInfo; public class TestUIDriver { private static ThreadLocal> driver = new ThreadLocal<>(); @@ -234,13 +235,13 @@ public static void switchApplicationContext(String context) { for (String contextName : contextNames) { if (contextName.contains(context)) { androidDriver.context(contextName); - System.out.println("Switched to context: " + contextName); + putLogInfo("Switched to context: " + contextName); contextFound = true; break; } } if (!contextFound) { - System.out.println("Provided context is not available"); + putLogInfo("Provided context is not available"); } } else if (driver instanceof IOSDriver) { IOSDriver iosDriver = (IOSDriver) driver; @@ -249,16 +250,16 @@ public static void switchApplicationContext(String context) { for (String contextName : contextNames) { if (contextName.contains(context)) { iosDriver.context(contextName); - System.out.println("Switched to context: " + contextName); + putLogInfo("Switched to context: " + contextName); contextFound = true; break; } } if (!contextFound) { - System.out.println("Provided context is not available"); + putLogInfo("Provided context is not available"); } } else { - System.out.println("Unsupported driver type."); + putLogInfo("Unsupported driver type."); } } } From 49f290692a15c6dd56d347b26f8349c96d66a68b Mon Sep 17 00:00:00 2001 From: Teodora Jovcheska Date: Tue, 2 Jul 2024 16:53:51 +0200 Subject: [PATCH 5/5] Updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 369be9e..98ac1b8 100644 --- a/README.md +++ b/README.md @@ -254,7 +254,7 @@ In certain scenarios, applications may switch contexts, which can be necessary f Utilize the following function to switch the application context: ```java -changeAppContext(""); // Pass the application context to switch to +switchApplicationContext(""); // Pass the application context to switch to ``` ## Elements