如何在多个 testng 类之间保留 Appium 会话 [英] How to preserve Appium session between multiple testng class

查看:35
本文介绍了如何在多个 testng 类之间保留 Appium 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Appium 自动化 Android 应用程序,我有一个带有设置和拆卸的基类(在设置初始化 appium 会话和拆卸销毁会话中).

I am automating Android application using Appium, I have One Base Class with Setup and Tear down (In setup initialization appium session and in teardown destroying session ).

我在所有testng类中继承的这个基类,现在为每个测试类Appium生成新的会话.

This Base Class I inherited in all testng classes, now for each test class Appium new session generated.

所以我的问题是,一旦为任何类生成了 appium 会话,我们如何在所有类中维护它.

So My question is that How we maintain appium session through out the all class once it generate for any class.

谢谢萨迪克

推荐答案

我已经使用 Singlton 设计模式实现了这种方法,方法如下:

I have implemented this approach using Singlton design pattern here is approach:

public class SingltonFactory{

    private static SingltonFactory instance = new SingltonFactory();
    private static AppiumDriver<MobileElement> driver;

    private SingltonFactory() {
    }

    // Get the only object available
    public static SingltonFactory getInstance() {
        return instance;
    }

    // Get the only object available
    public void setDriver(AppiumDriver<MobileElement> driver1) {
        driver = driver1;
    }

    public AppiumDriver<MobileElement> getAppiumDriver() {
        return driver;
    }   

}

在您的测试用例之前添加初始化 SingltonFactory 并分配驱动程序对象,如下所示:

Add initialize SingltonFactory in your before test cases and assign driver object like below:

AppiumFactory appiumFactory = AppiumFactory.getInstance();
if(appiumFactory.getAppiumDriver() == null) {
    driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);                
}
else{
   driver = appiumFactory.getAppiumDriver();
}

这篇关于如何在多个 testng 类之间保留 Appium 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆