Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
switchApplicationContext(""); // Pass the application context to switch to
```

## Elements

Same element can be defined for all the platforms, i.e. same element definition
Expand Down
45 changes: 41 additions & 4 deletions src/main/java/testUI/TestUIDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
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.*;
import static testUI.Utils.Logger.putLogInfo;

public class TestUIDriver {
private static ThreadLocal<List<AppiumDriver>> driver = new ThreadLocal<>();
Expand Down Expand Up @@ -225,4 +223,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<String> contextNames = androidDriver.getContextHandles();

boolean contextFound = false;
for (String contextName : contextNames) {
if (contextName.contains(context)) {
androidDriver.context(contextName);
putLogInfo("Switched to context: " + contextName);
contextFound = true;
break;
}
}
if (!contextFound) {
putLogInfo("Provided context is not available");
}
} else if (driver instanceof IOSDriver) {
IOSDriver iosDriver = (IOSDriver) driver;
Set<String> contextNames = iosDriver.getContextHandles();
boolean contextFound = false;
for (String contextName : contextNames) {
if (contextName.contains(context)) {
iosDriver.context(contextName);
putLogInfo("Switched to context: " + contextName);
contextFound = true;
break;
}
}
if (!contextFound) {
putLogInfo("Provided context is not available");
}
} else {
putLogInfo("Unsupported driver type.");
}
}
}