Uiautomator仿WebDriver封装

本帖已被设为精华帖!,

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();
}
}

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

* 注:本文来自网络投稿,不代表本站立场,如若侵犯版权,请及时知会删除