【发布时间】:2022-06-10 21:37:46
【问题描述】:
我正在尝试使用 testng、cucumber 和 appium 启动并行执行测试,但由于 BasePage 中的 initElements 方法 1,我的第二个线程出现错误,但是当我使用 Method2 时效果很好。请参阅下面的代码和日志 你能帮我吗?先感谢您。 BaseMobile:
public class BaseMobile {
protected static AppiumDriver driver = new AndroidDeviceA().getDriver();
TestUtils utils = new TestUtils();
public final static Logger logger = LogManager.getLogger();
//Method1
public static <T extends BaseMobile> T instanceOf(Class<T> clazz) {
return PageFactory.initElements(driver, clazz);
}
} 方法二:
public BaseMobile(){
this.driver = new AndroidDeviceA().getDriver();
PageFactory.initElements(new AppiumFieldDecorator(this.driver), this);
}
AndroidDeviceA 类:
public class AndroidDeviceA {
private static ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
TestUtils utils = new TestUtils();
public AppiumDriver getDriver() {
return driver.get();
}
public void setDriver(AppiumDriver driver2) {
driver.set(driver2);
}
public void initializeDriver() throws Exception {
AppiumDriver driver = null;
if (driver == null) {
try {
utils.log().info("initializing Appium driver");
driver = new AndroidDriver(new AppiumServer().getServer().getUrl(), new UiAutomatorOptions().getUiAutomatorOptionsMobile());
if (driver == null) {
throw new Exception("driver is null. ABORT!!!");
}
utils.log().info("Driver is initialized");
utils.log().info("session"+ driver.getSessionId());
this.driver.set(driver);
} catch (IOException e) {
e.printStackTrace();
utils.log().fatal("Driver initialization failure. ABORT !!!!" + e.toString());
throw e;
}
}
}
}
类步骤:
@When("User click on debug mode")
public void user_click_on_debug_mode() {
instanceOf(StartScreenBorne.class).clickOnDebugMode();
//new StartScreenBorne().clickOnDebugMode();
}
黄瓜跑者:
public class RunnerBase {
private static ThreadLocal<TestNGCucumberRunner> testNGCucumberRunner = new ThreadLocal<>();
public static TestNGCucumberRunner getRunner(){
return testNGCucumberRunner.get();
}
private static void setRunner(TestNGCucumberRunner testNGCucumberRunner1){
testNGCucumberRunner.set(testNGCucumberRunner1);
}
@Parameters({"platformName", "udid", "deviceName", "systemPort",
"chromeDriverPort"})
@BeforeClass(alwaysRun = true)
public void setUpClass(String platformName, String udid, String deviceName, @Optional("Android") String systemPort,
@Optional("Android") String chromeDriverPort) throws Exception {
ThreadContext.put("ROUTINGKEY", platformName + "_" + deviceName);
System.out.println(
"Current Thread ID: "
+ Thread.currentThread().getId());
GlobalParams params = new GlobalParams();
params.setPlatformName(platformName);
params.setUDID(udid);
params.setDeviceName(deviceName);
params.setSystemPort(systemPort);
params.setChromeDriverPort(chromeDriverPort);
new AppiumServer().startServer();
new AndroidDeviceA().initializeDriver();
new AndroidDeviceA().getDriver().manage().timeouts().implicitlyWait(Duration.ofSeconds(40));
setRunner(new TestNGCucumberRunner(this.getClass()));
}
@Test(groups = "cucumber", description = "Runs Cucumber Scenarios", dataProvider = "scenarios")
public void scenario(PickleWrapper pickle, FeatureWrapper cucumberFeature) throws Throwable {
getRunner().runScenario(pickle.getPickle());
}
@DataProvider
public Object[][] scenarios() {
return getRunner().provideScenarios();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() {
AndroidDeviceA driverManager = new AndroidDeviceA();
if(driverManager.getDriver() != null){
driverManager.getDriver().quit();
driverManager.setDriver(null);
}
AppiumServer serverManager = new AppiumServer();
if(serverManager.getServer() != null){
serverManager.getServer().stop();
}
if(testNGCucumberRunner != null){
getRunner().finish();
}
}
}
日志:
套件 > R52N102P7GJ STANDARD_ERROR 2022 年 6 月 7 日晚上 10:28:45 net.masterthought.cucumber.ReportParser parseJsonFiles 信息:文件 'C:\Users\users\AppData\Local\Temp\cucumber8938908637014613434.json' 包含 1 个功能
Suite > R9WM91REZCJ > com.runners.MyPixel3TestNGRunnerTest > 场景[0]("使用公式产品创建订单", "可选[non reg Borne]") 失败 org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 quit() 后使用 WebDriver? 构建信息:版本:'4.1.2',修订:'9a5a329c5a' 系统信息:主机:'DESKTOP-N3FD63R',ip:'192.168.1.13',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0',java.version:'15.0 .2' 驱动信息:io.appium.java_client.android.AndroidDriver 命令:[null, findElement {using=id, value=button_splashscreen_debug}] 功能 {appium:androidInstallTimeout: 180000, appium:app: C:\Users\users\Documents\ap..., appium:appActivity: com.tabesto.kiosk.ui.start...., appium:appPackage: com. tabesto.kiosk.debug, appium:appWaitDuration: 90000, appium:appWaitForLaunch: true, appium:autoGrantPermissions: true, appium:automationName: UiAutomator2, appium:chromedriverPort: 11000, appium:databaseEnabled: false, appium:desired: {androidInstallTimeout: 180000 ,应用程序:C:\Users\users\Documents\ap...,appActivity:com.tabesto.kiosk.ui.start....,appPackage:com.tabesto.kiosk.debug,appWaitDuration:90000,appWaitForLaunch:true ,autoGrantPermissions:true,automationName:UiAutomator2,chromedriverPort:11000,deviceName:Galaxy Tab A,eventTimings:true,nativeWebScreenshot:true,newCommandTimeout:120,noReset:true,platformName:android,systemPort:10000,udid:R52N102P7GJ,uiautomator2ServerLaunchTimeout:90000 }, appium:deviceApiLevel: 30, appium:deviceManufacturer: 三星, appium:deviceModel: SM-T510, appium: deviceName: R52N102P7GJ, appium:deviceScreenDensity: 240, appium:deviceScreenSize: 1200x1920, appium:deviceUDID: R52N102P7GJ, appium:eventTimings: true, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:nativeWebScreenshot: true, appium:networkConnectionEnabled: true, appium:newCommandTimeout: 120, appium:noReset: true, appium:pixelRatio: 1.5, appium:platformVersion: 11, appium:statBarHeight: 36, appium:systemPort: 10000, appium:takesScreenshot: true, appium:udid: R52N102P7GJ, appium:uiautomator2ServerLaunchTimeout: 90000, appium:viewportRect: {height: 1812, left: 0, top: 36, width: 1200}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: android} 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:145) 在 io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:180) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558) 在 org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162) 在 org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:382) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:374) 在 pages.mobile.kiosk.BaseMobile.clickOnDebugMode(BaseMobile.java:53) 在steps.moble.kiosk.StartScreenSteps.user_click_on_debug_mode(StartScreenSteps.java:32) 在?.用户点击调试按钮
【问题讨论】:
-
如果按顺序运行测试会怎样?在这种情况下一切正常吗?
-
一切都按顺序运行并与method2并行运行
-
@AlexeyR。我添加了更多代码,你能检查一下吗?
标签: java selenium cucumber appium