uiautomator 仿 WebDriver 封装,页面工厂制作,实现多语言定位

转载地址:https://testerhome.com/topics/5454


uiautomator仿webdriver封装,让写代码根据友好,方便
用uiautomator也有一段时间了,分享下自己的成果,也希望以28原则,让自动化变得更美丽
要是有好的建议,欢迎骚扰

driver页封装类

    private UiDevice driver = null;
    public DriverBase(UiDevice uidevice) {
        if (uidevice == null) {
            this.driver = UiDevice.getInstance();
        } else {
            this.driver = uidevice;
        }
    }
    public UiDevice getDriver() {
        return this.driver;
    }
    public UiObject findElement(UiSelector by) {
        Logs.logInfo("查找->" + by.toString().substring(10));
        return new UiObject(by);
    }
    public UiScrollable findElementByscrollable(UiSelector by) {
        Logs.logInfo("查找可滚动->" + by.toString().substring(10));
        return new UiScrollable(by);
    }
    public UiCollection findElementByUiCollection(UiSelector by) {
        Logs.logInfo("查找->" + by.toString().substring(10));
        return new UiCollection(by);
    }

java注解类

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
public @interface FindBy {
    String id() default "";

    String text() default "";

    int index() default -1;

    String desc() default "";

    Class<?> className() default FindBy.class;

    String checkable() default "";

    int instance() default -1;

}

页面工厂类
每个页面做成一个page,方便管理,可根据配置,自定义语言

public class HomePage {

    static {
        new PageObjectBase().initElements(HomePage.class);
    }

    @FindByCN(text="你好")
    @FindBy(className = ImageView.class, index = 5, instance = 3)
    public static UiSelector demo01;

    @FindByCN(text = "主页")
    @FindBy(text = "Home")
    public static UiSelector home;

    @FindByCN(text = "我")
    @FindBy(text = "Me")
    public static UiSelector me;

    @FindBy(id = "com.xxxx.xxxxx:id/xxxx")
    public static UiSelector liveMakeup;

    @FindBy(text = "Are you sure you want to quit the application?")
    public static UiSelector exitDialog;

}

利用java反射,初始化页面工厂

public class PageObjectBase {

    public void initElements(Class<?> clazz) {
        Field[] field = clazz.getDeclaredFields();
        for (Field f : field) {
            if (Configs.TEST_LANGUAGE.equals("CN")) {
                FindByCN fbCN = f.getAnnotation(FindByCN.class);
                if (fbCN == null) {
                    findByInit(f);
                } else {
                    findByCNInit(f, fbCN);
                }
            } else {
                findByInit(f);
            }
        }
    }
    private void findByCNInit(Field field, FindByCN fb) {
        UiSelector By = new UiSelector();
        try {
            if (fb.text() != "") {
                By = By.text(fb.text());
            }
            if (fb.id() != "") {
                By = By.resourceId(fb.id());
            }
            if (fb.className() != FindBy.class) {
                By = By.className(fb.className());
            }
            if (fb.index() != -1) {
                By = By.index(fb.index());
            }
            if (fb.desc() != "") {
                By = By.description(fb.desc());
            }
            if (fb.checkable() != "") {
                By = By.checkable(Boolean.valueOf(fb.checkable()));
            }
            if (fb.instance() != -1) {
                By = By.instance(fb.instance());
            }

            field.setAccessible(true);
            field.set(this, By);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    private void findByInit(Field field) {
        FindBy fb = field.getAnnotation(FindBy.class);
        if (fb == null) {
            return;
        }
        UiSelector By = new UiSelector();
        try {
            if (fb.text() != "") {
                By = By.text(fb.text());
            }
            if (fb.id() != "") {
                By = By.resourceId(fb.id());
            }
            if (fb.className() != FindBy.class) {
                By = By.className(fb.className());
            }
            if (fb.index() != -1) {
                By = By.index(fb.index());
            }
            if (fb.desc() != "") {
                By = By.description(fb.desc());
            }
            if (fb.checkable() != "") {
                By = By.checkable(Boolean.valueOf(fb.checkable()));
            }
            if (fb.instance() != -1) {
                By = By.instance(fb.instance());
            }

            field.setAccessible(true);
            field.set(this, By);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

}

TestCase编写
是不是代码风格跟WebDriver一样了

public class Demo01 extends TestCaseBase {
    public void test01() throws UiObjectNotFoundException {
        driver.findElement(HomePage.demo01).click();
        driver.findElement(By.text("测试者")).click();
    }
}

代码写的不是非常好,不要嘲笑,但功能都有了。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值