java 如何使用 appium 在 android 设备上关闭/终止应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37178914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:16:21  来源:igfitidea点击:

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

javaandroidappiumkill

提问by Chechy Thehost

To kill/close app I tried driver.close(), driver.closeApp(), driver.quit()but the app keeps running on the background. Should one of these commands really close the application? If so, the how? If not - could someone offer an alternative solution?

要杀死/关闭应用程序,我尝试过,driver.close(), driver.closeApp(), driver.quit()但该应用程序一直在后台运行。这些命令之一真的应该关闭应用程序吗?如果是这样,如何?如果没有 - 有人可以提供替代解决方案吗?

回答by Naman

None of the method that you are calling removes the app from backgroundwhile using appium. This is what they refer to :

使用 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

so what you could possibly try and do is :

所以你可以尝试做的是:

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

OR try a combination of these two :

或尝试这两者的组合:

((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

回答by Emna Ayadi

I would suggest :

我会建议 :

  • 重置应用程序()

  • 关闭应用程序()

  • 删除应用程序()

    可以参考这个链接:Java-client Appium

回答by karthick23

If you have the apk installation as part of the script, you can use inbuilt appium capabiltiy which closes and removes the app. And when you trigger the script again it checks whether it exists in device and if not will install it,

如果您将 apk 安装作为脚本的一部分,您可以使用内置的 appium 功能来关闭和删除该应用程序。当您再次触发脚本时,它会检查它是否存在于设备中,如果不存在则安装它,

capabilities.setCapability("fullReset", true);

回答by Gaurav

To close the app sesssion use the following command:

要关闭应用程序会话,请使用以下命令:

driver.resetApp();

回答by Star

  1. Set your capabilities

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

    driver.closeApp();
    Boolean b = driver.isAppInstalled("here put your app's bundle ID");
    if (b == true) {
        driver.launchApp();
    } else {
        driver.installApp(app.getAbsolutePath());
    }
    
  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());
    }
    

Maybe you need to maneuver the code a bit as per your requirements.

也许您需要根据您的要求稍微调整代码。

回答by P.Gleeson

None of the above worked for me in Python, so I just adb shelled on to the device to reset the app cache. This removes the app from recent processes and stops it running in the background.

以上在 Python 中都不适合我,所以我只是将 adb 炮轰到设备上以重置应用程序缓存。这将从最近的进程中删除应用程序并停止它在后台运行。

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

desired_caps1 is the desired capabilites within my external JSON file, and obviously "appPackage" is just the package name for the app, for eg: com.example.id

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

回答by Hacci56

also you can try a manual way like this,

你也可以尝试这样的手动方式,

//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();

I hope it helps

我希望它有帮助

回答by orion

For nativescript-dev-appium
version 6.1.0 release,
driver.closeApp() and driver.launchApp() are combined, use:
driver.restartApp()

对于 nativescript-dev-appium
version 6.1.0 release
driver.closeApp() 和 driver.launchApp() 结合使用,使用:
driver.restartApp()

回答by Ron Werner

To kill/terminate an app, as of Appium 1.6.0 if you use UIAutomator2 driver you can use this in Java:

要终止/终止应用程序,从 Appium 1.6.0 开始,如果您使用 UIAutomator2 驱动程序,您可以在 Java 中使用它:

driver.terminateApp(packagename);

driver.terminateApp(packagename);

where packagenameis your app package, e.g. com.twitter.android

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

See http://appium.io/docs/en/commands/device/app/terminate-app/for reference

请参阅http://appium.io/docs/en/commands/device/app/terminate-app/以供参考