Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cssValue condition. #727

Merged
merged 3 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/com/codeborne/selenide/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,45 @@ public String toString() {
};
}

/**
* Checks if css property (style) applies for the element.
* Both explicit and computed properties are supported.
* <p>
* Note that if css property is missing {@link WebElement#getCssValue} return empty string.
* In this case you should assert against empty string.
* <p>
* Sample:
* <p>
* {@code <input style="font-size: 12">}
* <p>
* {@code $("input").shouldHave(cssValue("font-size", "12"));}
* <p>
* {@code $("input").shouldHave(cssValue("display", "block"));}
*
* @param propertyName the css property (style) name of the element
* @param expectedValue expected value of css property
*
* @see WebElement#getCssValue
*/
public static Condition cssValue(final String propertyName, final String expectedValue) {
return new Condition("cssValue") {
@Override
public boolean apply(WebElement element) {
return expectedValue.equalsIgnoreCase(element.getCssValue(propertyName));
}

@Override
public String actualValue(WebElement element) {
return element.getCssValue(propertyName);
}

@Override
public String toString() {
return name + " " + propertyName + '=' + expectedValue;
}
};
}

/**
* Check if browser focus is currently in given element.
*/
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/codeborne/selenide/ConditionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void textConditionIsCaseInsensitive() {
WebElement element = elementWithText("John Malkovich The First");
assertTrue(Condition.text("john malkovich").apply(element));
}

@Test
public void textConditionIgnoresWhitespaces() {
assertTrue(Condition.text("john the malkovich").apply(
Expand Down Expand Up @@ -205,6 +205,12 @@ public void elementHasClass() {
assertFalse(Condition.hasClass("active").apply(elementWithAttribute("class", "btn btn-warning")));
}

@Test
public void elementHasCssValue() {
assertTrue(Condition.cssValue("display", "none").apply(elementWithCssStyle("display", "none")));
assertFalse(Condition.cssValue("font-size", "24").apply(elementWithCssStyle("font-size", "20")));
}

@Test
public void elementHasClassToString() {
assertEquals("css class 'Foo'", Condition.hasClass("Foo").toString());
Expand Down Expand Up @@ -400,4 +406,10 @@ private WebElement elementWithSelectedAndText(boolean isSelected, String text) {
when(element.getText()).thenReturn(text);
return element;
}

private WebElement elementWithCssStyle(String propertyName, String value) {
WebElement element = mock(WebElement.class);
when(element.getCssValue(propertyName)).thenReturn(value);
return element;
}
}
149 changes: 94 additions & 55 deletions src/test/java/integration/SelenideMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,66 @@
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.ElementShould;
import com.codeborne.selenide.ex.ElementShouldNot;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Condition.attribute;
import static com.codeborne.selenide.Condition.be;
import static com.codeborne.selenide.Condition.cssClass;
import static com.codeborne.selenide.Condition.cssValue;
import static com.codeborne.selenide.Condition.disabled;
import static com.codeborne.selenide.Condition.disappear;
import static com.codeborne.selenide.Condition.disappears;
import static com.codeborne.selenide.Condition.empty;
import static com.codeborne.selenide.Condition.enabled;
import static com.codeborne.selenide.Condition.exactText;
import static com.codeborne.selenide.Condition.exist;
import static com.codeborne.selenide.Condition.focused;
import static com.codeborne.selenide.Condition.hasValue;
import static com.codeborne.selenide.Condition.have;
import static com.codeborne.selenide.Condition.hidden;
import static com.codeborne.selenide.Condition.id;
import static com.codeborne.selenide.Condition.name;
import static com.codeborne.selenide.Condition.not;
import static com.codeborne.selenide.Condition.present;
import static com.codeborne.selenide.Condition.readonly;
import static com.codeborne.selenide.Condition.selected;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Condition.type;
import static com.codeborne.selenide.Condition.value;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Configuration.baseUrl;
import static com.codeborne.selenide.Configuration.timeout;
import static com.codeborne.selenide.Selectors.*;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.WebDriverRunner.*;
import static com.codeborne.selenide.Selectors.by;
import static com.codeborne.selenide.Selectors.byCssSelector;
import static com.codeborne.selenide.Selectors.byId;
import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selectors.byValue;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.actions;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.sleep;
import static com.codeborne.selenide.Selenide.title;
import static com.codeborne.selenide.Selenide.zoom;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
import static com.codeborne.selenide.WebDriverRunner.isChrome;
import static com.codeborne.selenide.WebDriverRunner.isFirefox;
import static com.codeborne.selenide.WebDriverRunner.isHtmlUnit;
import static com.codeborne.selenide.WebDriverRunner.isPhantomjs;
import static com.codeborne.selenide.WebDriverRunner.url;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

public class SelenideMethodsTest extends IntegrationTest {
Expand Down Expand Up @@ -79,16 +123,14 @@ public void toStringMethodShowsElementDetails() {
assertEquals("<h1>Page with selects</h1>", $("h1").toString());
assertEquals("<h2>Dropdown list</h2>", $("h2").toString());
assertEquals("<input name=\"rememberMe\" type=\"checkbox\" value=\"on\"></input>",
$(By.name("rememberMe")).toString());
$(By.name("rememberMe")).toString());

if (isHtmlUnit()) {
assertEquals("<option value=\"livemail.ru\" selected:true>@livemail.ru</option>",
$(By.name("domain")).find("option").toString());

}
else {
$(By.name("domain")).find("option").toString());
} else {
assertEquals("<option data-mailserverid=\"111\" value=\"livemail.ru\" selected:true>@livemail.ru</option>",
$(By.name("domain")).find("option").toString());
$(By.name("domain")).find("option").toString());
}

assertTrue($(byText("Want to see ajax in action?")).toString().contains("<a href="));
Expand All @@ -99,14 +141,13 @@ public void toStringMethodShowsElementDetails() {
public void toStringShowsAllAttributesButStyleSortedAlphabetically() {
if (isHtmlUnit()) {
assertEquals("<div class=\"invisible-with-multiple-attributes\" id=\"gopher\" " +
"onclick=\"void(0);\" onchange=\"console.log(this);\" placeholder=\"Животное\" " +
"displayed:false></div>", $("#gopher").toString());
}
else {
"onclick=\"void(0);\" onchange=\"console.log(this);\" placeholder=\"Животное\" " +
"displayed:false></div>", $("#gopher").toString());
} else {
assertEquals("<div class=\"invisible-with-multiple-attributes\" " +
"data-animal-id=\"111\" id=\"gopher\" ng-class=\"widget\" ng-click=\"none\" " +
"onchange=\"console.log(this);\" onclick=\"void(0);\" placeholder=\"Животное\" " +
"displayed:false></div>", $("#gopher").toString());
"data-animal-id=\"111\" id=\"gopher\" ng-class=\"widget\" ng-click=\"none\" " +
"onchange=\"console.log(this);\" onclick=\"void(0);\" placeholder=\"Животное\" " +
"displayed:false></div>", $("#gopher").toString());
}
}

Expand All @@ -127,24 +168,23 @@ public void userCanGetInnerHtmlOfElement() {
if (isHtmlUnit()) {
assertEquals("<span></span> L'a\n Baskerville", $("#baskerville").innerHtml().trim());
assertEquals("Username: <span class=\"name\">Bob Smith</span> Last login: <span class=\"last-login\">01.01.1970</span>",
$("#status").innerHtml().trim());
}
else {
$("#status").innerHtml().trim());
} else {
assertEquals("<span></span> L'a\n Baskerville", $("#baskerville").innerHtml().trim());
assertEquals("Username: " +
"<span class=\"name\">Bob Smith</span>&nbsp;" +
"Last login: <span class=\"last-login\">01.01.1970</span>",
$("#status").innerHtml().trim());
"<span class=\"name\">Bob Smith</span>&nbsp;" +
"Last login: <span class=\"last-login\">01.01.1970</span>",
$("#status").innerHtml().trim());
}
}

@Test
public void userCanGetTextAndHtmlOfHiddenElement() {
assertEquals("видишь суслика? и я не вижу. <b>а он есть</b>!",
$("#theHiddenElement").innerHtml().trim().toLowerCase());
$("#theHiddenElement").innerHtml().trim().toLowerCase());

assertEquals("Видишь суслика? И я не вижу. А он есть!",
$("#theHiddenElement").innerText().trim());
$("#theHiddenElement").innerText().trim());
}

@Test
Expand Down Expand Up @@ -222,12 +262,11 @@ public void userCanCheckIfElementContainsText() {
assertEquals("@livemail.ru", $(By.name("domain")).find("option").text());
if (isHtmlUnit()) {
assertEquals("Radio buttons\n" +
"uncheckedМастер " +
"uncheckedМаргарита " +
"uncheckedКот \"Бегемот\" " +
"uncheckedTheodor Woland", $("#radioButtons").text());
}
else {
"uncheckedМастер " +
"uncheckedМаргарита " +
"uncheckedКот \"Бегемот\" " +
"uncheckedTheodor Woland", $("#radioButtons").text());
} else {
assertEquals("Radio buttons\nМастер Маргарита Кот \"Бегемот\" Theodor Woland", $("#radioButtons").text());
}

Expand All @@ -244,11 +283,10 @@ public void userCanCheckIfElementHasExactText() {
$(By.name("domain")).find("option").shouldHave(text("@livemail.ru"));
if (isHtmlUnit()) {
$("#radioButtons").shouldHave(text("Radio buttons\n" +
"uncheckedМастер uncheckedМаргарита uncheckedКот \"Бегемот\" uncheckedTheodor Woland"));
}
else {
"uncheckedМастер uncheckedМаргарита uncheckedКот \"Бегемот\" uncheckedTheodor Woland"));
} else {
$("#radioButtons").shouldHave(text("Radio buttons\n" +
"Мастер Маргарита Кот \"Бегемот\" Theodor Woland"));
"Мастер Маргарита Кот \"Бегемот\" Theodor Woland"));
}
}

Expand Down Expand Up @@ -304,7 +342,6 @@ public void userCanFollowLinks() {
assertTrue("Actual URL is: " + url(), url().contains("long_ajax_request.html"));
}


@Test
public void userCanFollowLinksUsingScrollIntoViewBoolean() {
$(By.linkText("Want to see ajax in action?")).scrollIntoView(false).followLink();
Expand Down Expand Up @@ -350,6 +387,14 @@ public void userCanCheckCssClass() {
$(byText("First name")).shouldNotHave(cssClass("anything"));
}

@Test
public void userCanCheckCssValue() {
$(byId("theHiddenElement")).shouldHave(cssValue("display", "none"));
$(byText("First name")).shouldNotHave(cssValue("font-size", "24"));
$(byText("Last name")).shouldHave(cssValue("non-exist-prop", ""));
$(byCssSelector("#status")).shouldHave(cssValue("line-height", "10px"));
}

@Test
public void userCanGetPageTitle() {
assertEquals("Test page :: with selects, but withour JQuery", title());
Expand Down Expand Up @@ -407,8 +452,7 @@ public void errorMessageShouldContainUrlIfBrowserFailedToOpenPage() {
try {
baseUrl = "http://localhost:8080";
open("www.yandex.ru");
}
catch (WebDriverException e) {
} catch (WebDriverException e) {
assertTrue(e.getAdditionalInformation().contains("selenide.baseUrl: http://localhost:8080"));
assertTrue(e.getAdditionalInformation().contains("selenide.url: http://localhost:8080www.yandex.ru"));
}
Expand All @@ -429,13 +473,13 @@ public void userCanDoubleClickOnElement() {
openFile("page_with_jquery.html");

$("#double-clickable-button")
.shouldHave(value("double click me"))
.shouldBe(enabled);
.shouldHave(value("double click me"))
.shouldBe(enabled);

$("#double-clickable-button")
.doubleClick()
.shouldHave(value("do not click me anymore"))
.shouldBe(disabled);
.doubleClick()
.shouldHave(value("do not click me anymore"))
.shouldBe(disabled);

$("h2").shouldHave(text("Double click worked"));
}
Expand Down Expand Up @@ -463,7 +507,6 @@ public void checkFailsForInvalidSelector() {
$(By.xpath("//input[:attr='al]")).is(visible);
}


@Test
public void shouldMethodsMayContainOptionalMessageThatIsPartOfErrorMessage_1() {
timeout = 100L;
Expand Down Expand Up @@ -501,16 +544,14 @@ public void shouldNotMethodsMayContainOptionalMessageThatIsPartOfErrorMessage()
try {
$("h1").shouldNotHave(text("Page with selects").because("it's wrong text"));
fail("exception expected");
}
catch (ElementShouldNot expected) {
} catch (ElementShouldNot expected) {
assertTrue(expected.getMessage().contains("because it's wrong text"));
}

try {
$("h1").shouldNotBe(text("Page with selects").because("it's wrong text"));
fail("exception expected");
}
catch (ElementShouldNot expected) {
} catch (ElementShouldNot expected) {
assertTrue(expected.getMessage().contains("because it's wrong text"));
}
}
Expand All @@ -520,10 +561,9 @@ public void waitWhileMethodMayContainOptionalMessageThatIsPartOfErrorMessage() {
try {
$("h1").waitWhile(visible.because("we expect it do disappear"), 100);
fail("exception expected");
}
catch (ElementShouldNot expected) {
} catch (ElementShouldNot expected) {
assertTrue("Actual error: " + expected.getMessage(),
expected.getMessage().contains("because we expect it do disappear"));
expected.getMessage().contains("because we expect it do disappear"));
}
}

Expand All @@ -532,10 +572,9 @@ public void waitUntilMethodMayContainOptionalMessageThatIsPartOfErrorMessage() {
try {
$("h1").waitUntil(hidden.because("it's sensitive information"), 100);
fail("exception expected");
}
catch (ElementShould expected) {
} catch (ElementShould expected) {
assertTrue("Actual error: " + expected.getMessage(),
expected.getMessage().contains("because it's sensitive information"));
expected.getMessage().contains("because it's sensitive information"));
}
}

Expand Down
Loading