0

I want to choose starting app(appPackage,appActivity) in @BeforeScenario to develop a test scenarios for multiple apps in one test project. I know use the start activity but because of security permission denial, I can not use it. The only working method is using capabilities in beforeScenario. Before start the test I want to choose the starting app.My Code:

`@BeforeScenario public void beforeScenario() throws MalformedURLException {

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities
            .setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
    desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "device");
    desiredCapabilities.setCapability(MobileCapabilityType.UDID, "L2N4C19924005752");
    if (localAndroid) {
        logger.info("Local Browser");


        desiredCapabilities
                .setCapability(AndroidMobileCapabilityType.APP_PACKAGE,
                        notapadAppPackageName);
        desiredCapabilities
                .setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,
                        notepadAppActivityName);

    }

    desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
    desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
    desiredCapabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
    desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
    desiredCapabilities.setCapability("unicodeKeyboard", false);
    desiredCapabilities.setCapability("appWaitDuration", 30);
    desiredCapabilities.setCapability("resetKeyboard", false);
    URL url = new URL("http://localhost:4723/wd/hub");
    appiumDriver = new AndroidDriver(url, desiredCapabilities);

    selector = SelectorFactory
            .createElementHelper(localAndroid ? SelectorType.ANDROID : SelectorType.IOS);
    appiumDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    appiumFluentWait = new FluentWait(appiumDriver);
    appiumFluentWait.withTimeout(8, TimeUnit.SECONDS)
            .pollingEvery(350, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);
}

`

1 Answers1

0

You can pass app name from VM aguements and base on that you can set driver instance. Please have a look on code below:

@BeforeScenario public void beforeScenario(String appName) throws MalformedURLException {

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    switch (appName) {
  case "notepadApp":
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,notapadAppPackageName);
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,notepadAppActivityName);
    break;
  case "App-A":
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,appAAppPackageName);
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,appApadAppActivityName);
    break;
    case "App-B":
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,appBAppPackageName);
    desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,appBApadAppActivityName);
    break;
  default:
    System.out.println("No matching app is found.");
}

    // Common desired capabilities set
    desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
    desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "device");
    desiredCapabilities.setCapability(MobileCapabilityType.UDID, "L2N4C19924005752");
    desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
    desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
    desiredCapabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
    desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
    desiredCapabilities.setCapability("unicodeKeyboard", false);
    desiredCapabilities.setCapability("appWaitDuration", 30);
    desiredCapabilities.setCapability("resetKeyboard", false);
    URL url = new URL("http://localhost:4723/wd/hub");
    appiumDriver = new AndroidDriver(url, desiredCapabilities);

    selector = SelectorFactory
            .createElementHelper(localAndroid ? SelectorType.ANDROID : SelectorType.IOS);
    appiumDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    appiumFluentWait = new FluentWait(appiumDriver);
    appiumFluentWait.withTimeout(8, TimeUnit.SECONDS)
            .pollingEvery(350, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);
}
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • In my project I dont use apk file. Just develop test cases according to different apps. Ex. my project has different specs for each apps. So I want to make a parameter that when start the test, the driver know that which apps to run the test. – Silas Hayri Jan 31 '20 at 05:34
  • So you don’t need to install app on device before you run test cases? Do have all app’s data (appPackage and appActivity). – Muzzamil Jan 31 '20 at 06:55
  • Yeap, I have the appPAckage and appActivityname. Just needed setup the beforetest class to choose which app start the test – Silas Hayri Jan 31 '20 at 08:50
  • You can decide it with the help of VM arguent. Use switch case for multiple app and pass appName to decide which app you want to launch – Muzzamil Jan 31 '20 at 11:28
  • I will do it. Please share your code in question that way you are creating driver instance. I will modify same code. – Muzzamil Jan 31 '20 at 11:55
  • @SilasHayri Pleasw check it and let me know – Muzzamil Jan 31 '20 at 20:30
  • I do it but when run the mvn gauge:execute -DspecsDir=specs -Dtags="BrowserTest3" -DappName="notepadApp" I got the `Gauge Specs execution failed. null: GaugeExecutionFailedException ` – Silas Hayri Feb 03 '20 at 06:00
  • please give me the mvn gauge:execute example and also how to run it on intellij @muzzamil – Silas Hayri Feb 03 '20 at 06:33
  • Any idea or solution?? – Silas Hayri Feb 04 '20 at 08:21
  • May i know where are you getting exception (line)? Gauge is maven architecture type? – Muzzamil Feb 04 '20 at 08:51
  • when try to run lin intellij I got the ` Failed: Before Scenario Message: java.lang.IllegalArgumentException: wrong number of arguments ` – Silas Hayri Feb 04 '20 at 10:43
  • Yeap, you can run the gauge in maven by mvn gauge:execute -DspecsDir=specs -Dtags="BrowserTest3" -DappName="notepadApp" . May I give the beforescenario parameters in the wrong place or code – Silas Hayri Feb 04 '20 at 10:53
  • do you have added **"appName"** in Pom.xml so you can use as VM arguements – Muzzamil Feb 04 '20 at 11:19
  • How to add it into pom.xml – Silas Hayri Feb 04 '20 at 12:07
  • here you can check. it comes in maven sure fire plugin https://stackoverflow.com/questions/39750348/how-to-add-vm-args-using-pom-xml-plugin – Muzzamil Feb 04 '20 at 12:09
  • @SilasHayri are you ab;e to fix it ? – Muzzamil Feb 04 '20 at 15:50
  • let me check it and give the result asap – Silas Hayri Feb 04 '20 at 16:49
  • ` Browser ${jvm.appName} ${jvm.appName}` here is added my pom and still get the same error – Silas Hayri Feb 04 '20 at 17:11
  • I think we should use json file in config – Silas Hayri Feb 04 '20 at 18:05
  • Better if you can try app name as hardcoded in **beforeScenario** to get to know where is error. I hope your were able to launch app before – Muzzamil Feb 04 '20 at 19:13
  • Actually, I just want to run my test scenario acording to each app by it's name. Ex. When run the browser test, the appPAckage and appActivity change acording to browser and so far others.But I just got the error – Silas Hayri Feb 04 '20 at 19:18
  • Yes you can try with multiple app in each execution by passing multiple. I am just stuck with gauge. I have no hands on with gauge. – Muzzamil Feb 04 '20 at 19:29
  • So how do you choose the apps package and activityname in junit appium – Silas Hayri Feb 04 '20 at 19:37
  • Same way I have mentioned but I don’t know why you are able to access it through pom.xml and another thing if you can pass all this information as hardcoded still you are getting exception. If your code was working fine before it should work now as I am we are just deciding app pakg and activity using appname – Muzzamil Feb 04 '20 at 19:41
  • do you have another solution like dynamic capabilies for beforeclass? – Silas Hayri Feb 04 '20 at 20:06