问题描述:需要创建自定义注解,该注解以字符串作为参数创建并处理它,然后在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.
2条答案
按热度按时间kqlmhetl1#
我加了这个公关https://github.com/serenity-bdd/serenity-core/pull/1048 在宁静中实现自定义注解。
添加此新接口customfindbyannotationservice的实现并实现org.openqa.selenium.by,如本例所示:
}
uklbhaso2#
自定义注解并不是那么容易注入到现有框架中。您可以在页面对象上使用@whenpageopens注解来执行任何需要执行的自定义设置,例如。
(其中initialiseautoxpathfields是您编写的用于进行自定义注解处理的类)。