簡體   English   中英

如何使用 appium 在 android 設備上關閉/終止應用程序?

[英]How to close/kill an app on android device using appium?

為了殺死/關閉應用程序,我嘗試了driver.close(), driver.closeApp(), driver.quit()但應用程序一直在后台運行。 這些命令之一真的應該關閉應用程序嗎? 如果是這樣,如何? 如果沒有 - 有人可以提供替代解決方案嗎?

使用 appium 時,您調用的任何方法都不會從后台刪除應用程序 這就是他們所指的:

((AppiumDriver)driver).closeApp(); // Close the app which was provided in the capabilities at session creation   

((AppiumDriver)driver).close(); // from *RemoteWebDriver.java*, used to close the current browser page  

((AppiumDriver)driver).quit(); // quits the session created between the client and the server

所以你可以嘗試做的是:

((AppiumDriver)driver).resetApp(); // Reset the currently running app for this session

或嘗試這兩者的組合:

((AppiumDriver)driver).removeApp(<package name>); // Remove the specified app from the device (uninstall)
((AppiumDriver)driver).installApp(<path to apk>); // Install an app on the mobile device

我建議:

  • 重置應用程序()

  • 關閉應用程序()

  • 刪除應用程序()

    可以參考這個鏈接: Java-client Appium

如果您將 apk 安裝作為腳本的一部分,您可以使用內置的 appium 功能來關閉和刪除該應用程序。 當您再次觸發腳本時,它會檢查它是否存在於設備中,如果不存在則安裝它,

capabilities.setCapability("fullReset", true);

要關閉應用程序會話,請使用以下命令:

driver.resetApp();
  1. 設置你的能力

    cap.setCapability("fullReset", false); cap.setCapability("noReset", true);
  2. 邏輯

    driver.closeApp(); Boolean b = driver.isAppInstalled("here put your app's bundle ID"); if (b == true) { driver.launchApp(); } else { driver.installApp(app.getAbsolutePath()); }

也許您需要根據您的要求稍微調整代碼。

以上這些都不適合我在 Python 中使用,所以我只是將 adb 加殼到設備上以重置應用程序緩存。 這將從最近的進程中刪除應用程序並停止它在后台運行。

os.system('adb shell "pm clear ' + desired_caps1["appPackage"] + '"')

required_caps1 是我的外部 JSON 文件中所需的功能,顯然“appPackage”只是應用程序的包名稱,例如:com.example.id

你也可以嘗試這樣的手動方式,

//This code will press your android device's recent button :

$driver.pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH);

//and click to the close button of you application:

$driver.findElement(By.id("id of the element to close app")).click();

我希望它有幫助

對於 nativescript-dev-appium
版本 6.1.0 發布
driver.closeApp() 和 driver.launchApp() 結合使用,使用:
driver.restartApp()

要終止/終止應用程序,從 Appium 1.6.0 開始,如果您使用 UIAutomator2 驅動程序,您可以在 Java 中使用它:

driver.terminateApp(packagename);

其中packagename是您的應用程序包,例如 com.twitter.android

請參閱http://appium.io/docs/en/commands/device/app/terminate-app/以供參考

暫無
暫無

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

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