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:
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();
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();
}
}
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