Skip to content

Commit

Permalink
Added a selector to find buttons by their label, e.g. find(By.buttonT…
Browse files Browse the repository at this point in the history
…ext('Add to cart'));
  • Loading branch information
wakaleo committed Nov 6, 2014
1 parent 8ba6aeb commit 4a119f5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions core/src/main/java/net/thucydides/core/annotations/findby/By.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,46 @@ public static By jquery(final String jQuerySelector) {
return new ByjQuerySelector(jQuerySelector);
}

public static By buttonText(final String text) {
Preconditions.checkNotNull(text);
return new ByButtonTextSelector(text);
}

public static class ByButtonTextSelector extends By {

String buttonLabel;

public ByButtonTextSelector(String buttonLabel) {
this.buttonLabel = buttonLabel;
}

@SuppressWarnings("unchecked")
@Override
public List<WebElement> findElements(SearchContext context) {
return context.findElements(byXpathforButtonWithLabel(buttonLabel));
}

@Override
public WebElement findElement(SearchContext context) {

WebElement element = context.findElement(byXpathforButtonWithLabel(buttonLabel));
if (element != null){
return element;
}
throw new NoSuchElementException("Cannot locate element using " + toString());
}

private org.openqa.selenium.By byXpathforButtonWithLabel(String buttonLabel) {
return By.xpath(String.format("//*[normalize-space(.)=\"%s\"]", buttonLabel));
}

@Override
public String toString() {
return "By.buttonText: " + buttonLabel;
}
}


public static class ByjQuerySelector extends By {
private final String jQuerySelector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ class WhenUsingSmartFindBy extends Specification {
element.getAttribute("value") == "<enter first name>"
}


def "should be able to find an element using button text"(){

when: "we try to find an element using button text"
def element = driver.findElement(By.buttonText("Red Button"))

then: "we should find the element"
element.getAttribute("id") == "red-button"
}

def "should throw exception if button not found"(){

when: "we try to find an element using button text"
def element = driver.findElement(By.buttonText("Missing Button"))

then:
thrown(NoSuchElementException)
}

def "should be able to find elements using button text"(){

when: "we try to find an element using button text"
def elements = driver.findElements(By.buttonText("Blue's Button"))

then: "we should find the element"
elements.size() == 1
}


def "should return an empty list if no matching buttons found"(){

when: "we try to find elements using button text"
def elements = driver.findElements(By.buttonText("Mission Button"))

then: "we should not find the element"
elements.size() == 0
}

def "should be able to find a nested element using jquery"(){

when: "we try to find an element using a jquery selector"
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/resources/static-site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ <h3 id="placetitle">Location</h3>
<input id="buttonThatIsInitiallyEnabled" type="submit" value="Initially enabled"/>
<input id="alertButton" type="button" value="Alert" onclick="alert('Alert!');"/>

<button id="red-button">Red Button</button>
<button id="blue-button">Blue's Button</button>

<input name="hiddenfield" type="hidden"/>

<input name="csshiddenfield" style="display: none"/>
Expand Down

0 comments on commit 4a119f5

Please sign in to comment.