Last active
January 5, 2022 14:55
-
-
Save IsmagilovQA/311ad0d31c6832e9d247acba44926833 to your computer and use it in GitHub Desktop.
Selenide | Custom condition: ElementWithinViewportCondition
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
https://gist.github.com/BorisOsipov/16a1ac309f0f9acd9ee34ac50fc0b45f | |
@ParametersAreNonnullByDefault | |
public class ElementWithinViewportCondition extends Condition { | |
public ElementWithinViewportCondition() { | |
super("visible"); | |
} | |
@Override | |
public boolean apply(Driver driver, WebElement element) { | |
return isElementWithinViewport(element); | |
} | |
@Override | |
@CheckReturnValue | |
@Nonnull | |
public String actualValue(Driver driver, WebElement element) { | |
return String.format("элемент внутри видимой части экрана: %s", isElementWithinViewport(element)); | |
} | |
@Override | |
public String toString() { | |
return String.format("%s", getName()); | |
} | |
@CheckReturnValue | |
private boolean isElementWithinViewport(WebElement element) { | |
return Boolean.TRUE.equals(Selenide.executeJavaScript("const elem = arguments[0];" | |
+ "const box = elem.getBoundingClientRect();" | |
+ "const cx = box.left + box.width / 2;" | |
+ "const cy = box.top + box.height / 2;" | |
+ "let e = document.elementFromPoint(cx, cy);" | |
+ "for (; e; e = e.parentElement) {" | |
+ " if (e === elem) {" | |
+ " return true;" | |
+ " }" | |
+ "}" | |
+ "return false;", element)); | |
} | |
} | |
$(".foo").shouldBe(new ElementWithinViewportCondition()); | |
$(".foo").shouldNot(new ElementWithinViewportCondition()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment