【问题标题】:Automate launching Safari from a iOS mobile app using appium使用 appium 从 iOS 移动应用程序自动启动 Safari
【发布时间】:2018-05-31 22:51:50
【问题描述】:

我正在 iOS 移动应用 上使用 Appium 进行一些自动化操作。

我需要:

  • 打开应用
  • 做一些任务
  • 开启野生动物园

我查看了如何做到这一点,但我一直在阅读,由于苹果框架的限制,这是不可能的,它不允许您在每个会话中向多个应用程序发送命令。

有人知道解决这个问题的方法吗?或者,如果我读到的不是 100% 正确的。

【问题讨论】:

    标签: ios mobile automation appium


    【解决方案1】:

    它不允许您在每个会话中向多个应用发送命令

    没错,但您可以在单个测试中运行 2 个会话

    1. 使用基于应用程序的功能创建 appium 驱动程序实例
    2. 在应用中做你需要的事情
    3. 退出驱动程序
    4. 使用基于浏览器的功能创建 appium 驱动程序实例
    5. 在 safari 中做你需要的事情
    6. 退出驱动程序

    它可能看起来像这样:

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

    【讨论】:

    • 哦,我明白了,这很有道理.. 你能给我一个例子来说明如何在一个测试中运行 2 个会话吗?或将我重定向到某个我可以看到它是如何完成的?感谢您的回答
    • 我在答案中添加了 java 中的示例。
    • 我几乎在 ruby 中完成这项工作,就在我创建第二个驱动程序时,Safari 没有启动,它只是退出了。你知道为什么会发生这种情况吗?无论如何,非常感谢您的回答,这是一个巨大的帮助!
    【解决方案2】:

    您可以使用以下方法,

    1. 创建了两个设置,一个用于应用程序,另一个用于 safari。
    2. 首次启动应用程序并执行任务
    3. 清除第一个会话
    4. 再次为 safari 创建了新的 Appium 对象(调用第二个设置)
    5. 执行浏览器活动
    6. 关闭 safari appium 会话

    【讨论】:

    • 感谢您的回答,这是有道理的,但是当您说created two setup one for app and other for safari 时,我真的不明白您的意思。 :P
    【解决方案3】:

    你也可以按照我的方法不退出驱动。

    1. 去终止应用程序。 (我使用 javascript 运行 terminateApp 导致本地方法不适合我。)
    2. 在主屏幕上找到 Safari,然后点击它
    3. 使用 drive.get 按预期打开网站。
    4. 在那里您可以更改为WEBVIEW_*** 来检查网页元素。
    5. 通过NATIVE_APP关键字返回原生上下文

    示例代码:

        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");
    

    【讨论】:

      【解决方案4】:

      您可以通过driver.activateApp(BUNDLE_ID);激活系统应用

      无需杀死应用驱动并启动浏览器驱动即可访问浏览器,只需在应用之间切换即可。

      野生动物园

      driver.activateApp("com.apple.mobilesafari");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 2023-03-21
        • 2011-07-22
        相关资源
        最近更新 更多