自動化テストに jBehave/Selenium を使用しています。
次に、次のコードを使用して、ページに表示される要素を取得します。
public WebElement getVisibleElement( final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod ) {
return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until( new Function<WebDriver, WebElement>(){
public WebElement apply(WebDriver driver) {
try{
WebElement element = parentElement.findElement(by);
if ( element.isDisplayed() ) {
return element;
} else {
return null;
}
} catch( NoSuchElementException e ) {}
return null;
}
} );
}
問題は、要素がページに存在しない場合、Selenium がページ上でそれを見つけようとして多くの時間を費やすことです。そのような場合に長い時間を費やさないように、コードを最適化する方法はありますか?