[英]Automate launching Safari from a iOS mobile app using appium
I am doing some automation with Appium on a iOS mobile app .我正在iOS 移动应用程序上使用Appium进行一些自动化。
I need to:我需要:
I looked around how to do it but I've been reading that it's impossible due to a limitation in apple's framework, it doesn't allow you to sent commands to more than one app per session.我环顾四周,但我一直在读到,由于苹果框架的限制,这是不可能的,它不允许您在每个会话中向多个应用程序发送命令。
Does anyone know a way around this?有谁知道解决这个问题的方法? Or if what I read is just not 100% true.
或者,如果我读到的内容不是 100% 正确。
it doesn't allow you to sent commands to more than one app per session
它不允许您在每个会话中向多个应用程序发送命令
Thats true, but you can run 2 sessions in a single test :没错,但您可以在一个测试中运行2 个会话:
In a quick way it may look like:它可能看起来像:
@Test
public void testBothAppAndSafari() throws MalformedURLException {
URL appiumServerUrl = new URL("<your appium server host>");
DesiredCapabilities appCaps = new DesiredCapabilities();
// put required native app capabilities in appCaps
DesiredCapabilities safariCaps = new DesiredCapabilities();
// put required safari capabilities in safariCaps
IOSDriver driver = new IOSDriver(appiumServerUrl, appCaps);
driver.findElement(<locator for element in native app>).click();
// do whatever you want with mobile app
driver.quit();
driver = new IOSDriver(appiumServerUrl, safariCaps);
driver.findElement(<locator for element in web>).click();
// do whatever you want in safari
driver.quit();
}
You can use following approach,您可以使用以下方法,
You also can follow my approach without quit driver.您也可以在不退出驱动程序的情况下按照我的方法进行操作。
WEBVIEW_***
to inspect web element.WEBVIEW_***
以检查网络元素。NATIVE_APP
keywordNATIVE_APP
关键字返回原生上下文Sample code:示例代码:
System.out.println("Run application");
Map<String, Object> params = new HashMap<>();
params.put("bundleId", "com.example");
boolean terminalApp = (boolean) driver.executeScript("mobile: terminateApp", params);
System.out.println("terminateApp: " + terminateApp);
driver.findElementById("Safari").click();
Set<String> contextNames = appDriver.getContextHandles();
// Change context to WEBVIEW_***
appDriver.context(String.valueOf(contextNames.toArray()[1]));
driver.get("https://www.google.com.vn");
Thread.sleep(20000);
// Do something.
// ...
// If you want to communicate with NATIVE context just change to NATIVE_APP.
appDriver.context("NATIVE_APP");
you can activate system apps via driver.activateApp(BUNDLE_ID);
您可以通过
driver.activateApp(BUNDLE_ID);
激活系统应用程序driver.activateApp(BUNDLE_ID);
there is no need to kill the app driver and start browser driver to access browser, just switch between apps.无需杀死应用程序驱动程序并启动浏览器驱动程序来访问浏览器,只需在应用程序之间切换即可。
safari苹果浏览器
driver.activateApp("com.apple.mobilesafari");
you can activate installed apps via driver.activateApp(BUNDLE_ID);您可以通过 driver.activateApp(BUNDLE_ID) 激活已安装的应用程序; method, it will open the app and you can perform the action.
方法,它将打开应用程序,您可以执行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.