2017-09-04 99 views
0

問題描述:需要創建自定義註釋,它創建以字符串作爲參數並對其進行處理,並在平靜框架中返回Weblement。 我已經通過自定義註釋+谷歌注入嘗試過代碼,但無法在運行時寧靜期間初始化我的頁面。 某人可以提供一些相同的指導嗎?Selenity框架自定義註釋創建初始化問題

代碼:

HomePage類

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}"}; 

} 

Proceess實現

 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)); 

我已經下文稱一些文檔,其使用s注入使用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

自定義註釋並不容易注入到現有的框架。您可以在頁面對象上使用@WhenPageOpens註釋來執行您需要執行的任何自定義設置,例如

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

(其中InitialiseAutoxpathFields是您爲編寫自定義註釋處理而編寫的類)。

+0

我試過了,但沒能注入我註釋處理。它無法調用我的課程。 – user216112

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; 
} 

}

+0

謝謝。我會檢查並且會更新你。 – user216112