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