Skip to content

Commit

Permalink
Added more flexible dynamic tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Oct 2, 2019
1 parent 5e3f5dc commit dedfa47
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.serenitybdd.screenplay;

import net.serenitybdd.core.steps.HasCustomFieldValues;
import net.serenitybdd.markers.CanBeSilent;
import net.thucydides.core.annotations.Step;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class AnonymousPerformableFunction implements Performable, CanBeSilent {
private final String title;
private final Map<String, Object> fieldValues = new HashMap();
private final Consumer<Actor> actions;
private boolean isSilent = false;

public AnonymousPerformableFunction(String title, Consumer<Actor> actions) {
this.title = title;
this.actions = actions;
}

@Override
@Step("!#title")
public <T extends Actor> void performAs(T actor) {
actions.accept(actor);
}

@Override
public boolean isSilent() {
return isSilent;
}

public AnonymousPerformableFunction withNoReporting() {
this.isSilent = true;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.serenitybdd.screenplay;

import net.serenitybdd.markers.CanBeSilent;
import net.thucydides.core.annotations.Step;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class AnonymousPerformableRunnable implements Performable, CanBeSilent {
private final String title;
private final Map<String, Object> fieldValues = new HashMap();
private final Runnable actions;
private boolean isSilent = false;

public AnonymousPerformableRunnable(String title, Runnable actions) {
this.title = title;
this.actions = actions;
}

@Override
@Step("!#title")
public <T extends Actor> void performAs(T actor) {
actions.run();
}

@Override
public boolean isSilent() {
return isSilent;
}

public AnonymousPerformableRunnable withNoReporting() {
this.isSilent = true;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.serenitybdd.core.steps.Instrumented;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* A marker class to indicate that a Performable represents a higher level business task,
Expand All @@ -12,4 +14,12 @@ public interface Task extends Performable {
static <T extends Performable> AnonymousTask where(String title, T... steps) {
return Instrumented.instanceOf(AnonymousTask.class).withProperties(title, Arrays.asList(steps));
}

static <T extends Performable> AnonymousPerformableFunction where(String title, Consumer<Actor> performableOperation) {
return Instrumented.instanceOf(AnonymousPerformableFunction.class).withProperties(title, performableOperation);
}

static <T extends Performable> AnonymousPerformableFunction where(String title, Runnable performableOperation) {
return Instrumented.instanceOf(AnonymousPerformableFunction.class).withProperties(title, performableOperation);
}
}

0 comments on commit dedfa47

Please sign in to comment.