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 确实与前者有所不同。

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


↙↙↙阅读原文可查看相关链接,并与作者交流