问题描述:需要创建自定义注释,创建将字符串作为参数并处理它并在宁静框架中返回Weblement。 我通过自定义注释+谷歌注入尝试了代码,但无法在运行时平静期间初始化我的页面。 somebod可以提供相同的指导吗?
代码:
HomePage Class
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));
我已经提到了一些使用injection-using-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.
答案 0 :(得分:0)
自定义注释并不容易注入现有框架。您可以在页面对象上使用@WhenPageOpens注释来执行您需要执行的任何自定义设置,例如
@WhenPageOpens
public void injectCustomFields() {
InitialiseAutoxpathFields.in(this);
}
(其中InitialiseAutoxpathFields是您编写的用于执行自定义注释处理的类。)
答案 1 :(得分:0)
我添加了此PR https://github.com/serenity-bdd/serenity-core/pull/1048以在Serenity中实现自定义注释。
添加此新接口的实现 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;
}
}