簡體   English   中英

使用 appium 從 iOS 移動應用程序自動啟動 Safari

[英]Automate launching Safari from a iOS mobile app using appium

我正在iOS 移動應用程序上使用Appium進行一些自動化。

我需要:

  • 打開應用程序
  • 做一些任務
  • 開放野生動物園

我環顧四周,但我一直在讀到,由於蘋果框架的限制,這是不可能的,它不允許您在每個會話中向多個應用程序發送命令。

有誰知道解決這個問題的方法? 或者,如果我讀到的內容不是 100% 正確。

它不允許您在每個會話中向多個應用程序發送命令

沒錯,但您可以在一個測試中運行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();
}

您可以使用以下方法,

  1. 創建了兩個設置,一個用於應用程序,另一個用於 safari。
  2. 首先啟動應用程序並執行任務
  3. 清除第一次會議
  4. 為 safari 再次創建新的 Appium 對象(調用第二個設置)
  5. 執行瀏覽器活動
  6. 關閉 safari appium 會話

您也可以在不退出驅動程序的情況下按照我的方法進行操作。

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

您可以通過driver.activateApp(BUNDLE_ID);激活系統應用程序driver.activateApp(BUNDLE_ID);

無需殺死應用程序驅動程序並啟動瀏覽器驅動程序來訪問瀏覽器,只需在應用程序之間切換即可。

蘋果瀏覽器

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

您可以通過 driver.activateApp(BUNDLE_ID) 激活已安裝的應用程序; 方法,它將打開應用程序,您可以執行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM