Skip to content

Commit

Permalink
Improved reporting for Enter and Type tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Jun 2, 2019
1 parent eaf3009 commit d1a9bed
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.By;

import java.util.Arrays;

import static net.serenitybdd.screenplay.Tasks.instrumented;

public class Enter {

private final String theText;
private final CharSequence[] theText;

public Enter(String theText) {
public Enter(CharSequence... theText) {
this.theText = theText;
}

public static Enter theValue(String text) {
public static Enter theValue(CharSequence... text) {
return new Enter(text);
}

public static SendKeys keyValues(String text) {
public static SendKeys keyValues(CharSequence... text) {
return new SendKeys(text);
}

public EnterValue into(String cssOrXpathForElement) {
return instrumented(EnterValueIntoTarget.class, theText, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement));
return instrumented(EnterValueIntoTarget.class, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement), theText);
}

public EnterValue into(Target target) {
return instrumented(EnterValueIntoTarget.class, theText, target);
return instrumented(EnterValueIntoTarget.class, target, theText);
}

public EnterValue into(WebElementFacade element) {
return instrumented(EnterValueIntoElement.class, theText, element);
return instrumented(EnterValueIntoElement.class, element, theText);
}

public EnterValue into(By... locators) {
return instrumented(EnterValueIntoBy.class, theText, locators);
return instrumented(EnterValueIntoBy.class, Arrays.asList(locators), theText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
import org.openqa.selenium.Keys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static net.serenitybdd.screenplay.actions.type.RenderEnteredText.getFollowedByKeysDescriptionFor;
import static net.serenitybdd.screenplay.actions.type.RenderEnteredText.getTextAsStringFor;

public abstract class EnterValue implements Interaction {

protected final CharSequence[] theText;
protected final List<Keys> followedByKeys;
protected String theTextAsAString;

public EnterValue(CharSequence... theText) {
this.theText = theText;
this.theTextAsAString = getTextAsStringFor(theText);
this.followedByKeys = new ArrayList<>();
}

public EnterValue thenHit(Keys... keys) {
this.followedByKeys.addAll(NewList.of(keys));
theTextAsAString = getTextAsStringFor(theText) + getFollowedByKeysDescriptionFor(followedByKeys);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.Arrays;
import java.util.List;

public class EnterValueIntoBy extends EnterValue {

private final List<By> locators;
private final String locatorNames;

protected WebElement resolveFor(Actor theUser) {
return WebElementLocator.forLocators(locators).andActor(theUser);
}

public EnterValueIntoBy(String theText, By... locators) {
public EnterValueIntoBy(List<By> locators, CharSequence... theText) {
super(theText);
this.locators = NewList.copyOf(locators);
this.locatorNames = (locators.size() == 1) ? locators.get(0).toString() : locators.toString();
}

@Step("{0} enters '#theText' into #locators")
@Step("{0} enters #theTextAsAString into #locatorNames")
public <T extends Actor> void performAs(T theUser) {
resolveFor(theUser).sendKeys(theText);
if (getFollowedByKeys() != null && getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class EnterValueIntoElement extends EnterValue {

private WebElementFacade element;

public EnterValueIntoElement(String theText, WebElementFacade element) {
public EnterValueIntoElement(WebElementFacade element, CharSequence... theText) {
super(theText);
this.element = element;
}

@Step("{0} enters '#theText' into #element")
@Step("{0} enters #theTextAsAString into #element")
public <T extends Actor> void performAs(T theUser) {
element.type(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class EnterValueIntoTarget extends EnterValue {

private Target target;

public EnterValueIntoTarget(String theText, Target target) {
public EnterValueIntoTarget(Target target, CharSequence... theText) {
super(theText);
this.target = target;
}

@Step("{0} enters '#theText' into #target")
@Step("{0} enters #theTextAsAString into #target")
public <T extends Actor> void performAs(T theUser) {
target.resolveFor(theUser).type(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static SendKeys of(CharSequence... text) {
}

public EnterValue into(String cssOrXpathForElement) {
return instrumented(SendKeysIntoTarget.class, theText, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement));
return instrumented(SendKeysIntoTarget.class, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement), theText);
}

public EnterValue into(Target target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.serenitybdd.screenplay.actions;

import net.serenitybdd.core.collect.NewList;
import net.serenitybdd.screenplay.Actor;
import net.thucydides.core.annotations.Step;
import org.openqa.selenium.By;
Expand All @@ -11,6 +10,7 @@
public class SendKeysIntoBy extends EnterValue {

private final List<By> locators;
private final String locatorNames;

protected WebElement resolveFor(Actor theUser) {
return WebElementLocator.forLocators(locators).andActor(theUser);
Expand All @@ -19,9 +19,10 @@ protected WebElement resolveFor(Actor theUser) {
public SendKeysIntoBy(List<By> locators, CharSequence... theText) {
super(theText);
this.locators = locators;
this.locatorNames = (locators.size() == 1) ? locators.get(0).toString() : locators.toString();
}

@Step("{0} enters '#theText' into #locators")
@Step("{0} enters #theTextAsAString into #locatorNames")
public <T extends Actor> void performAs(T theUser) {
resolveFor(theUser).sendKeys(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public SendKeysIntoTarget(Target target, CharSequence... theText) {
this.target = target;
}

@Step("{0} enters '#theText' into #target")
@Step("{0} enters #theTextAsAString into #target")
public <T extends Actor> void performAs(T theUser) {
target.resolveFor(theUser).sendKeys(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public SendKeystoElement(WebElementFacade element, CharSequence... theText) {
this.element = element;
}

@Step("{0} enters '#theText' into #element")
@Step("{0} enters #theTextAsAString into #element")
public <T extends Actor> void performAs(T theUser) {
element.sendKeys(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.serenitybdd.screenplay.actions.type;

import net.serenitybdd.core.collect.NewList;
import net.serenitybdd.core.strings.Joiner;
import net.serenitybdd.screenplay.actions.KeyNames;
import org.openqa.selenium.Keys;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;

public class RenderEnteredText {

private static final String ENTER_KEYS_INTRO_TEXT = " then hits ";

public static String getFollowedByKeysDescriptionFor(List<Keys> keys) {
if (keys.isEmpty()) {
return "";
}
if (keys.size() == 1) {
return ENTER_KEYS_INTRO_TEXT + KeyNames.of(keys);
}
if (keys.size() == 2) {
return ENTER_KEYS_INTRO_TEXT + Joiner.on(" and ").join(KeyNames.of(keys));
}

String allButLastTwo = Joiner.on(", ").join(KeyNames.allButLastTwo(keys));
String lastTwoKeys = Joiner.on(" and ").join(KeyNames.lastTwoOf(keys));
String allKeys = Joiner.on(", ").join(NewList.of(allButLastTwo, lastTwoKeys));

return ENTER_KEYS_INTRO_TEXT + allKeys;
}


public static String getTextAsStringFor(CharSequence... theText) {

return stream(theText).map(
textValue -> (textValue instanceof Keys) ? ((Keys) textValue).name() : "'" + textValue.toString() + "'"
).collect(Collectors.joining(","));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.By;

import java.util.Arrays;

import static net.serenitybdd.screenplay.Tasks.instrumented;

public class Type {

private final String theText;
private final CharSequence[] theText;

public Type(String theText) {
public Type(CharSequence... theText) {
this.theText = theText;
}

public static Type theValue(String text) {
public static Type theValue(CharSequence... text) {
return new Type(text);
}

public TypeValue into(String cssOrXpathForElement) {
return instrumented(TypeValueIntoTarget.class, theText, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement));
return instrumented(TypeValueIntoTarget.class, Target.the(cssOrXpathForElement).locatedBy(cssOrXpathForElement), theText);
}

public TypeValue into(Target target) {
return instrumented(TypeValueIntoTarget.class, theText, target);
return instrumented(TypeValueIntoTarget.class, target, theText);
}

public TypeValue into(WebElementFacade element) {
return instrumented(TypeValueIntoElement.class, theText, element);
return instrumented(TypeValueIntoElement.class, element, theText);
}

public TypeValue into(By... locators) {
return instrumented(TypeValueIntoBy.class, theText, locators);
return instrumented(TypeValueIntoBy.class, Arrays.asList(locators), theText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,34 @@
import net.serenitybdd.screenplay.actions.KeyNames;
import org.openqa.selenium.Keys;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static net.serenitybdd.screenplay.actions.type.RenderEnteredText.getFollowedByKeysDescriptionFor;
import static net.serenitybdd.screenplay.actions.type.RenderEnteredText.getTextAsStringFor;

public abstract class TypeValue implements Interaction {

protected final String theText;
protected final CharSequence[] theText;
protected final List<Keys> followedByKeys;
private static final String ENTER_KEYS_INTRO_TEXT = " then hits ";
protected String theTextAsAString;

public TypeValue(String theText) {
public TypeValue(CharSequence... theText) {
this.theText = theText;
this.followedByKeys = new ArrayList<>();
this.theTextAsAString = getTextAsStringFor(theText);
}

public TypeValue thenHit(Keys... keys) {
this.followedByKeys.addAll(NewList.of(keys));
theTextAsAString = getTextAsStringFor(theText) + getFollowedByKeysDescriptionFor(followedByKeys);
return this;
}

public Keys[] getFollowedByKeys() {
return followedByKeys.toArray(new Keys[]{});
}

private String getFollowedByKeysDescriptionFor(List<Keys> keys) {
if (keys.isEmpty()) {
return "";
}
if (keys.size() == 1) {
return ENTER_KEYS_INTRO_TEXT + KeyNames.of(keys);
}
if (keys.size() == 2) {
return ENTER_KEYS_INTRO_TEXT + Joiner.on(" and ").join(KeyNames.of(keys));
}

String allButLastTwo = Joiner.on(", ").join(KeyNames.allButLastTwo(keys));
String lastTwoKeys = Joiner.on(" and ").join(KeyNames.lastTwoOf(keys));
String allKeys = Joiner.on(", ").join(NewList.of(allButLastTwo, lastTwoKeys));

return ENTER_KEYS_INTRO_TEXT + allKeys;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ protected WebElement resolveFor(Actor theUser) {
return WebElementLocator.forLocators(locators).andActor(theUser);
}

public TypeValueIntoBy(String theText, By... locators) {
public TypeValueIntoBy(List<By> locators, CharSequence... theText) {
super(theText);
this.locators = NewList.copyOf(locators);
}

@Step("{0} enters '#theText' into #locators")
@Step("{0} enters #theTextAsAString into #locators")
public <T extends Actor> void performAs(T theUser) {
resolveFor(theUser).sendKeys(theText);
WebElement element = resolveFor(theUser);

element.sendKeys(theText);
if (getFollowedByKeys().length > 0) {
resolveFor(theUser).sendKeys(getFollowedByKeys());
element.sendKeys(getFollowedByKeys());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class TypeValueIntoElement extends TypeValue {

private WebElementFacade element;

public TypeValueIntoElement(String theText, WebElementFacade element) {
public TypeValueIntoElement(WebElementFacade element, CharSequence... theText) {
super(theText);
this.element = element;
}

@Step("{0} enters '#theText' into #element")
@Step("{0} enters #theTextAsAString into #element")
public <T extends Actor> void performAs(T theUser) {
element.sendKeys(theText);
if (getFollowedByKeys().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class TypeValueIntoTarget extends TypeValue {

private Target target;

public TypeValueIntoTarget(String theText, Target target) {
public TypeValueIntoTarget(Target target, CharSequence... theText) {
super(theText);
this.target = target;
}

@Step("{0} enters '#theText' into #target")
@Step("{0} enters #theTextAsAString into #target")
public <T extends Actor> void performAs(T theUser) {
target.resolveFor(theUser).sendKeys(theText);
if(getFollowedByKeys().length!=0) {
Expand Down

0 comments on commit d1a9bed

Please sign in to comment.