java 反射和注解 (Reflect and Annotation)




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

}
}









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

}

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值