Skip to content

Commit

Permalink
Actors can now perform tasks conditionally
Browse files Browse the repository at this point in the history
Use the Unless class static methods and a bolean expression, e.g.

```
Unless.the(items.isEmpty(), AddTodoItems.called(items))
```

or use a question of type Question<Boolean>:

```
Unless.the(itemsListisEmpty(), AddTodoItems.called(items))
```
  • Loading branch information
wakaleo committed Feb 6, 2016
1 parent 5cb49b2 commit c9572c8
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 1 deletion.
Expand Up @@ -2,6 +2,7 @@

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Performable;
import net.serenitybdd.screenplay.Unless;
import net.serenitybdd.screenplay.actions.Settable;
import net.serenitybdd.screenplay.webtests.pages.ProfilePage;
import net.thucydides.core.annotations.Step;
Expand Down Expand Up @@ -60,7 +61,7 @@ public <T extends Actor> void performAs(T theUser) {
UpdateHerProfile.name().to(name),
UpdateHerProfile.country().to(countryOfResidence),
UpdateHerProfile.dob().to(dob),
UpdateHerProfile.color().to(color)
Unless.the(color.equals("none"),UpdateHerProfile.color().to(color))
);
}

Expand Down
@@ -0,0 +1,18 @@
package net.serenitybdd.screenplay;

public class ConditionalPerformable implements Performable {
private final Question<Boolean> shouldNotPerform;
private final Performable task;

public ConditionalPerformable(Question<Boolean> shouldNotPerform, Performable task) {
this.shouldNotPerform = shouldNotPerform;
this.task = task;
}

@Override
public <T extends Actor> void performAs(T actor) {
if (shouldNotPerform.answeredBy(actor)) { return; }

task.performAs(actor);
}
}
@@ -0,0 +1,6 @@
package net.serenitybdd.screenplay;

public class SilentPerformable implements Performable {
@Override
public <T extends Actor> void performAs(T actor) {}
}
@@ -0,0 +1,22 @@
package net.serenitybdd.screenplay;

public class Unless {
public static Performable the(Boolean shouldNotPerform, Performable task) {
return (shouldNotPerform) ? new SilentPerformable() : task;
}

public static Performable the(Question<Boolean> shouldNotPerform, Performable task) {
return new ConditionalPerformable(shouldNotPerform, task);
}

// Synonyms

public static Performable it(Question<Boolean> shouldNotPerform, Performable task) {
return new ConditionalPerformable(shouldNotPerform, task);
}

public static Performable we(Boolean shouldNotPerform, Performable task) {
return the(shouldNotPerform, task);
}

}
Expand Up @@ -97,6 +97,7 @@ public void shouldBeAbleToPurchaseSomeItems() {
// Expected to fail
@Test
public void shouldBeAbleToPurchaseAnItemForFree() {

givenThat(dana).attemptsTo(purchase().anApple().thatCosts(0).dollars(), // Will fail
purchase().aPear().thatCosts(5).dollars()); // Should be skipped
then(dana).should(seeThat(theTotalCost(), equalTo(15)));
Expand Down
@@ -0,0 +1,83 @@
package net.serenitybdd.journey.shopping;

import net.serenitybdd.journey.shopping.tasks.Purchase;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Question;
import net.serenitybdd.screenplay.Unless;
import org.junit.Test;
import org.junit.runner.RunWith;

import static net.serenitybdd.journey.shopping.tasks.Purchase.purchase;
import static net.serenitybdd.screenplay.GivenWhenThen.when;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SerenityRunner.class)
public class WhenTestsAreRunConditionally {

Actor dana = Actor.named("Dana");

@Test
public void shouldBeAbleToBuySomethingConditionally() {
int cost = 10;

Purchase purchase = purchase();

purchase.theItemWasPurchased = false;
when(dana).attemptsTo(Unless.the(cost < 100, purchase.aPear().thatCosts(5).dollars()));

assertThat(purchase.theItemWasPurchased).isFalse();
}

@Test
public void shouldBeAbleToNotBuySomethingConditionally() {
int cost = 10;
Purchase purchase = purchase();

purchase.theItemWasPurchased = false;
when(dana).attemptsTo(Unless.the(cost > 100, purchase.aPear().thatCosts(5).dollars()));

assertThat(purchase.theItemWasPurchased).isTrue();
}

private class IsExpensive implements Question<Boolean> {

private final int cost;

public IsExpensive(int cost) {
this.cost = cost;
}

@Override
public Boolean answeredBy(Actor actor) {
return (cost > 10);
}
}


@Test
public void shouldBeAbleToBuySomethingConditionallyUsingAQuestion() {
int cost = 5;
Purchase purchase = purchase();
IsExpensive isTooExpensive = new IsExpensive(cost);

purchase.theItemWasPurchased = false;
when(dana).attemptsTo(Unless.it(isTooExpensive, purchase.aPear().thatCosts(5).dollars()));

assertThat(purchase.theItemWasPurchased).isTrue();
}

@Test
public void shouldBeAbleToNotBuySomethingConditionallyUsingAQuestion() {
int cost = 20;
Purchase purchase = purchase();
IsExpensive isTooExpensive = new IsExpensive(cost);

purchase.theItemWasPurchased = false;
when(dana).attemptsTo(Unless.it(isTooExpensive, purchase.aPear().thatCosts(5).dollars()));

assertThat(purchase.theItemWasPurchased).isFalse();
}

}

Expand Up @@ -21,6 +21,8 @@ public class Purchase implements Performable {
String currency;


public Boolean theItemWasPurchased = false;

public static Purchase purchased() {return instrumented(Purchase.class);}
public static Purchase purchase() {return instrumented(Purchase.class);}
public static Purchase andPurchased() {return instrumented(Purchase.class);}
Expand All @@ -29,6 +31,7 @@ public class Purchase implements Performable {
public <T extends Actor> void performAs(T actor) {
assertThat(cost).isGreaterThan(0);
andThat(actor).has(placed_the_item_in_her_basket());
this.theItemWasPurchased = true;
}

public Purchase anApple() {
Expand Down

0 comments on commit c9572c8

Please sign in to comment.