serenity框架的java自定义注解创建初始化问题

vbopmzt1  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(457)

问题描述:需要创建自定义注解,该注解以字符串作为参数创建并处理它,然后在serenity框架中返回WebElement。我已经尝试了通过自定义注解+google注入的代码,但无法在运行时serenity期间初始化我的页面。有人能提供一些相同的指导吗?
代码:
主页类

public class Homepage {
    @FindBy(css = ".sbibod")
    public SearchForm searchForm; 

@AutoxpathAnnotation(ValuesPair = ".sbibod")
public WebElement searchForm2;

注解接口

//import net.serenitybdd.core.annotations.findby.How;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)

public @interface AutoxpathAnnotation {

          String[] ValuesPair() default {"{Customer Service Name2}"};

}

过程实现

Class c = obj.getClass(); 
// Here need to Pass HomePage Object, Don't Know How to Pass through Page Object Model. Also need to know where this function needs to be written.

        @SuppressWarnings("unchecked")
        Annotation an = c.getAnnotation(AutoxpathAnnotation.class);
        AutoxpathAnnotation ref = (AutoxpathAnnotation)an;
        xapthform = "//label[contains(text(),'"+VisibleText+"')]/../following-sibling::*/select";

            //Input will Handle Checkbox, Button and radioBox
        if (type.equals("input")) {
            xapthform = "//label[contains(text(),'"+VisibleText+"')]/../following-sibling::*/input";

        if (type.equals("textarea")) {
            xapthform = "//label[contains(text(),'"+VisibleText+"')]/../following-sibling::*/textarea";
        } 

        System.out.println("Searching values on the Screen: ");
        System.out.println("------------------------------------------------------");

        return (WebElement) getDriver().findElement(By.xpath(xapthform));

我参考了一些使用guice注入的文档

public class DriverModule extends AbstractModule implements MethodInterceptor {
    @Inject
    private WebDriver driver;

    private static Injector injector;

    @Override
    protected void configure() {
        bind(WebDriver.class)
            .toProvider(WebDriverProvider.class)
            .in(Singleton.class);

    //Todo some Operation

    }       

    But not sure how it will work exactly in RunTime.
kqlmhetl

kqlmhetl1#

我加了这个公关https://github.com/serenity-bdd/serenity-core/pull/1048 在宁静中实现自定义注解。
添加此新接口customfindbyannotationservice的实现并实现org.openqa.selenium.by,如本例所示:

public class ByReact extends By {

private static final char DOUBLE_QUOTE = '"';
private static final String SINGLE_QUOTE = "'";

private final String reactSelector;

public ByReact(String reactSelector){
    this.reactSelector = reactSelector;
}

@Override
public WebElement findElement(SearchContext context) {
    String jquery = "return __retractor(" + quoted(reactSelector) + ")[0];";
    return (WebElement) ((JavascriptExecutor) context).executeScript(jquery);
}

@Override
public List<WebElement> findElements(SearchContext context) {
    String jquery = "return __retractor(" + quoted(reactSelector) + ");";
    return (List<WebElement>) ((JavascriptExecutor) context).executeScript(jquery);
}

private String quoted(final String reactSelector) {
    if (reactSelector.contains("'")) {
        return DOUBLE_QUOTE + reactSelector + '"';
    } else {
        return  "'" + reactSelector + SINGLE_QUOTE;
    }
}

public String toString() {
    return "By.ByReact: " + reactSelector;
}

}

uklbhaso

uklbhaso2#

自定义注解并不是那么容易注入到现有框架中。您可以在页面对象上使用@whenpageopens注解来执行任何需要执行的自定义设置,例如。

@WhenPageOpens
public void injectCustomFields() {
    InitialiseAutoxpathFields.in(this);
}

(其中initialiseautoxpathfields是您编写的用于进行自定义注解处理的类)。

相关问题