how to Make selectable appPackage and appActivity BeforeScenario in android Appium Test Automation

huangapple go评论85阅读模式
英文:

how to Make selectable appPackage and appActivity BeforeScenario in android Appium Test Automation

问题

@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);
}
英文:

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

得分: 0

你可以从虚拟机参数中传递应用程序名称,并根据此设置驱动程序实例。请查看下面的代码:

@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("未找到匹配的应用程序。");
    }

    // 设置通用的所需能力
    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);
}
英文:

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);
}

huangapple
  • 本文由 发表于 2020年1月30日 19:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59985169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定