From 6f1628bc721463cdb3e97ee3700ef979b5dc7d8d Mon Sep 17 00:00:00 2001 From: John Ferguson Smart Date: Tue, 30 Jun 2015 16:31:08 +1000 Subject: [PATCH] Fixed #87 --- .../serenitybdd/core/pages/PageObject.java | 8 ++- .../core/pages/RenderedPageObjectView.java | 69 ++++++++++++++----- .../core/pages/WebElementFacadeImpl.java | 59 +++++++--------- 3 files changed, 82 insertions(+), 54 deletions(-) diff --git a/serenity-core/src/main/java/net/serenitybdd/core/pages/PageObject.java b/serenity-core/src/main/java/net/serenitybdd/core/pages/PageObject.java index 3baca0bd24..3f56b12e08 100644 --- a/serenity-core/src/main/java/net/serenitybdd/core/pages/PageObject.java +++ b/serenity-core/src/main/java/net/serenitybdd/core/pages/PageObject.java @@ -361,12 +361,18 @@ public PageObject waitForTextToAppear(final WebElement element, return this; } + private boolean driverIsDisabled() { + return StepEventBus.getEventBus().webdriverCallsAreSuspended(); + } + /** * Waits for a given text to disappear from the element. */ public PageObject waitForTextToDisappear(final WebElement element, final String expectedText) { - waitForCondition().until(elementDoesNotContain(element, expectedText)); + if (!driverIsDisabled()) { + waitForCondition().until(elementDoesNotContain(element, expectedText)); + } return this; } diff --git a/serenity-core/src/main/java/net/serenitybdd/core/pages/RenderedPageObjectView.java b/serenity-core/src/main/java/net/serenitybdd/core/pages/RenderedPageObjectView.java index 77281b8194..53fbc1760a 100644 --- a/serenity-core/src/main/java/net/serenitybdd/core/pages/RenderedPageObjectView.java +++ b/serenity-core/src/main/java/net/serenitybdd/core/pages/RenderedPageObjectView.java @@ -6,6 +6,7 @@ import net.serenitybdd.core.time.Stopwatch; import net.thucydides.core.scheduling.NormalFluentWait; import net.thucydides.core.scheduling.ThucydidesFluentWait; +import net.thucydides.core.steps.StepEventBus; import net.thucydides.core.webdriver.ConfigurableTimeouts; import net.thucydides.core.webdriver.WebDriverFacade; import org.openqa.selenium.*; @@ -54,6 +55,10 @@ public RenderedPageObjectView(final WebDriver driver, final PageObject pageObjec this.timeoutCanBeOverriden = timeoutCanBeOverriden; } + private boolean driverIsDisabled() { + return StepEventBus.getEventBus().webdriverCallsAreSuspended(); + } + public ThucydidesFluentWait waitForCondition() { return new NormalFluentWait<>(driver, webdriverClock, sleeper) .withTimeout(waitForTimeout.in(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS) @@ -85,7 +90,9 @@ public String toString() { * This method will wait until an element is present and visible on the screen. */ public void waitFor(final By byElementCriteria) { - waitForCondition().until(elementDisplayed(byElementCriteria)); + if (!driverIsDisabled()) { + waitForCondition().until(elementDisplayed(byElementCriteria)); + } } public void waitFor(final ExpectedCondition expectedCondition) { @@ -108,12 +115,16 @@ public List waitFor(final List webElements) } private WebElementFacade waitForElement(final WebElementFacade webElement) { - waitForCondition().until(elementIsDisplayed(webElement)); + if (!driverIsDisabled()) { + waitForCondition().until(elementIsDisplayed(webElement)); + } return webElement; } private List waitForElements(final List elements) { - waitForCondition().until(elementsAreDisplayed(elements)); + if (!driverIsDisabled()) { + waitForCondition().until(elementsAreDisplayed(elements)); + } return elements; } @@ -225,7 +236,9 @@ public String toString() { } public void waitForText(final String expectedText) { - waitForCondition().until(textPresent(expectedText)); + if (!driverIsDisabled()) { + waitForCondition().until(textPresent(expectedText)); + } } private ExpectedCondition textPresentInElement(final WebElement element, final String expectedText) { @@ -242,7 +255,9 @@ public String toString() { } public void waitForText(final WebElement element, final String expectedText) { - waitForCondition().until(textPresentInElement(element, expectedText)); + if (!driverIsDisabled()) { + waitForCondition().until(textPresentInElement(element, expectedText)); + } } private ExpectedCondition titlePresent(final String expectedTitle) { @@ -258,7 +273,9 @@ public String toString() { } public void waitForTitle(final String expectedTitle) { - waitForCondition().until(titlePresent(expectedTitle)); + if (!driverIsDisabled()) { + waitForCondition().until(titlePresent(expectedTitle)); + } } private boolean titleIs(final String expectedTitle) { @@ -287,15 +304,19 @@ public String toString() { } public void waitForTextToDisappear(final String expectedText, final long timeout) { - waitForCondition() - .withTimeout(timeout, TimeUnit.MILLISECONDS) - .until(textNotPresent(expectedText)); + if (!driverIsDisabled()) { + waitForCondition() + .withTimeout(timeout, TimeUnit.MILLISECONDS) + .until(textNotPresent(expectedText)); + } } public void waitForTextToAppear(final String expectedText, final long timeout) { - waitForCondition() - .withTimeout(timeout, TimeUnit.MILLISECONDS) - .until(textPresent(expectedText)); + if (!driverIsDisabled()) { + waitForCondition() + .withTimeout(timeout, TimeUnit.MILLISECONDS) + .until(textPresent(expectedText)); + } } private ExpectedCondition titleNotPresent(final String expectedTitle) { @@ -311,7 +332,9 @@ public String toString() { } public void waitForTitleToDisappear(final String expectedTitle) { - waitForCondition().until(titleNotPresent(expectedTitle)); + if (!driverIsDisabled()) { + waitForCondition().until(titleNotPresent(expectedTitle)); + } } private ExpectedCondition anyTextPresent(final String... expectedTexts) { @@ -323,7 +346,9 @@ public Boolean apply(WebDriver driver) { } public void waitForAnyTextToAppear(final String... expectedTexts) { - waitForCondition().until(anyTextPresent(expectedTexts)); + if (!driverIsDisabled()) { + waitForCondition().until(anyTextPresent(expectedTexts)); + } } private ExpectedCondition anyTextPresentInElement(final WebElement element, final String... expectedTexts) { @@ -341,7 +366,9 @@ public String toString() { } public void waitForAnyTextToAppear(final WebElement element, final String... expectedTexts) { - waitForCondition().until(anyTextPresentInElement(element, expectedTexts)); + if (!driverIsDisabled()) { + waitForCondition().until(anyTextPresentInElement(element, expectedTexts)); + } } @@ -386,7 +413,9 @@ public String toString() { } public void waitForAllTextToAppear(final String... expectedTexts) { - waitForCondition().until(allTextPresent(expectedTexts)); + if (!driverIsDisabled()) { + waitForCondition().until(allTextPresent(expectedTexts)); + } } private ExpectedCondition elementNotDisplayed(final By byElementCriteria) { @@ -402,7 +431,9 @@ public String toString() { } public void waitForElementsToDisappear(final By byElementCriteria) { - waitForCondition().until(elementNotDisplayed(byElementCriteria)); + if (!driverIsDisabled()) { + waitForCondition().until(elementNotDisplayed(byElementCriteria)); + } } private ExpectedCondition anyElementPresent(final By... expectedElements) { @@ -422,7 +453,9 @@ public String toString() { }; } public void waitForAnyRenderedElementOf(final By[] expectedElements) { - waitForCondition().until(anyElementPresent(expectedElements)); + if (!driverIsDisabled()) { + waitForCondition().until(anyElementPresent(expectedElements)); + } } // public void setWaitForTimeoutInMilliseconds(long waitForTimeoutInMilliseconds) { diff --git a/serenity-core/src/main/java/net/serenitybdd/core/pages/WebElementFacadeImpl.java b/serenity-core/src/main/java/net/serenitybdd/core/pages/WebElementFacadeImpl.java index 3048e54c0f..deb8c0204a 100644 --- a/serenity-core/src/main/java/net/serenitybdd/core/pages/WebElementFacadeImpl.java +++ b/serenity-core/src/main/java/net/serenitybdd/core/pages/WebElementFacadeImpl.java @@ -704,7 +704,9 @@ private void failWithMessage(String errorMessage) { private void checkPresenceOfWebElement() { try { - waitForCondition().until(elementIsDisplayed()); + if (!driverIsDisabled()) { + waitForCondition().until(elementIsDisplayed()); + } } catch (Throwable error) { if (webElement != null) { throwShouldBeVisibleErrorWithCauseIfPresent(error, error.getMessage()); @@ -716,12 +718,10 @@ private void checkPresenceOfWebElement() { @Override public WebElementFacade waitUntilVisible() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsDisplayed()); + if (!driverIsDisabled()) { + waitForCondition().until(elementIsDisplayed()); + } } catch (Throwable error) { if (webElement != null) { throwShouldBeVisibleErrorWithCauseIfPresent(error, error.getMessage()); @@ -734,12 +734,10 @@ public WebElementFacade waitUntilVisible() { @Override public WebElementFacade waitUntilPresent() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsPresent()); + if (!driverIsDisabled()) { + waitForCondition().until(elementIsPresent()); + } } catch (TimeoutException timeout) { throwShouldBePresentErrorWithCauseIfPresent(timeout, timeout.getMessage()); } @@ -867,12 +865,10 @@ private Wait waitBriefly() { @Override public WebElementFacade waitUntilNotVisible() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsNotDisplayed()); + if (!driverIsDisabled()) { + waitForCondition().until(elementIsNotDisplayed()); + } } catch (TimeoutException timeout) { throwShouldBeInvisibleErrorWithCauseIfPresent(timeout, "Expected hidden element was displayed"); } @@ -881,7 +877,6 @@ public WebElementFacade waitUntilNotVisible() { @Override public String getValue() { -// waitUntilVisible(); checkPresenceOfWebElement(); return getElement().getAttribute("value"); } @@ -901,44 +896,38 @@ public String getText() { @Override public WebElementFacade waitUntilEnabled() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsEnabled()); - return this; + if (!driverIsDisabled()) { + waitForCondition().until(elementIsEnabled()); + } } catch (TimeoutException timeout) { throw new ElementShouldBeEnabledException("Expected enabled element was not enabled", timeout); } + return this; } @Override public WebElementFacade waitUntilClickable() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsClickable()); - return this; + if (!driverIsDisabled()) { + waitForCondition().until(elementIsClickable()); + } } catch (TimeoutException timeout) { throw new ElementShouldBeEnabledException("Expected enabled element was not enabled", timeout); } + return this; } @Override public WebElementFacade waitUntilDisabled() { - if (driverIsDisabled()) { - return this; - } - try { - waitForCondition().until(elementIsNotEnabled()); - return this; + if (!driverIsDisabled()) { + waitForCondition().until(elementIsNotEnabled()); + } } catch (TimeoutException timeout) { throw new ElementShouldBeDisabledException("Expected disabled element was not disabled", timeout); } + return this; } @Override