2017-03-02 58 views
0

我正在尋找一種方式使用lambda來寫塊:硒的webdriver的Java:轉換表達與λ

public void waitUntilElemIsDisabled(WebElement element) { 
     try { 
      wait.until(new Function<WebDriver, Boolean>() { 
       @Override 
       public Boolean apply(WebDriver driver) { 
        return !element.isEnabled(); 
       } 
      }); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      logger.error(e.toString()); 
     } 
    } 

我嘗試wait.until(e -> element.isEnabled());但我發現了一個語法錯誤:

The method until(Predicate<WebDriver>) is ambiguous for the type FluentWait<WebDriver> 

如果有幫助,這是我的初始化wait

wait = new FluentWait<>(webDriver).withTimeout(impTimeout, s).pollingEvery(pollingMsInt, ms) 
        .ignoring(NoSuchElementException.class); 

我在使用lambda時相當新,目前跟在this指南之後,但我無法找到與我使用的模式相匹配的模式。

在此先感謝。

+1

看起來您可能正在使用不推薦的'until()'方法。看看這個[有點相關的答案](http://stackoverflow.com/a/42421762/1183506) – mrfreester

+0

我經常使用IDE的重構能力來代替使用我的大腦來解決這些問題。在IntelliJ中,我將點擊lambda表達式,使用Refactor/Extract to Value,如果需要更正類型參數,然後使用Refactor/Inline將該值重新集成到原始表達式中。通常,它會自動添加任何需要的轉換或顯式類型參數。 –

回答

0

管理解決這個問題。

解決方案:

private Function<WebDriver, Boolean> webElemBooleanFunction(boolean condition) { 
    return x -> condition; 
} 

調用使用:

public void waitUntilFieldIsPopulated(WebElement element) { 
    try { 
     wait.until(webElemBooleanFunction(getElementValue(element).length() > 0)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     logger.error(e.toString()); 
    } 
} 

感謝您的提示!