我知道有些人会说这个问题是重复的,但我认为不是。
我在模拟器上安装了App,我正在运行以下代码来检查应用程序是否可用。如果App可用,则返回true,参见下面的代码和结果:
public class Install {
public AndroidDriver driver;
@BeforeClass
@Parameters({"appPackage", "appActivity"})
public void SetupAndroidDrivers(String _appPackage, String _appActivity) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "DeviceName");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", _appPackage);
capabilities.setCapability("appActivity", _appActivity); // This is Launcher activity of your app (you can get it from apk info app)
try {
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
Thread.sleep(30000);
} catch (IOException e){
}
}
@Test(priority=1)
@Parameters({"appPackage","Build"})
public void Test(String _appPackage, String Build) {
System.out.println("Check if the App is Installed");
//String appId="com.google.android.youtube";
System.out.println(driver.isAppInstalled(_appPackage));
}
}
Check if the App is Installed
true
===============================================
Android and IOS App Test
Total tests run: 1, Failures: 0, Skips: 0
===============================================
问题是当应用程序不可用时,它会崩溃,下面是日志文件的详细信息:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Could not find package (My_AppPackage) on the device (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.30 seconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'absd460.local', ip: '192.168.56.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_131'
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:27)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:158)
at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:22)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:202)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:50)
at device_testing.Install.SetupAndroidDrivers(Install.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:170)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
注意:我已尝试使用此链接Detect an application is installed or not?中的解决方案,但我无法导入packagemanager或者我错过了什么?
答案 0 :(得分:1)
我对java不熟悉,但您可以通过adb命令检查是否安装了应用
adb shell pm list packages [your.package.name]
如果已经安装了app,则上面的命令将返回
package:[your.package.name]
否则它不会返回任何内容,即空字符串。
您可以根据返回使用 if else 并执行所需的操作
答案 1 :(得分:0)
您可以使用
driver.isAppInstalled(&#34; com.example.AppName&#34);
参考:https://appium.io/docs/en/commands/device/app/is-app-installed/
答案 2 :(得分:-1)
通过使用
检查包名称来检查是否安装了应用程序return mContext.getPackageManager().isPackageAvailable(_appPackage);