简体   繁体   English

使用 appium 从 iOS 移动应用程序自动启动 Safari

[英]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:我需要:

  • open the app打开应用程序
  • do some tasks做一些任务
  • open safari开放野生动物园

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 个会话

  1. create instance of appium driver with app -based capabilities创建具有基于应用程序功能的 appium 驱动程序实例
  2. do what you need in the app在应用程序中执行您需要的操作
  3. quit driver退出司机
  4. create instance of appium driver with browser -based capabilities创建具有基于浏览器功能的 appium 驱动程序实例
  5. do what you need in the safari在 safari 中做你需要做的事
  6. quit driver退出司机

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,您可以使用以下方法,

  1. Created two setup one for app and other for safari.创建了两个设置,一个用于应用程序,另一个用于 safari。
  2. First launch application and perform task首先启动应用程序并执行任务
  3. clear first session清除第一次会议
  4. Created again new Appium object for safari ( call second setup )为 safari 再次创建新的 Appium 对象(调用第二个设置)
  5. Perform browser activity执行浏览器活动
  6. Close safari appium session关闭 safari appium 会话

You also can follow my approach without quit driver.您也可以在不退出驱动程序的情况下按照我的方法进行操作。

  1. Go terminate application.去终止应用程序。 (I've used javascript to run terminateApp cause native method not work for me.) (我使用 javascript 来运行terminateApp导致本机方法对我不起作用。)
  2. Find Safari on Home screen and then click on it在主屏幕上找到 Safari,然后单击它
  3. Use drive.get to open website as you expected.使用 drive.get 按预期打开网站。
  4. In there you can change to WEBVIEW_*** to inspect web element.在那里您可以更改为WEBVIEW_***以检查网络元素。
  5. Back to native context by NATIVE_APP keyword通过NATIVE_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.

相关问题 使用Appium在真实设备上自动化Mobile Safari并遇到未捕获的异常 - Using Appium to automate Mobile Safari on real device and encountering uncaught exceptions Appium Inspector未针对iOS App启动 - Appium inspector is not launching for iOS App 使用appium自动化IOS UITableview - Automate IOS UITableview using appium 如何在iOS设备上使用appium自动化不是我的应用程序 - How do I automate an app that is not mine using appium on an ios device 在不使用iOS应用程序中的任何特定URL的情况下启动Safari App - Launching Safari App without using any specific url from an iOS App 如何启用从Safari iOS中的深层链接启动应用程序 - How to enable launching of the app from deep link in Safari iOS 从ios框架启动应用程序或在野生动物园中打开链接 - launching app or opening link in safari from ios framework iOS 9.3.2自定义URL方案未从Safari启动应用 - iOS 9.3.2 Custom URL Scheme not launching app from Safari 如何使用Appium在iOS中自动执行割炬选项? - How to automate torch option in iOS using appium? 如何使用appium在移动应用程序中有效地划分测试用例以使其自动化 - How to divide test cases effectively to automate them in a mobile app using appium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM