Skip to content

Commit

Permalink
Implemented the timeoutInSeconds attribute on the FindBy annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Mar 18, 2015
1 parent 7f611da commit d8ccfda
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,4 @@ public List<WebElementFacade> thenFindAll(

public void shouldContainElements(String xpathOrCssSelector);



}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class SmartAjaxElementLocator extends SmartElementLocator implements WithConfigurableTimeout {
public static final Duration ZERO_SECONDS = new Duration(0, TimeUnit.SECONDS);
protected int timeOutInSeconds;
protected Optional<Integer> annotatedTimeoutInSecondes;
protected Optional<Integer> annotatedTimeoutInSeconds;
private final Clock clock;

private final Field field;
Expand All @@ -50,18 +50,17 @@ public SmartAjaxElementLocator(SearchContext searchContext, Field field, MobileP
public SmartAjaxElementLocator(Clock clock, SearchContext searchContext, Field field, MobilePlatform platform, Integer defaultTimeoutInSeconds) {
super(searchContext, field, platform);
this.timeOutInSeconds = defaultTimeoutInSeconds;
this.annotatedTimeoutInSecondes = timeoutFrom(field);
this.annotatedTimeoutInSeconds = timeoutFrom(field);
this.clock = clock;
this.field = field;
if ((searchContext instanceof WebDriverFacade) || (searchContext instanceof WebElementFacade)) {
this.searchContext = searchContext;
if (searchContext instanceof WebDriverFacade){
this.searchContext = ((WebDriverFacade) searchContext)
.withTimeoutOf(new Duration(annotatedTimeoutInSeconds.or(defaultTimeoutInSeconds), TimeUnit.SECONDS));
} else if (searchContext instanceof WebDriver) {
this.searchContext = WebdriverProxyFactory.getFactory().proxyFor((WebDriver) searchContext);
} else {
this.searchContext = searchContext;
}


this.platform = platform;
}

Expand Down Expand Up @@ -114,16 +113,16 @@ protected boolean isElementUsable(WebElement element) {
* Will poll the interface on a regular basis until the element is present.
*/
public WebElement ajaxFindElement() {
SlowLoadingElement loadingElement = new SlowLoadingElement(clock, annotatedTimeoutInSecondes.or(timeOutInSeconds));
SlowLoadingElement loadingElement = new SlowLoadingElement(clock, annotatedTimeoutInSeconds.or(timeOutInSeconds));
try {
return loadingElement.get().getElement();
} catch (ElementNotVisibleAfterTimeoutError notVisible) {
throw new ElementNotVisibleException(
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSecondes.or(timeOutInSeconds), notVisible.getMessage()),
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSeconds.or(timeOutInSeconds), notVisible.getMessage()),
notVisible.getCause());
} catch (Error e) {
throw new NoSuchElementException(
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSecondes.or(timeOutInSeconds), e.getMessage()),
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSeconds.or(timeOutInSeconds), e.getMessage()),
e.getCause());
}
}
Expand All @@ -137,12 +136,12 @@ public List<WebElement> findElements() {
if (aPreviousStepHasFailed()) {
return EMPTY_LIST_OF_WEBELEMENTS;
}
SlowLoadingElementList list = new SlowLoadingElementList(clock, annotatedTimeoutInSecondes.or(timeOutInSeconds));
SlowLoadingElementList list = new SlowLoadingElementList(clock, annotatedTimeoutInSeconds.or(timeOutInSeconds));
try {
return list.get().getElements();
} catch (Error e) {
throw new NoSuchElementException(
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSecondes.or(timeOutInSeconds), e.getMessage()),
String.format("Timed out after %d seconds. %s", annotatedTimeoutInSeconds.or(timeOutInSeconds), e.getMessage()),
e.getCause());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.openqa.selenium.TimeoutException
import spock.lang.Specification
import spock.lang.Unroll

import java.util.concurrent.TimeUnit

import static java.util.concurrent.TimeUnit.MILLISECONDS
import static java.util.concurrent.TimeUnit.SECONDS

Expand Down Expand Up @@ -418,4 +420,31 @@ class WhenManagingWebdriverTimeouts extends Specification {
page.country.isCurrentlyVisible()
}
def "You can check whether a child element is present using a By selector"() {
when:
environmentVariables.setProperty("webdriver.timeouts.implicitlywait","0")
page = openTestPageUsing(defaultBrowser)
then:
page.clients.shouldContainElements(By.cssSelector(".color"))
and:
page.clients.shouldContainElements(".color")
}
def "You can check whether a child element is present"() {
when:
environmentVariables.setProperty("webdriver.timeouts.implicitlywait","0")
page = openTestPageUsing(defaultBrowser)
then:
page.clients.containsElements(By.cssSelector(".color"))
and:
!page.clients.containsElements(By.cssSelector(".flavor"))
}
def "You can check whether a child element is present with waits"() {
when:
page = openTestPageUsing(defaultBrowser)
then:
page.clients.withTimeoutOf(0, TimeUnit.SECONDS).containsElements(By.cssSelector(".color"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class StaticSitePage extends PageObject {

public WebElement focusmessage;

public WebElement clients;
public WebElementFacade clients;

public WebElement clients_with_no_headings;

Expand Down

0 comments on commit d8ccfda

Please sign in to comment.