uiautomator 仿 WebDriver 封装,页面工厂制作,实现多语言定位
来源:互联网 发布:网络行为管理服务器 编辑:程序博客网 时间:2024/06/03 11:15
转载地址: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 0
- uiautomator 仿 WebDriver 封装,页面工厂制作,实现多语言定位
- WebDriver工厂类封装
- webdriver定位页面元素插件-WebDriver Element Locator
- selenium webdriver xpath 定位页面元素
- java selenium webdriver实战 页面元素定位
- 【WebDriver】selenium使用CSS定位页面元素
- 【Selenium】webdriver进行页面元素定位
- WebDriver封装
- WebDriver拾级而上·之三 定位页面元素
- selenium webdriver学习(四)------------定位页面元素
- WebDriver拾级而上·之三 定位页面元素
- 【selenium学习笔记】webdriver进行页面元素定位
- python+selenuim webdriver 页面跳转后如何定位元素
- Webdriver学习笔记(五)页面元素定位补充(list和层级定位)
- C语言实现封装
- C语言实现封装
- 仿QQ悬挂窗口的实现封装
- WebDriver--定位UI元素
- 100天土鸡饲养计划(1)
- 'telnet'不是内部或外部命令,也不是可运行的程序或批处理文件。
- GUI界面,客户端向服务端传送文件
- php 给文件夹下的所有图片批量打水印
- 用ChemDraw创建E-Notebook数据库的方法
- uiautomator 仿 WebDriver 封装,页面工厂制作,实现多语言定位
- 从百度、谷歌到优必选、天机智讯 人工智能到底会通向哪里
- 位运算实现整数的加减乘除
- Eclipse快捷键 10个最有用的快捷键
- oracle adg 启动报ORA-03113错误
- Java 请求https 的各种方式详细代码
- Bootstrap 使用
- CSS中的background和background-color的区别
- Material-CalendarView实现的旅行日历