java 反射和注解 (Reflect and Annotation)
来源:互联网 发布:阿里妈妈和淘宝联盟 编辑:程序博客网 时间:2024/04/28 23:41
========Annotation One============package com.my.core;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface TestFindBy {String id() default "default_id";}==========Annotation Two=============package com.my.core;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface NoteClass{String note() default "default";}=========Test Class============----------------------------package com.my.core;@NoteClasspublic class TestPage {@TestFindBy(id="user_Name")public ScreenElement userNameField;@TestFindBy(id="user_Password")public ScreenElement userPwdField;@TestFindBy(id="user_submit")public ScreenElement userSubmit;}----------------------package com.my.core;public class ScreenElement {public String id;public ScreenElement(String id) {this.id = id;}public String getId() {return id;}public void setId(String id) {this.id = id;}public By findBy() {return By.id(this.id);}}----------------------package com.my.core;import java.lang.reflect.Field;public class ScreenFactory { public static void main(String arg[]) throws IllegalArgumentException, IllegalAccessException{ TestPage pageObj= ScreenFactory.initElements(new TestPage()); System.out.println(pageObj.userNameField.getId()); System.out.println(pageObj.userPwdField.getId()); System.out.println(pageObj.userSubmit.getId()); WebDriver driver = new ChromeDriver(); waitUntilVisibleThenSendkey(driver, pageObj.userNameField, "Tom"); waitUntilVisibleThenSendkey(driver, pageObj.userPwdField, "Pwd"); waitUntilClickableThenClick(driver, pageObj.userSubmit); } public static<T extends TestPage> T initElements(T screen) throws IllegalArgumentException, IllegalAccessException{ if(TestPage.class.isAnnotationPresent(NoteClass.class)){ Field[] fs= TestPage.class.getDeclaredFields(); for(Field f:fs){ if(f.isAnnotationPresent(TestFindBy.class)){ String anId=f.getAnnotation(TestFindBy.class).id(); f.set(screen, new ScreenElement(anId)); } }public static void waitUntilVisibleThenSendkey(WebDriver driver,ScreenElement se,String keys) { for(int i=0;i<5;i++){ WebElement we =new WebDriverWait(driver, 30) .until(ExpectedConditions.visibilityOfElementLocated(se.findBy())); try{ we.getText(); we.sendKeys(keys);; return;} catch (Throwable t) {System.out.println(t.getMessage());} }} }return screen; } public static void waitUntilClickableThenClick(WebDriver driver,ScreenElement se) { for(int i=0;i<5;i++){ WebElement we =new WebDriverWait(driver, 30) .until(ExpectedConditions.elementToBeClickable(se.findBy())); try{ we.getText(); we.click(); return; }catch(Throwable t){ System.out.println(t.getMessage()); } }}}
========Annotation One============
package com.my.core;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestFindBy {
String id() default "default_id";
}
==========Annotation Two=============
package com.my.core;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoteClass{
String note() default "default";
}
=========Test Class============
----------------------------
package com.my.core;
@NoteClass
public class TestPage {
@TestFindBy(id="user_Name")
public ScreenElement userNameField;
@TestFindBy(id="user_Password")
public ScreenElement userPwdField;
@TestFindBy(id="user_submit")
public ScreenElement userSubmit;
}
----------------------
package com.my.core;
public class ScreenElement {
public String id;
public ScreenElement(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public By findBy() {
return By.id(this.id);
}
----------------------
package com.my.core;
import java.lang.reflect.Field;
public class ScreenFactory {
public static void main(String arg[]) throws IllegalArgumentException, IllegalAccessException{
TestPage pageObj= ScreenFactory.initElements(new TestPage());
System.out.println(pageObj.userNameField.getId());
System.out.println(pageObj.userPwdField.getId());
System.out.println(pageObj.userSubmit.getId());
WebDriver driver = new ChromeDriver();
waitUntilVisibleThenSendkey(driver, pageObj.userNameField, "Tom");
waitUntilVisibleThenSendkey(driver, pageObj.userPwdField, "Pwd");
waitUntilClickableThenClick(driver, pageObj.userSubmit);
public static<T extends TestPage> T initElements(T screen) throws IllegalArgumentException, IllegalAccessException{
if(TestPage.class.isAnnotationPresent(NoteClass.class)){
Field[] fs= TestPage.class.getDeclaredFields();
for(Field f:fs){
if(f.isAnnotationPresent(TestFindBy.class)){
String anId=f.getAnnotation(TestFindBy.class).id();
f.set(screen, new ScreenElement(anId));
}
}
public static void waitUntilVisibleThenSendkey(WebDriver driver,ScreenElement se,String keys) {
for(int i=0;i<5;i++){
WebElement we =new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(se.findBy()));
try{
we.getText();
we.sendKeys(keys);;
return;
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
}
}
return screen;
}
public static void waitUntilClickableThenClick(WebDriver driver,ScreenElement se) {
for(int i=0;i<5;i++){
WebElement we =new WebDriverWait(driver, 30)
.until(ExpectedConditions.elementToBeClickable(se.findBy()));
try{
we.getText();
we.click();
return;
}catch(Throwable t){
System.out.println(t.getMessage());
}
}
}
- java 反射和注解 (Reflect and Annotation)
- 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句
- java 自定义注解annotation和通过反射获取注解
- 黑马程序员----------------------反射reflect和注解学习总结
- java 注解annotation的使用,以及反射如何获取注解
- java 注解annotation的使用,以及反射如何获取注解
- java 注解annotation的使用,以及反射如何获取注解
- java反射和注解
- java反射和注解
- Java注解和反射
- 枚举和注解(Enum and Annotation)
- java 反射机制 reflect
- java反射机制reflect
- Java 反射,reflect
- Java reflect 反射
- java reflect 反射
- java反射reflect
- Java反射机制(reflect)
- android studio启动慢解决办法
- tomcat7+nginx 负载均衡
- Redis教程(七):Key操作命令详解
- Qt处理HTML格式文本的方法
- 1092. To Buy or Not to Buy (20)
- java 反射和注解 (Reflect and Annotation)
- poj 1321 棋盘问题
- iOS 沙盒目录结构解析
- 1093. Count PAT's (25)
- 测试
- 自己动手丰衣足食之移动端城市选择插件
- LeetCode96. Unique Binary Search Trees简单,简洁
- PAT1024科学计数法
- 使用 VisualVM 进行性能分析及调优(转)