Skip to content

Commit

Permalink
Added support for By locators in Target objects and Action classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Jan 14, 2016
1 parent e2ea2ea commit 511f607
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.serenitybdd.screenplay.actions;

import com.google.common.collect.Lists;
import net.serenitybdd.core.pages.WebElementFacade;
import net.serenitybdd.screenplay.Action;
import net.serenitybdd.screenplay.targets.Target;
Expand All @@ -22,7 +23,7 @@ public static Action on(WebElementFacade element) {
}

public static Action on(By... locators) {
return instrumented(ClickOnBy.class, locators);
return instrumented(ClickOnBy.class, Lists.newArrayList(locators));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.thucydides.core.annotations.Step;
import org.openqa.selenium.By;

import java.util.List;

public class ClickOnBy extends ByAction {

@Step("{0} clicks on #target")
Expand All @@ -15,4 +17,8 @@ public ClickOnBy(By... locators) {
super(locators);
}

public ClickOnBy(List<By> locators) {
super(locators.toArray(new By[]{}));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package net.serenitybdd.screenplay.questions;

import net.serenitybdd.core.pages.WebElementState;

/**
* Created by john on 14/01/2016.
*/
public class UnresolvedTargetWebElementState implements WebElementState {

private final String name;

public UnresolvedTargetWebElementState(String name) {
this.name = name;
}

@Override
public String toString() {
return "not so";
}

@Override
public boolean isVisible() {
return false;
}

@Override
public boolean isCurrentlyVisible() {
return false;
}

@Override
public boolean isCurrentlyEnabled() {
return false;
}

@Override
public void shouldBeVisible() {
throw new AssertionError("Element should be visible");
}

@Override
public void shouldBeCurrentlyVisible() {
throw new AssertionError("Element should be visible");
}

@Override
public void shouldNotBeVisible() {}

@Override
public void shouldNotBeCurrentlyVisible() {}

@Override
public boolean hasFocus() {
return false;
}

@Override
public boolean containsText(String value) {
return false;
}

@Override
public boolean containsOnlyText(String value) {
return false;
}

@Override
public boolean containsSelectOption(String value) {
return false;
}

@Override
public void shouldContainText(String textValue) {
throw new AssertionError("Element should contain text " + textValue);
}

@Override
public void shouldContainOnlyText(String textValue) {
throw new AssertionError("Element should contain text " + textValue);
}

@Override
public void shouldContainSelectedOption(String textValue) {
throw new AssertionError("Element should contain selected option " + textValue);
}

@Override
public void shouldNotContainText(String textValue) {}

@Override
public void shouldBeEnabled() {
throw new AssertionError("Element should be enabled");
}

@Override
public boolean isEnabled() {
return false;
}

@Override
public void shouldNotBeEnabled() {

}

@Override
public String getSelectedVisibleTextValue() {
return null;
}

@Override
public String getSelectedValue() {
return null;
}

@Override
public boolean isPresent() {
return false;
}

@Override
public void shouldBePresent() {
throw new AssertionError("Element should be present");
}

@Override
public void shouldNotBePresent() {

}

@Override
public boolean isSelected() {
return false;
}

@Override
public String getTextValue() {
return null;
}

@Override
public WebElementState expect(String errorMessage) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import net.serenitybdd.core.pages.WebElementState;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Question;
import net.serenitybdd.screenplay.annotations.Subject;
import net.serenitybdd.screenplay.targets.Target;
import org.openqa.selenium.NoSuchElementException;

@Subject("#target")
public class WebElementQuestion implements Question<WebElementState> {

private final Target target;
Expand All @@ -27,6 +30,10 @@ public static Question<WebElementState> the(Target target) {

@Override
public WebElementState answeredBy(Actor actor) {
return target.resolveFor(actor);
try {
return target.resolveFor(actor);
} catch (NoSuchElementException unresolvedTarget) {
return new UnresolvedTargetWebElementState(target.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public List<WebElementFacade> resolveAllFor(Actor actor) {
return resolver.findAll(locator);
}


public XPathOrCssTarget of(String... parameters) {
throw new UnsupportedOperationException("The of() method is not supported for By-type Targets");
}

@Override
public String getCssOrXPathSelector() {
throw new UnsupportedOperationException("The getCssOrXPathSelector() method is not supported for By-type Targets");
}

public ByTarget called(String name) {
return new ByTarget(name, locator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public static TargetBuilder the(String targetElementName) {
public abstract List<WebElementFacade> resolveAllFor(Actor actor);

public abstract Target called(String name);

public abstract Target of(String... parameters);

public abstract String getCssOrXPathSelector();

public String getName() {
return targetElementName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public List<WebElementFacade> resolveAllFor(Actor actor) {
return resolver.findAll(cssOrXPathSelector);
}

public XPathOrCssTarget of(String... parameters) {
public Target of(String... parameters) {
return new XPathOrCssTarget(targetElementName, instantiated(cssOrXPathSelector, parameters));
}

public XPathOrCssTarget called(String name) {
public Target called(String name) {
return new XPathOrCssTarget(name, cssOrXPathSelector);
}

public String getCssOrXPathSelector() {
return cssOrXPathSelector;
}

private String instantiated(String cssOrXPathSelector, String[] parameters) {
return new TargetSelectorWithVariables(cssOrXPathSelector).resolvedWith(parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ public void dropAllListeners() {
}

public boolean webdriverCallsAreSuspended() {
if (softAssertsActive()) {
return !webdriverSuspensions.isEmpty();
}
return aStepInTheCurrentTestHasFailed() || !webdriverSuspensions.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StepFailure(final ExecutedStepDescription description, final Throwable ca
if (cause instanceof SerenityManagedException) {
this.exceptionClass = ((SerenityManagedException)cause).getExceptionClass();
this.message = cause.getMessage();
this.stackTraceElements = ((SerenityManagedException)cause).getStackTrace();
this.stackTraceElements = cause.getStackTrace();
} else {
this.exceptionClass = cause.getClass();
this.message = cause.getMessage();
Expand Down

0 comments on commit 511f607

Please sign in to comment.