Selenide: Extending com.codeborne.selenide.Condition

Of all the libraries I’ve used over the years, Selenide has come to occupy a top spot in my developer’s toolkit second only to that of jQuery. Selenide has played a crucial role in making Selenium (while functional) easy to use.

I’ve been using Selenide for a few months on a current project; I’ve finally got enough code coverage to where I’m able to take a few minutes and refactor the test code to be more efficient and readable. I’ve been suffering from some intermittent test failures due to timing issues with JUnit asserts, and finally found the time to address them.

Previously, I had written:

assertTrue("Element should be displayed.", $(elem).isDisplayed());

But with a few months of Selenide under my belt, I now know that it’s always better to use the SelenideElement.should() methods where possible. With should(), Selenide will by default check for a state every 100ms until 4 seconds have expired. But, Selenide does not include a check for whether an application is in fullscreen mode. By extending the existing com.codeborne.selenide.Condition class, I get:

package dg.mydg.selenide;
import org.openqa.selenium.WebElement;
import static com.codeborne.selenide.Selenide.executeJavaScript;

public abstract class Condition extends com.codeborne.selenide.Condition {

    /**
     * Checks to see whether the application is in fullscreen mode.
     */
    public static final Condition fullscreen = new Condition("fullscreen") {
        @Override public boolean apply(WebElement element) {
            Boolean isFullscreen = executeJavaScript("return (window.innerWidth == screen.width)");
            return ((isFullscreen == null) ? false : isFullscreen);
        }

        @Override public String toString() {
            return "is in fullscreen mode"; 
        }
    };
}

Now I can use it within the Selenide should framework:

$(elem).shouldBe(fullscreen);

Leave a Reply