Switch from Native app to Safari Browser

Currently in one of my scenario Mobile safari will be launched from native app. But I can’t switch to this newly opened Mobile Safari in iPhone. Following is the code used for opening the app, which is fine.

       DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("automationName", "XCUITest");
        capabilities.setCapability("platformName", "IOS");
        capabilities.setCapability("udid", "xxxxxxx");
        capabilities.setCapability("deviceName", "Appium iPhone");
        capabilities.setCapability("xcodeOrgId", "XXXXXX");
        capabilities.setCapability("xcodeSigningId", "Apple Developer Program");
        capabilities.setCapability("bundleId", "xxx.xxxx.xxx.xxx");
        capabilities.setCapability("useNewWDA", "true");
        driver = new IOSDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);

Above code will open the app in connect real device. Doing a click on the button inside the app will opening mobile safari with the correct url. But when I tried something like

ArrayList contexts = new ArrayList(driver.getContextHandles());

it is only returning Native_Context, which is basically my original native app. How can I switch to safari and do something over there. My assumption was that safari will be a webview and once I switch to this webview I can interact with Safari.

In the case of Android, similar story,

ArrayList contexts = new ArrayList(driver.getContextHandles());

was returning Native and WebContext. I am able to switch to this webcontext and do the needed operation.

Appium Version : 1.22.2
Iphone X
IOS : 15.3.1

Any help will be appreciated.

you cant switch to Safari using your app context cause it NOT webView inside app.
inspect and work with Safari now as with your app.

Try to enable appium:includeSafariInWebviews capability. I’m not sure if this is exactly what you need though

1 Like

Thank you guys for the quick response. Thanks @mykola-mokhnach. With “includeSafariInWebviews” I am able to access Safari Webview. This is exactly what I was looking for. Really appreciate it.