7

我正在使用 selenium pagefactory,并引入了一个注释 @Name(Description = "Username"),我将其用于所有 WebElement。稍后我需要在我的自定义方法中找到 Description 的值,以进行报告,例如:

public static void click(WebElement element) throws Throwable {
        try {
            element.click();
        } catch(ElementNotInteractableException E1) {
            throw new UnableToInteractWithElementException("Unable To Interact With " + WebElement Descriptionn);
        }
    }

我的@Name 注释界面和 pagefactory 如下所示:

名称接口

@Target({ElementType.FIELD, ElementType.TYPE,ElementType.CONSTRUCTOR})
public @interface Name {    
    String Description() default "";
}
@Name(Description = "Username")
@FindBy(id="txtLoginID")
public WebElement username;

我在使用反射时遇到的问题是需要定义 pagefactory 的类名,并将字段名作为字符串 "username" 提供,以检索注释值。

我希望能够通过仅向我的 click(WebElement element) 方法提供我的 WebElement 而没有其他对象来检索注释值。

4

2 回答 2

2

我可以向您展示如何使用反射部分获取描述。

首先,我们必须修复注释@Name,因为您没有添加RetentionPolicy

@Target({ElementType.FIELD, ElementType.TYPE, ElementType.CONSTRUCTOR})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Name {
    String Description() default "";
}

现在,我所做的和测试的,我们为名称创建存储,我们将在初始化页面对象时存储它们,如下所示:

public class NameStore {
    public static HashMap<WebElement, String> map = new HashMap<>();

    public static void store(Object pageObject) {
        Field[] fields = pageObject.getClass().getDeclaredFields();
        for (Field field : fields) {
            Name annotation = field.getAnnotation(Name.class);
            if (annotation != null) {
                try {
                    map.put((WebElement) field.get(pageObject), annotation.Description());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getDescription(WebElement element) {
        return map.get(element);
    }
}

您将页面对象作为参数传递,@Name注释被读取并存储到HashMap<WebElement, String>

初始化存储在构造函数中的名称:

    public PageObject(WebDriver driver) {
        PageFactory.initElements(driver, this);
        NameStore.store(this); //THIS IS CRUCIAL. Also make sure that's always AFTER PageFactory initialization
    }

现在,要更新您的click方法:

public static void click(WebElement element) throws Throwable {
        try {
            element.click();
        } catch(ElementNotInteractableException E1) {
            throw new UnableToInteractWithElementException("Unable To Interact With " + NameStore.get(element));
        }
    }

希望能帮助到你!

免责声明:我没有在多线程的上下文中对其进行测试。我没有在构造函数的上下文中进行测试。

于 2019-08-30T14:04:23.377 回答
1

嗨,我不是反思的专业人士,我不确定我的问题是否正确。您可能有另一种最佳方法,但遵循一个解决方案,创建一个接收 WebElement 并返回描述的方法:

private static String getDescription(WebElement element) throws IllegalAccessException {
    //Get all the fields of the page object.
    for (Field field : YOUR_PAGE_OBJECT.class.getDeclaredFields()) {
        Name name = field.getAnnotation(Name.class);
        //Consider only the ones that is annotated by your @Name
        if (name != null) {
            WebElement classElement = (WebElement) field.get(element);
            //Get the element from the class and compare with your current.
            if (classElement != null && (classElement).equals(element)) {
                return name.Description();
            }
        }
    }
    return ":C"; // or throw something, whatever you want...
}

您的代码将结束如下:

public static void click(WebElement element) throws Throwable {
        try {
            element.click();
        } catch(ElementNotInteractableException E1) {
            throw new UnableToInteractWithElementException("Unable To Interact With " + getDescription(element));
        }
    }
于 2019-08-23T21:46:54.410 回答