Appium 最近在使用 Appium 做自动化测试,包括 Android 和 iOS,只是在封装的过程中,不知道该如何实现下面的代码,求建议

dy20082250 · 2017年12月05日 · 最后由 hello 回复于 2018年07月04日 · 1449 次阅读
public class PlatformSelector {

    public WebDriver setDriver(MobileType mobileType, URL url, DesiredCapabilities cap) {
        MobileDriver platform = new MobileDriver();
        WebDriver driver = null;
        try{
            switch (mobileType) {
                case ANDROID:
                    driver = platform.androidDriver(url, cap);
                    LogUtil.frameLog("Launch ANDROID platform Complete");
                    return driver;
                case FIREOS:
                    driver = platform.fireOSDriver(url, cap);
                    LogUtil.frameLog("Launch FIREOS platform Complete");
                    return driver;
                case iOS:
                    driver = platform.iosDriver(url, cap);
                    LogUtil.frameLog("Launch iOS platform Complete");
                    return driver;
                default:
                    LogUtil.frameLog("Please Check the Type of Platform");
                    return driver;
            }
        }
        catch(Exception e){
            LogUtil.error("Launch Mobile paltform Failed\n");
            return driver;
        }
    }


}
public class MobileDriver {

    private WebDriver driver;
    private static int RETRY_TIME;
    private static int RETRY_WAIT;

    public MobileDriver(){
        RETRY_TIME = Integer.parseInt(FrameConfig.getInstance().getConfig("RetryTime"));
        RETRY_WAIT = Integer.parseInt(FrameConfig.getInstance().getConfig("RetryWait"));
    }

    protected WebDriver androidDriver(URL url,DesiredCapabilities cap) {
        LogUtil.frameLog("Launch Android Platform Application...");

        for(int i = 1; i <= RETRY_TIME; i++){
            try{
                driver = new AndroidDriver<AndroidElement>(url,cap);
                driver.manage().timeouts().implicitlyWait(Long.parseLong(FrameConfig.getInstance().getConfig("TimeOut")), TimeUnit.SECONDS);
                break;
            }
            catch(Exception e){
                LogUtil.warn("Launch Android Platform Application, Retry (" + i + "/" + RETRY_TIME + ") Times After " + RETRY_WAIT + "Seconds");
                SysUtil.sleep(RETRY_WAIT);
                if(i == RETRY_TIME){
                    throw e;
                }
            }
        }
        return driver;
    }

    protected WebDriver iosDriver(URL url,DesiredCapabilities cap) {
        LogUtil.frameLog("Launch iOS Platform Application...");

        for(int i = 1; i <= RETRY_TIME; i++){
            try{
                driver = new IOSDriver<MobileElement>(url,cap);
                driver.manage().timeouts().implicitlyWait(Long.parseLong(FrameConfig.getInstance().getConfig("TimeOut")), TimeUnit.SECONDS);
                break;
            }
            catch(Exception e){
                LogUtil.warn("Launch iOS Platform Application Failed, Retry (" + i + "/" + RETRY_TIME + ") Times After " + RETRY_WAIT + "Seconds");
                SysUtil.sleep(RETRY_WAIT);
                if(i == RETRY_TIME){
                    throw e;
                }
            }
        }
        return driver;
    }

}

我想要达到的目的是在底层分别封装 AndroidDriver 和 IOSDriver,然后在脚本层面通过传递不同的平台参数
实现 Driver 的自动配置,然后在脚本层面就不必关心 AndroidDriver 和 IOSDriver 的区别了;

大家看看有什么好的实现方法没?

public class SecondTest extends TestBase{

    private URL url;
    private DesiredCapabilities cap;
    private String userName;
    private String passWord;


    @BeforeClass
    public void setUp(){

        initVariables();
        setDriver(MobileType.ANDROID, url, cap);
    }
    /***********************************************
     *
     *初始化基本信息
     *
     ***********************************************/
    private void initVariables(){

        userName = "xxxxxxx";
        passWord = "xxxxx";
        cap = new DesiredCapabilities();
        cap.setCapability("deviceName","192.168.54.101:5555");
        cap.setCapability("automationName","Appium");
        cap.setCapability("platformName","Android");
        cap.setCapability("platformVersion","7.1.1");

       //配置测试apk
        cap.setCapability("appPackage", "xxxxxxxxx");
        cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".AppStartActivity");
        cap.setCapability("sessionOverride", true);    //每次启动时覆盖session,否则第二次后运行会报错不能新建session
        cap.setCapability("unicodeKeyboard", true);    //设置键盘
        cap.setCapability("resetKeyboard", false);     //设置默认键盘为appium的键盘

        try {
            url = new URL("http://127.0.0.1:4723/wd/hub");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Test(description = "登录APP")
    public void Login(){

        action.sleep(5, "等待APP的启动");

        action.swipe("向左滑动");
        action.sleep(2, "等待页面滑动动作完成");
        action.swipe("向左滑动");
        action.sleep(2, "等待页面滑动动作完成");
        action.swipe("向左滑动");
        action.sleep(2, "等待页面滑动动作完成");

        action.sendKeys(By.id("com.eisoo.anycontent:id/accountEdt"), userName, "输入用户名");
        action.sendKeys(By.id("com.eisoo.anycontent:id/et_password"), passWord, "输入用户名");
        action.click(By.id("com.eisoo.anycontent:id/tv_account_login"), "点击登录按钮");
        action.sleep(10, "等待");
    }

}

上面的效果就是我想要达到的,只是目前对 API 的都是 AndroidDriver,而 IOSDriver 的 API 确实与前者有所不同。

大家有什么类似的经验,一块儿学习一下呀

共收到 16 条回复 时间 点赞

自己顶一下,希望更多的朋友能够看到

可以试试 AppiumDriver 适用于 Android 和 iOS

渐次消逝 回复

在尝试,但是很多时候需要使用强制类型转换,我在想能不能添加一个 flag,然后可以不使用强制类型转换

还有人吗?

有 JAVA 的高手没?

怎么你在写自动化脚本的时候用到了封装, 我就没用到哦!

金主 回复

。。。

我们做的是 配置写在 properties 文件里根据 配置自动读取 Android 或者 ios
定义 protected static AppiumDriver driver;

根据 true 或者 false 执行相对应的 方法初始化对应的 driver

斯拉 回复

我也正在尝试使用这样的配置或者说使用 flag 的方式,只是我觉得这种方式的封装程度还是不够,我希望能能够完全屏蔽
AndroidDriver 和 IOSDriver 的区别,在写业务测试脚本这个层面提供一套统一的 API

谢谢你的回复,如果我有进一步的进展,也会继续在这里回复

自己顶一下,希望更多的朋友看到,参与到 appium 框架二次开发中来

dy20082250 回复

能加个好友吗

fy 回复

可以呀

fy · #12 · 2017年12月12日
仅楼主可见

我也是在搞这一块纠结着,通过 AppiumDriver 的方式实现时,会有很多 AndroidDriver 的方法不可用。比如 hideKeyBoard() 等方法不能调用。楼主有什么好方法希望分享下。

你查查 api 有没有判断是什么手机,然后调用相应的 driver,这是我想到的

匿名 #2 · 2017年12月23日

试试把 iosDriver() 和 androidDriver() 糅合成一个 webDriver() 方法呢?

不用 AppiumDriver 的话,楼主最后怎么实现的?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册