Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Created April 23, 2018 15:21
Show Gist options
  • Save sauceaaron/01af226f71aa638201819ec5aa84a292 to your computer and use it in GitHub Desktop.
Save sauceaaron/01af226f71aa638201819ec5aa84a292 to your computer and use it in GitHub Desktop.
import io.appium.java_client.AppiumDriver;
import java.util.Set;
public class SwitchToWebView
{
public void switchToWebContext(AppiumDriver driver)
{
String webContext = getWebContext(driver);
System.out.println("got web context: " + webContext);
if (driver.getContext().equals(webContext))
{
System.out.println("already in web context");
return;
}
driver.context(webContext);
}
public String getWebContext(AppiumDriver driver)
{
Set<String> contextHandles = driver.getContextHandles();
for (String contextHandle : contextHandles)
{
System.out.println(contextHandle);
if (contextHandle.contains("WEBVIEW"))
{
return contextHandle;
}
}
throw new RuntimeException("No webview context found");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment