Skip to content

Commit

Permalink
Closes #134 for "select", $.getText() returns text(s) of selected opt…
Browse files Browse the repository at this point in the history
…ion(s).
  • Loading branch information
asolntsev committed Jun 23, 2015
1 parent f5f90aa commit 90e717c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/codeborne/selenide/SelenideElement.java
Expand Up @@ -80,6 +80,14 @@ public interface SelenideElement extends WebElement, FindsByLinkText, FindsById,
*/
SelenideElement pressTab();

/**
* Get the visible text of this element, including sub-elements without leading/trailing whitespace.
* NB! For "select", returns text(s) of selected option(s).
*
* @return The innerText of this element
*/
@Override String getText();

/**
* Short form of getText()
* @see WebElement#getText()
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/codeborne/selenide/conditions/Text.java
Expand Up @@ -2,7 +2,11 @@

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.impl.Html;
import com.google.common.base.Joiner;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

public class Text extends Condition {
protected final String text;
Expand All @@ -13,7 +17,19 @@ public Text(final String text) {

@Override
public boolean apply(WebElement element) {
return Html.text.contains(element.getText(), text.toLowerCase());
String elementText = "select".equalsIgnoreCase(element.getTagName()) ?
getSelectedOptionsTexts(element) :
element.getText();
return Html.text.contains(elementText, this.text.toLowerCase());
}

private String getSelectedOptionsTexts(WebElement element) {
List<WebElement> selectedOptions = new Select(element).getAllSelectedOptions();
StringBuilder sb = new StringBuilder();
for (WebElement selectedOption : selectedOptions) {
sb.append(selectedOption.getText());
}
return sb.toString();
}

@Override
Expand Down
Expand Up @@ -158,8 +158,8 @@ else if ("followLink".equals(method.getName())) {
followLink();
return null;
}
else if ("text".equals(method.getName())) {
return getDelegate().getText();
else if ("text".equals(method.getName()) || "getText".equals(method.getName())) {
return getText();
}
else if ("innerText".equals(method.getName())) {
return getInnerText();
Expand Down Expand Up @@ -340,6 +340,11 @@ protected void setSelected(boolean selected) {
}
}

protected String getText() {
WebElement element = getDelegate();
return "select".equalsIgnoreCase(element.getTagName()) ? getSelectedText(element) : element.getText();
}

protected String getInnerText() {
WebElement element = getDelegate();
if (isHtmlUnit()) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/integration/SelectsTest.java
Expand Up @@ -45,6 +45,22 @@ public void userCanSelectOptionByText() {
assertEquals("@мыло.ру", select.getSelectedText());
}

@Test
public void getTextReturnsTextsOfSelectedOptions() {
assertEquals("-- Select your hero --", $("#hero").getText());

$("#hero").selectOptionByValue("john mc'lain");
assertEquals("John Mc'Lain", $("#hero").getText());
}

@Test
public void shouldHaveTextChecksSelectedOption() {
$("#hero").shouldNotHave(text("John Mc'Lain").because("Option is not selected yet"));

$("#hero").selectOptionByValue("john mc'lain");
$("#hero").shouldHave(text("John Mc'Lain").because("Option `john mc'lain` is selected"));
}

@Test
public void optionValueWithApostrophe() {
$("#hero").selectOptionByValue("john mc'lain");
Expand Down

0 comments on commit 90e717c

Please sign in to comment.