1

Working in an inherited automation codebase. There is a method which waits to see if jQuery has been initialized.

.until(new Function<WebDriver, Boolean>() {
                    @Override
                    public Boolean apply(WebDriver webDriver) {
                        boolean go = (Long) runJavaScript("return jQuery.active") == 0;
                        if (!go) {
                            LOGGER.debug("stalling");
                        }
                        return go;
                    }

Ran into an issue with jQuery not being defined. I added a check to see if jQuery is defined and refactored the code to use a lambda expression

.until((Function<WebDriver, Boolean>) webDriver -> {
                    boolean go = (Boolean) runJavaScript("return !!window.jQuery && jQuery.active == 0");
                    if(!go){
                        LOGGER.debug("stalling");
                    }
                    return go;
                });

My question pertains to the casting of the return of runJavaScript which is just a custom wrapper of JavaScriptExecutor's executeScript method. Initially it was built to return jQuery.active cast it to a Long and compare it to zero. I refactored it to cast the return of jQuery.active == 0 to a boolean. Is there any value to cast to a Long over a boolean in this case?

1
  • 1
    I'm not sure what the advantage would be in casting it to a long. If it's a Boolean value, why confuse that by giving it a type that has a huge range of viable values? If there is a performance benefit, I'd be surprised if it was noticeable in your test suite. In short, I would go with Boolean because there is no reason to add complexity and confusion to what is trying to be achieved.
    – mrfreester
    Commented Sep 25, 2017 at 17:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.