Last active
July 13, 2018 04:07
-
-
Save sauceaaron/2774c8ba918156f90e321abf5af09d3a to your computer and use it in GitHub Desktop.
Switch context to change tabs on iPad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.openqa.selenium.By; | |
| public static class Allstate | |
| { | |
| public static class HomePage | |
| { | |
| public static String url = "https://www-stest.allstate.com/"; | |
| public static String title = "Auto Insurance Quotes - Car Insurance | Allstate Online Quote"; | |
| public static By findAgentLink = By.linkText("Find An Agent"); | |
| } | |
| public static class FindAgentPage | |
| { | |
| public static String url = "https://agents.allstate.com/locator.html?zip=&intcid=%2Fhome%2Fhome.aspx%7CMainNav%7Cfindagent"; | |
| public static String title = "Find Insurance Agents Near You | Allstate"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.saucelabs.examples</groupId> | |
| <artifactId>allstate-switching-windows</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-compiler-plugin</artifactId> | |
| <configuration> | |
| <source>1.8</source> | |
| <target>1.8</target> | |
| </configuration> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| <dependencies> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.12</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.saucelabs</groupId> | |
| <artifactId>saucerest</artifactId> | |
| <version>1.0.35</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.seleniumhq.selenium</groupId> | |
| <artifactId>selenium-java</artifactId> | |
| <version>3.5.2</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>io.appium</groupId> | |
| <artifactId>java-client</artifactId> | |
| <version>5.0.4</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.appium.java_client.AppiumDriver; | |
| import io.appium.java_client.ios.IOSDriver; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebElement; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.support.ui.ExpectedConditions; | |
| import org.openqa.selenium.support.ui.WebDriverWait; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.Set; | |
| public class FindAgentAndSwitchContext | |
| { | |
| private static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| private static final String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| private static final String SAUCE_URL = "https://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub"; | |
| public static void main(String[] args) throws MalformedURLException | |
| { | |
| // SETUP | |
| URL url = new URL(SAUCE_URL); | |
| DesiredCapabilities capabilities = getIPadProSimulatorCapabilities(); | |
| AppiumDriver<WebElement> driver = new IOSDriver<WebElement>(url, capabilities); | |
| WebDriverWait wait = new WebDriverWait(driver, 60); | |
| // OPEN HOME PAGE | |
| System.out.println("Open " + Allstate.HomePage.url); | |
| driver.get(Allstate.HomePage.url); | |
| // INITIAL CONTEXT | |
| printContextWindowAndTitle(driver); | |
| // CLICK LINK THAT OPENS IN A NEW TAB | |
| System.out.println("Click " + Allstate.HomePage.findAgentLink); | |
| wait.until(ExpectedConditions.elementToBeClickable(Allstate.HomePage.findAgentLink)).click(); | |
| // CONTEXT AFTER OPENING NEW TAB | |
| printContextWindowAndTitle(driver); | |
| assert(driver.getTitle().equals(Allstate.FindAgentPage.title)); | |
| // SWITCH CONTEXT | |
| System.out.println("Switch context"); | |
| switchWebContext(driver); | |
| // CONTEXT AFTER SWITCHING BACK | |
| printContextWindowAndTitle(driver); | |
| assert(driver.getTitle().equals(Allstate.HomePage.title)); | |
| driver.quit(); | |
| } | |
| public static DesiredCapabilities getIPadProSimulatorCapabilities() | |
| { | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("appiumVersion", "1.8.1"); | |
| capabilities.setCapability("platformVersion","11.3"); | |
| capabilities.setCapability("platformName", "iOS"); | |
| capabilities.setCapability("deviceName","iPad Pro (12.9 inch) (2nd generation) Simulator"); | |
| capabilities.setCapability("deviceOrientation", "portrait"); | |
| capabilities.setCapability("browserName", "Safari"); | |
| capabilities.setCapability("locationServicesAuthorized", true); | |
| capabilities.setCapability("nativeWebTap", true); | |
| capabilities.setCapability("name", "Allstate Find Agent switching context"); | |
| return capabilities; | |
| } | |
| public static void switchWebContext(AppiumDriver driver) | |
| { | |
| String newContext = getNewContext(driver); | |
| if (newContext != null) | |
| { | |
| System.out.println("switching to new context: " + newContext); | |
| driver.context(newContext); | |
| } | |
| } | |
| // This assumes there are only two contexts | |
| public static String getNewContext(AppiumDriver driver) | |
| { | |
| String currentContext = driver.getContext(); | |
| Set<String> contextHandles = driver.getContextHandles(); | |
| for (String contextHandle : contextHandles) | |
| { | |
| if (contextHandle.startsWith("WEBVIEW") && ! contextHandle.equals(currentContext)) | |
| { | |
| return contextHandle; | |
| } | |
| } | |
| return null; | |
| } | |
| public static void printContextWindowAndTitle(AppiumDriver driver) | |
| { | |
| System.out.println("------"); | |
| System.out.println("CONTEXT HANDLES: " + driver.getContextHandles()); | |
| System.out.println("CURRENT CONTEXT: " + driver.getContext()); | |
| System.out.println("WINDOW HANDLES: " + driver.getWindowHandle()); | |
| System.out.println("CURRENT WINDOW: " + driver.getWindowHandle()); | |
| System.out.println("TITLE: " + driver.getTitle()); | |
| System.out.println("------"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment