Skip to content

Commit

Permalink
#1923 add condition partialValue
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Aug 16, 2022
1 parent bef146f commit 4660556
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/codeborne/selenide/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.codeborne.selenide.conditions.Or;
import com.codeborne.selenide.conditions.OwnText;
import com.codeborne.selenide.conditions.OwnTextCaseSensitive;
import com.codeborne.selenide.conditions.PartialValue;
import com.codeborne.selenide.conditions.PseudoElementPropertyWithValue;
import com.codeborne.selenide.conditions.Readonly;
import com.codeborne.selenide.conditions.Selected;
Expand Down Expand Up @@ -213,6 +214,21 @@ public static Condition value(String expectedValue) {
return new Value(expectedValue);
}

/**
* Assert that element contains given "value" attribute as substring
* NB! Ignores difference in non-visible characters like spaces, non-breakable spaces, tabs, newlines etc.
*
* <p>Sample: {@code $("input").shouldHave(partialValue("12345 666 77"));}</p>
*
* @param expectedValue expected value of "value" attribute
* @since 6.7.3
*/
@CheckReturnValue
@Nonnull
public static Condition partialValue(String expectedValue) {
return new PartialValue(expectedValue);
}

/**
* Check that element has given the property value of the pseudo-element
* <p>Sample: {@code $("input").shouldHave(pseudo(":first-letter", "color", "#ff0000"));}</p>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/codeborne/selenide/conditions/PartialValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.codeborne.selenide.conditions;

import com.codeborne.selenide.CheckResult;
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Driver;
import com.codeborne.selenide.impl.Html;
import org.openqa.selenium.WebElement;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import static org.apache.commons.lang3.StringUtils.isEmpty;

@ParametersAreNonnullByDefault
public class PartialValue extends Condition {
private final String expectedValue;

public PartialValue(String expectedValue) {
super("partial value");
this.expectedValue = expectedValue;

if (isEmpty(expectedValue)) {
throw new IllegalArgumentException("Argument must not be null or empty string. " +
"Use $.shouldBe(empty) or $.shouldHave(exactValue(\"\").");
}
}

@Nonnull
@CheckReturnValue
@Override
public CheckResult check(Driver driver, WebElement element) {
String value = getValueAttribute(element);
String actualValue = String.format("value=\"%s\"", value);
boolean valueMatches = Html.text.contains(value, expectedValue);
return new CheckResult(valueMatches, actualValue);
}

@Nonnull
@CheckReturnValue
@Override
public String toString() {
return String.format("%s \"%s\"", getName(), expectedValue);
}

private String getValueAttribute(WebElement element) {
String attr = element.getAttribute("value");
return attr == null ? "" : attr;
}
}
31 changes: 31 additions & 0 deletions statics/src/test/java/integration/GetSetValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.ElementShould;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Condition.attribute;
import static com.codeborne.selenide.Condition.partialValue;
import static com.codeborne.selenide.Condition.value;
import static com.codeborne.selenide.Selenide.$;
import static java.time.Duration.ofMillis;
Expand Down Expand Up @@ -75,6 +77,35 @@ void valueCheckIgnoresDifferenceInInvisibleCharacters() {
$("#empty-text-area").shouldHave(value("john Malkovich"));
}

@Test
void canCheckValueSubstring() {
$(By.name("password")).setValue("John Malkovich");
$(By.name("password")).shouldHave(partialValue("ohn Malkov"));

$("#empty-text-area").setValue("John Malkovich");
$("#empty-text-area").shouldHave(partialValue("hn Malk"));
}

@Test
void value_errorMessage() {
$("#empty-text-area").setValue("Bilbo Baggins");

assertThatThrownBy(() -> $("#empty-text-area").shouldHave(value("Bilbo Sumkins")))
.isInstanceOf(ElementShould.class)
.hasMessageStartingWith("Element should have value=\"Bilbo Sumkins\" {#empty-text-area}")
.hasMessageContaining("Actual value: value=\"Bilbo Baggins\"");
}

@Test
void partialValue_errorMessage() {
$("#empty-text-area").setValue("Bilbo Baggins");

assertThatThrownBy(() -> $("#empty-text-area").shouldHave(partialValue("Bilbo Big")))
.isInstanceOf(ElementShould.class)
.hasMessageStartingWith("Element should have partial value \"Bilbo Big\" {#empty-text-area}")
.hasMessageContaining("Actual value: value=\"Bilbo Baggins\"");
}

@Test
void userCanAppendValueToTextField() {
$(By.name("password")).val("Sherlyn");
Expand Down

0 comments on commit 4660556

Please sign in to comment.