-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/codeborne/selenide/conditions/PartialValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters