JAVA 自定义注解在自动化测试中的使用
在UI自动化测试中,相信很多人都喜欢用所谓的PO模式,其中的P,也就是page的意思,于是乎,在脚本里,或者在其它的page里,会要new很多的page对象,这样很麻烦,前面我们也讲到了注解的使用,很方便,那么我们可不可以用注解来代替这个new的过程呢?只有想不到,没有办不到的,因为springMVC就是用了这个方式来IOC,当然我们也可以直接用springMVC,但这无异于用牛刀来切豆腐,还不如我们自已实现一下,顺便增加一下对注解的使用的认识,代码如下:
1.先定义一个LoadPage的注解:
package com.test.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoadPage {
String value();
}
2.再来实现一下这个注解:
package com.test.annotation; import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List; import org.openqa.selenium.WebDriver; public class LoadAllPage { public final String basePath = "com.test"; private final String binPath = "bin"; private List<String> allClass = new ArrayList<String>(); private WebDriver driver; public void setDriver(WebDriver driver) {
this.driver = driver;
} public void loadAllPage(){
this.listAllFiles(binPath+File.separator+basePath.replace(".","/"));
this.getPageInstance();
} private void listAllFiles(String path){
path = path.replace("\\", "/");
File file = new File(path);
if(file.isFile() && file.getName().endsWith(".class")){
String filePath = file.getPath().replace("\\", "/");
int startIndex = 4;
int endIndex = filePath.lastIndexOf(".class");
allClass.add(filePath.substring(startIndex, endIndex).replace("/", "."));
}else if(file.isDirectory()){
File[] files = file.listFiles();
for (File f : files) {
this.listAllFiles(f.getPath());
}
}
} private void getPageInstance(){
for (String clazz : allClass) {
try {
Class<?> c = Class.forName(clazz);
if(c.isAnnotationPresent(LoadPage.class)){
LoadPage lp = c.getAnnotation(LoadPage.class);
Constructor<?> cons = c.getConstructor(WebDriver.class);
InitialManger.allInstance.put(lp.value(), cons.newInstance(driver));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
LoadAllPage lap = new LoadAllPage();
lap.loadAllPage();
}
}
3.再定义一个Page注解:
package com.test.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Page {
public String name() default "";
}
4.同样的,需要实现下Page注解
package com.test.annotation; import java.lang.reflect.Field;
import java.util.Iterator; public class AutoPage { public void setPageAnnotation(){
Iterator<String> it = InitialManger.allInstance.keySet().iterator();
while(it.hasNext()){
String key = it.next();
try {
Class<?> c = InitialManger.allInstance.get(key).getClass();
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if(field.isAnnotationPresent(Page.class)){
field.set(InitialManger.allInstance.get(key), InitialManger.allInstance.get(field.getAnnotation(Page.class).name()));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
} public void setTestAnnotation(Object o) {
try {
Class<?> c = o.getClass();
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if(field.isAnnotationPresent(Page.class)){
field.set(o, InitialManger.allInstance.get(field.getAnnotation(Page.class).name()));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} }
5.增加一个所有page实例化后的对象管理类:
package com.test.annotation; import java.util.HashMap;
import java.util.Map; public class InitialManger { public static Map<String, Object> allInstance = new HashMap<String, Object>(); }
6.再来初始化一下实现注解类:
package com.test.annotation; import org.openqa.selenium.WebDriver; public class InitialAnnotation { private WebDriver driver; public InitialAnnotation(WebDriver driver) {
this.driver = driver;
} public void initialAnnotation(){
LoadAllPage lap = new LoadAllPage();
lap.setDriver(driver);
lap.loadAllPage();
AutoPage ap = new AutoPage();
ap.setPageAnnotation();
} }
7.接下来就是使用了:在一个Page中加上这个@LoadPage注解:
package com.test.page; import org.openqa.selenium.WebDriver; import com.test.annotation.LoadPage;
import com.test.base.Page; @LoadPage("firstPage")
public class FirstPage extends Page{ public FirstPage(WebDriver driver) {
super(driver);
} public void linkToMobileList(){
driver.navigate().to("http://www.baidu.com");
} }
8.为了使@Page注解在case中能用到,所以得在TestBase的@BeforeClass中添加如下代码:
if(InitialManger.allInstance.isEmpty()){
InitialAnnotation init = new InitialAnnotation(driver);
init.initialAnnotation();
}
AutoPage ap = new AutoPage();
ap.setTestAnnotation(this);
9.在CASE中这样用即可:
package com.test.testcases; import java.util.Map; import org.testng.annotations.Test; import com.test.annotation.Page;
import com.test.base.TestBase;
import com.test.page.FirstPage; public class Test2 extends TestBase{ @Page(name="firstPage")
private FirstPage firstPage; @Test(dataProvider="providerMethod")
public void testLogin(Map<String, String> param){
firstPage.linkToMobileList();
} }
整个过程就是这样的,可能有人会说这样也不方便,等等等等,总是有人能接受,有人不能接受的,如果能接受,可以找我共同讨论一下。QQ:408129370
JAVA 自定义注解在自动化测试中的使用的更多相关文章
- java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题
一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...
- java自定义注解类
一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...
- java自定义注解实现前后台参数校验
2016.07.26 qq:992591601,欢迎交流 首先介绍些基本概念: Annotations(also known as metadata)provide a formalized way ...
- java自定义注解注解方法、类、属性等等【转】
http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...
- Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)
Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...
- JAVA自定义注解 ------ Annotation
日常开发工作中,合理的使用注解,可以简化代码编写以及使代码结构更加简单,下面记录下,JAVA自定义注解的开发过程. 定义注解声明类. 编写注解处理器(主要起作用部分). 使用注解. 相关知识点介绍, ...
- Java自定义注解和运行时靠反射获取注解
转载:http://blog.csdn.net/bao19901210/article/details/17201173/ java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编 ...
- Java自定义注解的实现
Java自定义注解的实现,总共三步(eg.@RandomlyThrowsException): 1.首先编写一个自定义注解@RandomlyThrowsException package com.gi ...
- JAVA自定义注解
在学习使用Spring和MyBatis框架的时候,使用了很多的注解来标注Bean或者数据访问层参数,那么JAVA的注解到底是个东西,作用是什么,又怎样自定义注解呢?这篇文章,即将作出简单易懂的解释. ...
随机推荐
- 【读书笔记】《深入浅出Webpack》
Webpack版本 分析版本为3.6.0 4.0为最近升级的版本,与之前版本变化较大,编译输出的文件与3.0版本会不一致,目前项目中使用的版本3.0版本,所以基于3.0版本进行分析学习. Webpac ...
- Java Web报错: GET http://localhost:8080/ 404 (Not Found)
eclipse正常启动tomcat,但是 访问http://localhost:8080 报404错误 搞笑的是我访问服务器中的其他网页也可以打开 报错如下: 解决: 如果这3项都已经变灰色,删除配置 ...
- POJ2417 Discrete Logging【BSGS】(模板题)
<题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...
- Django 学习第二天——URL路由及模板渲染方式
URL 的概念及格式: URL的引入:客户端:知道了url 就可以去进行访问: 服务端:设置好了url,别人才能访问到我 URL :网址(全球统一资源定位符):由 协议,域名(ip port) ,路径 ...
- OutputStreramWriter和InputStreamReader类
整个IO类中除了字节流和字符流还包括字节和字符转换流. OutputStreramWriter将输出的字符流转化为字节流 InputStreamReader将输入的字节流转换为字符流 但是不管如何操作 ...
- 【随笔】借鉴 & KPI式设计
1. 别人(某成功案例)是这么做的,我们也就这么做吧 刚来组里一会就目睹了需求讨论会上的一场争执,大概就是某产品经理在解释需求解释到后面有些说不通了就说“xxx App是这么做的我觉得我们也可以这样做 ...
- Cordova项目config.xml添加android权限
最近在开发cordova项目,安卓APP需要调用照相机和系统相册,在添加安卓权限的时候,总是报错. 以下是部分config.xml代码 <platform name="android& ...
- PlantUML windows android
dot执行程序. 渲染 url 连接(花费大量时间) 错误 和 语法注释 (是还在实验的) 缓存大小 5 在键入和渲染之间的延迟 (毫秒) 100
- Altium Designer Summer 09换成中文步骤
1.打开Altium Designer Summer 09软件,在左上角file文件中点击,再打开Preferences出现如下,然后关闭软件在打开就完成了
- JVM调优总结 -Xms -Xmx -Xmn -Xss(转)
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...