gpt4 book ai didi

java - 如何在android Appium测试自动化中制作可选的appPackage和appActivity BeforeScenario

转载 作者:行者123 更新时间:2023-12-01 18:38:52 27 4
gpt4 key购买 nike

我想选择在 @BeforeScenario 中启动应用程序(appPackage,appActivity) 来为一个测试项目中的多个应用程序开发测试场景。我知道使用启动 Activity ,但由于安全权限被拒绝,我无法使用它。唯一的工作方法是使用 beforeScenario 中的功能。在开始测试之前,我想选择起始应用程序。我的代码:

`@BeforeScenario public void beforeScenario() 抛出 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);
}

`

最佳答案

您可以从虚拟机参数传递应用程序名称,并在此基础上设置驱动程序实例。请看下面的代码:

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

关于java - 如何在android Appium测试自动化中制作可选的appPackage和appActivity BeforeScenario,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59985169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com