Skip to content

Commit

Permalink
Added the Evaluate action and the JavaScript question to perform Java…
Browse files Browse the repository at this point in the history
…Script queries.
  • Loading branch information
wakaleo committed Dec 28, 2015
1 parent ef0e61a commit b658677
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.serenitybdd.screenplay.actions;

import net.serenitybdd.core.steps.Instrumented;
import net.serenitybdd.screenplay.Action;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.thucydides.core.annotations.Step;

public class Evaluate implements Action {

private final String expression;
private Object[] parameters;

public Evaluate(String expression) {
this.expression = expression;
}

public Evaluate withParameters(Object... parameters) {
this.parameters = parameters;
return this;
}

@Step("Execute JavaScript #expression")
public <T extends Actor> void performAs(T theUser) {
BrowseTheWeb.as(theUser).evaluateJavascript(expression, parameters);
}

public static Evaluate javascript(String expression, Object... parameters) {
Evaluate evaluate = Instrumented.instanceOf(Evaluate.class).withProperties(expression);
return evaluate.withParameters(parameters);
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Attribute extends UIState<String> {
public class Attribute extends TargetedUIState<String> {

private final String attributeName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class CSSValue extends UIState<String> {
public class CSSValue extends TargetedUIState<String> {

private final String attributeName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class CurrentVisibility extends UIState<Boolean> {
public class CurrentVisibility extends TargetedUIState<Boolean> {

public CurrentVisibility(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class CurrentlyEnabled extends UIState<Boolean> {
public class CurrentlyEnabled extends TargetedUIState<Boolean> {

public CurrentlyEnabled(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Enabled extends UIState<Boolean> {
public class Enabled extends TargetedUIState<Boolean> {

public Enabled(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.serenitybdd.screenplay.questions;

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;

public class JavaScript extends UIState<String> {

private final String expression;
private final Object[] parameters;

public JavaScript(String expression, Object[] parameters, Actor actor) {
super(actor);
this.expression = expression;
this.parameters = parameters;
}

public static JavaScriptBuilder evaluate(String expression) {
return new JavaScriptBuilder(expression);
}

@Override
public String resolve() {
return BrowseTheWeb.as(actor).evaluateJavascript(expression, parameters).toString();
}

public static final class JavaScriptBuilder {
private final String expression;
private Object[] parameters = new Object[]{};

public JavaScriptBuilder(String expression, Object... parameters) {
this.expression = expression;
this.parameters = parameters;
}

public JavaScriptBuilder withParameters(Object... parameters) {
this.parameters = parameters;
return this;
}

public JavaScript onTheScreenOf(Actor actor) {
return new JavaScript(expression, parameters, actor);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Presence extends UIState<Boolean> {
public class Presence extends TargetedUIState<Boolean> {

public Presence(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class SelectedOptions extends UIState<List<String>> {
public class SelectedOptions extends TargetedUIState<List<String>> {

public SelectedOptions(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class SelectedStatus extends UIState<Boolean> {
public class SelectedStatus extends TargetedUIState<Boolean> {

public SelectedStatus(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class SelectedValue extends UIState<String> {
public class SelectedValue extends TargetedUIState<String> {

public SelectedValue(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class SelectedVisibleTextValue extends UIState<String> {
public class SelectedVisibleTextValue extends TargetedUIState<String> {

public SelectedVisibleTextValue(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.serenitybdd.screenplay.questions;

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.targets.Target;

import java.util.List;

public abstract class TargetedUIState<T> extends UIState<T>{

protected final Target target;

protected TargetedUIState(Target target, Actor actor) {
super(actor);
this.target = target;
}

public abstract List<T> resolveAll();

public List<T> asList() {
return resolveAll();
}

public <T> List<T> asListOf(Class<T> type) {
return convertToEnums(type, asList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Text extends UIState<String> {
public class Text extends TargetedUIState<String> {

public Text(Target target, Actor actor) {
super(target, actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class TheCoordinates extends UIState<org.openqa.selenium.interactions.internal.Coordinates> {
public class TheCoordinates extends TargetedUIState<org.openqa.selenium.interactions.internal.Coordinates> {

public TheCoordinates(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class TheLocation extends UIState<Point> {
public class TheLocation extends TargetedUIState<Point> {

public TheLocation(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class TheSize extends UIState<Dimension> {
public class TheSize extends TargetedUIState<Dimension> {

public TheSize(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.common.collect.Maps;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.questions.converters.*;
import net.serenitybdd.screenplay.targets.Target;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

Expand All @@ -13,7 +12,6 @@

public abstract class UIState<T> {

protected final Target target;
protected final Actor actor;

protected Map<Class<?>, Converter> DEFAULT_CONVERTERS = Maps.newHashMap();
Expand All @@ -27,13 +25,11 @@ public abstract class UIState<T> {
DEFAULT_CONVERTERS.put(Long.class, new LongConverter());
}

protected UIState(Target target, Actor actor) {
this.target = target;
protected UIState(Actor actor) {
this.actor = actor;
}

public abstract T resolve();
public abstract List<T> resolveAll();

public T value() { return resolve(); }

Expand Down Expand Up @@ -70,14 +66,6 @@ public <T> T asEnum(Class<T> enumType) {
return EnumValues.forType(enumType).getValueOf(value);
}

public List<T> asList() {
return resolveAll();
}

public <T> List<T> asListOf(Class<T> type) {
return convertToEnums(type, asList());
}

protected <T> List<T> convertToEnums(Class<T> enumType, List<?> values) {
return EnumValues.forType(enumType).getValuesOf(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UIStateReaderBuilder<T> {
public class UIStateReaderBuilder<T>{
private final Target target;
private final Class<T> type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Value extends UIState<String> {
public class Value extends TargetedUIState<String> {

public Value(Target target, Actor actor) {
super(target,actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;

public class Visibility extends UIState<Boolean> {
public class Visibility extends TargetedUIState<Boolean> {

public Visibility(Target target, Actor actor) {
super(target, actor);
Expand Down

0 comments on commit b658677

Please sign in to comment.