简体   繁体   中英

Switch between tabs on iOS Safari using Appium

I am trying to run my Appium tests on real iOS device. However, I cannot figure how to switch between tabs on Safari. I open a page on Safari using Appium IOSDriver instance. I then click a link on the page that opens a new tab. Now I wish to close this tab or at least switch back to the original window.

I have tried the following solutions but none of them seem to help:

  1. Perform touch action to open the tabs interface, did not manage to click the tabs option.

     TouchAction ta = new TouchAction(driver); int h=driver.manage().window().getSize().getHeight(); int w=driver.manage().window().getSize().getWidth(); ta.tap(h-50,w-50).perform(); 
  2. Tried switch back to original context, did not help

     for (String contextName : contextNames) { if(!contextName.equals(original)) { WebViewContext = contextName; ((AppiumDriver) driver).context(WebViewContext); Thread.sleep(4000); driver.close(); } } 
  3. Tried Javascript, no success

     Set<String> windows = driver.getWindowHandles(); String currentHandle = driver.getWindowHandle(); ((JavascriptExecutor)driver).executeScript("window.open();"); Set<String> newWindow = driver.getWindowHandles(); newWindow.removeAll(windows); driver.switchTo().window(original); 

I have tried the below way and it works for me:

    IOSDriver driver = new IOSDriver();
    driver.get("url to be opened");
    WebElement ele = driver.findElement(By.xpath("xpathOfElementToBeclicked"));

    ele.click(); //New window opens

    Set<String> contextView = ((AppiumDriver)driver).getContextHandles();
    ArrayList<String> s = new ArrayList<String>(contextView);        
    ((AppiumDriver)driver).context(s.get(contextView.size()-1));
    driver.close();

Give it a shot. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM