-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow more elegant waits in the Screenplay module
You can now write code like this: jane.should(eventually(seeThat(TheClickerValue.of(clicker), equalTo(10)))) This will not fail if the matcher cannot be evaluated the first time, but will retry up to a maximum of 'serenity.timouts' seconds (5 by default).
- Loading branch information
Showing
8 changed files
with
282 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
serenity-screenplay/src/main/java/net/serenitybdd/screenplay/EventualConsequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package net.serenitybdd.screenplay; | ||
|
||
import net.serenitybdd.core.time.Stopwatch; | ||
import net.thucydides.core.guice.Injectors; | ||
import net.thucydides.core.webdriver.Configuration; | ||
|
||
public class EventualConsequence<T> implements Consequence<T> { | ||
public static final int A_SHORT_PERIOD_BETWEEN_TRIES = 100; | ||
private final Consequence<T> consequenceThatMightTakeSomeTime; | ||
private final long timeout; | ||
|
||
private AssertionError caughtAssertionError = null; | ||
private RuntimeException caughtRuntimeException = null; | ||
|
||
public EventualConsequence(Consequence<T> consequenceThatMightTakeSomeTime, long timeout) { | ||
this.consequenceThatMightTakeSomeTime = consequenceThatMightTakeSomeTime; | ||
this.timeout = timeout; | ||
} | ||
|
||
public EventualConsequence(Consequence<T> consequenceThatMightTakeSomeTime) { | ||
this(consequenceThatMightTakeSomeTime, | ||
Injectors.getInjector().getInstance(Configuration.class).getElementTimeout() * 1000); | ||
} | ||
|
||
public static <T> EventualConsequence<T> eventually(Consequence<T> consequenceThatMightTakeSomeTime) { | ||
return new EventualConsequence(consequenceThatMightTakeSomeTime); | ||
} | ||
|
||
public EventualConsequenceBuilder<T> waitingForNoLongerThan(long amount) { | ||
return new EventualConsequenceBuilder(consequenceThatMightTakeSomeTime, amount); | ||
} | ||
|
||
@Override | ||
public void evaluateFor(Actor actor) { | ||
Stopwatch stopwatch = new Stopwatch(); | ||
|
||
stopwatch.start(); | ||
do { | ||
try { | ||
consequenceThatMightTakeSomeTime.evaluateFor(actor); | ||
return; | ||
} catch (AssertionError assertionError) { | ||
caughtAssertionError = assertionError; | ||
} catch (RuntimeException runtimeException) { | ||
caughtRuntimeException = runtimeException; | ||
} | ||
pauseBeforeNextAttempt(); | ||
} while (stopwatch.lapTime() < timeout); | ||
|
||
throwAnyCaughtErrors(); | ||
} | ||
|
||
private void pauseBeforeNextAttempt() { | ||
try { | ||
Thread.sleep(A_SHORT_PERIOD_BETWEEN_TRIES); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void throwAnyCaughtErrors() { | ||
if (caughtAssertionError != null) { | ||
throw caughtAssertionError; | ||
} | ||
if (caughtRuntimeException != null) { | ||
throw caughtRuntimeException; | ||
} | ||
} | ||
|
||
@Override | ||
public Consequence<T> orComplainWith(Class<? extends Error> complaintType) { | ||
return new EventualConsequence(consequenceThatMightTakeSomeTime.orComplainWith(complaintType)); | ||
} | ||
|
||
@Override | ||
public Consequence<T> orComplainWith(Class<? extends Error> complaintType, String complaintDetails) { | ||
return new EventualConsequence(consequenceThatMightTakeSomeTime.orComplainWith(complaintType, complaintDetails)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
serenity-screenplay/src/main/java/net/serenitybdd/screenplay/EventualConsequenceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.serenitybdd.screenplay; | ||
|
||
public class EventualConsequenceBuilder<T> { | ||
private final Consequence<T> consequence; | ||
private final long amount; | ||
|
||
public <T> EventualConsequenceBuilder(Consequence consequence, long amount) { | ||
this.consequence = consequence; | ||
this.amount = amount; | ||
} | ||
|
||
public EventualConsequence<T> milliseconds() { | ||
return new EventualConsequence<T>(consequence, amount); | ||
} | ||
|
||
public EventualConsequence<T> seconds() { | ||
return new EventualConsequence<T>(consequence, amount * 1000); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...creenplay/src/test/groovy/net/serenitybdd/screenplay/SomethingBadHappenedException.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.serenitybdd.screenplay | ||
|
||
public class SomethingBadHappenedException extends Error { | ||
|
||
public SomethingBadHappenedException(String msg) { | ||
super(msg) | ||
} | ||
|
||
public SomethingBadHappenedException(String msg, Throwable e) { | ||
super(msg, e) | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...screenplay/src/test/groovy/net/serenitybdd/screenplay/WhenWaitingForDelayedResults.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package net.serenitybdd.screenplay | ||
import net.serenitybdd.core.Serenity | ||
import net.thucydides.core.model.TestResult | ||
import static net.thucydides.core.model.TestResult.* | ||
import net.thucydides.core.steps.StepEventBus | ||
import spock.lang.Specification | ||
|
||
import static net.serenitybdd.screenplay.EventualConsequence.eventually | ||
import static net.serenitybdd.screenplay.GivenWhenThen.seeThat | ||
import static org.hamcrest.Matchers.equalTo | ||
|
||
class WhenWaitingForDelayedResults extends Specification { | ||
|
||
def setup() { | ||
Serenity.initialize(this) | ||
StepEventBus.eventBus.testStarted("some test") | ||
} | ||
|
||
def "should get a result immediately by default"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(seeThat(TheClickerValue.of(clicker), equalTo(1))) | ||
then: | ||
theTestResult() == SUCCESS | ||
} | ||
|
||
def "should be able to wait for a result to become available if it is slow to arrive"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(eventually(seeThat(TheClickerValue.of(clicker), equalTo(10)))) | ||
then: | ||
theTestResult() == SUCCESS | ||
} | ||
|
||
def "should not wait forever if the result never arrives"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(eventually(seeThat(TheClickerValue.of(clicker), equalTo(-1))).waitingForNoLongerThan(100).milliseconds()) | ||
then: | ||
theTestResult() == FAILURE | ||
} | ||
|
||
def "should transmit an error if one happens"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(eventually(seeThat(TheClickerValue.ofBroken(clicker), equalTo(1))).waitingForNoLongerThan(250).milliseconds()) | ||
then: | ||
theTestResult() == ERROR | ||
} | ||
|
||
def "should report custom error if one happens"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(eventually(seeThat(TheClickerValue.of(clicker), equalTo(-1)). | ||
orComplainWith(SomethingBadHappenedException)). | ||
waitingForNoLongerThan(100).milliseconds()) | ||
then: | ||
theTestResult() == ERROR | ||
} | ||
|
||
def "should report custom error if one is declared outside of the eventually scope"() { | ||
given: | ||
Actor jane = Actor.named("Jane") | ||
Clicker clicker = new Clicker(); | ||
when: | ||
jane.should(eventually(seeThat(TheClickerValue.of(clicker), equalTo(-1))). | ||
waitingForNoLongerThan(100).milliseconds().orComplainWith(SomethingBadHappenedException)) | ||
then: | ||
theTestResult() == ERROR | ||
} | ||
|
||
private TestResult theTestResult() { | ||
StepEventBus.eventBus.baseStepListener.testOutcomes[0].result | ||
} | ||
} | ||
|
||
class Clicker { | ||
int count = 0 | ||
|
||
def click() { | ||
count++ | ||
} | ||
} | ||
|
||
|
||
class TheClickerValue implements Question<Integer> { | ||
private final Clicker clicker; | ||
|
||
TheClickerValue(Clicker clicker) { | ||
this.clicker = clicker | ||
} | ||
|
||
public static TheClickerValue of(Clicker clicker) { | ||
new TheClickerValue(clicker) | ||
} | ||
|
||
public static TheClickerValue ofBroken(Clicker clicker) { | ||
new BrokenClickerValue(clicker) | ||
} | ||
|
||
@Override | ||
Integer answeredBy(Actor actor) { | ||
clicker.click(); | ||
return clicker.count; | ||
} | ||
} | ||
|
||
class BrokenClickerValue extends TheClickerValue { | ||
private final Clicker clicker; | ||
|
||
BrokenClickerValue(Clicker clicker) { | ||
super(clicker); | ||
} | ||
|
||
@Override | ||
Integer answeredBy(Actor actor) { | ||
throw new SomethingBadHappenedException("Oh crap"); | ||
} | ||
} |