-1

I know some of you will say the question is duplicate but i think its not.

I installed App on the emulator and i am running the below code to check if the App is available or not. If the App is available it returns true, see below code and result:

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

The problem is when the App is not available it crushes and below is the log file details:

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)

NB: I have tried to you use solution from this link Detect an application is installed or not? but i am not able to import packagemanager or am i missing something?

Rumid
  • 1,627
  • 2
  • 21
  • 39
Djkgotso
  • 130
  • 2
  • 4
  • 15

3 Answers3

2

i am not much familiar with java, but you can check if app is installed or not through adb command

adb shell pm list packages [your.package.name]

If app is already installed then above command will return,

package:[your.package.name]

Else it won't return anything i.e. empty String.

you can use if else based on return and do the desired thing

pankaj mishra
  • 2,555
  • 2
  • 17
  • 31
  • i need to do it programatically on the code above but thank you for trying to help @pankaj – Djkgotso Jan 05 '18 at 07:43
  • 1
    You can run a Process with Java... This is the fastest solution in my opinion... Example: https://stackoverflow.com/a/14029249/3685973 – barbudito Jan 05 '18 at 09:29
0

You can use

driver.isAppInstalled("com.example.AppName");

Reference: https://appium.io/docs/en/commands/device/app/is-app-installed/

nlt
  • 555
  • 7
  • 11
-1

Check if app is installed by checking package name with

return mContext.getPackageManager().isPackageAvailable(_appPackage);
Demonick
  • 2,116
  • 3
  • 30
  • 40
  • can you please help on how to implement the solution you posted from my code above. or edit my code with it please @Demonick – Djkgotso Jan 05 '18 at 07:42