org.openqa.selenium.WebDriverException: ReferenceError: jQuery is not defined
Asked Answered
I

3

2

Hi I am trying to write autonomous test using Webdriver for firefox profile, I enabled the javascript equal to true while creating Driver object.

In some view jquery responses late so for that I tried to put one check in webdriver code to wait For JQuery Processing

Code snippet for waitForJQueryProcessing:

public static boolean waitForJQueryProcessing(WebDriver driver,
        int timeOutInSeconds) {
    boolean jQcondition = false;
    try {
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject)
                        .executeScript("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver)
                .executeScript("return jQuery.active == 0");
        return jQcondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jQcondition;
}

But the above code is rising exception

Stacktrace


org.openqa.selenium.WebDriverException: ReferenceError: jQuery is not defined
Command duration or timeout: 10 milliseconds
Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
Session ID: 58ad81d0-53f9-4862-a916-a1900efdc9c0
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=21.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463)
    at com.iclinica.utils.WaitTool$9.apply(WaitTool.java:309)
    at com.iclinica.utils.WaitTool$9.apply(WaitTool.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    at com.iclinica.utils.WaitTool.waitForJQueryProcessing(WaitTool.java:304)
    at com.iclinica.globals.FirefoxCustomWebdriver.findElement(FirefoxCustomWebdriver.java:14)
    at com.iclinica.page.studyconfig.studydetails.StudyDetailsPage.studydetails(StudyDetailsPage.java:20)
    at com.iclinica.studyconfig.AddPatients.teststudycreation(AddPatients.java:168)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

I googled for setting jquery file path in webdriver object, but didn't find any result

I hope it makes sense.

Thanks Gaurav

Issue answered 20/6, 2013 at 8:35 Comment(2)
Well is jQuery loaded on the page? Sounds like it isn't.Dehart
Thanks @Arran, you mentioned correctly. Due to some error in js script, it was not loading js for multiple jsp, but for one jsp the same exception is still coming and I also verified the same jsp using firebug, but firebug is not showing any error.Issue
M
4

Use this instead:

public static boolean waitForJQueryProcessing(WebDriver driver,
        int timeOutInSeconds) {
    boolean jQcondition = false;
    try {
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject)
                        .executeScript("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver)
                .executeScript("return window.jQuery != undefined && jQuery.active === 0");
        return jQcondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jQcondition;
}

The change from your original code snippet is:

.executeScript("return window.jQuery != undefined && jQuery.active === 0");

This will make sure that your jQuery object is defined before checking if there are any active jQuery processes. Selenium runs fast and can sometimes make queries to jQuery before it has had a chance to load into the page you are testing.

Moguel answered 26/6, 2013 at 9:34 Comment(0)
F
3

There is one shorter version of code which works for me:

public void waitForAjaxLoad(WebDriver driver) throws InterruptedException{
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    if((Boolean) executor.executeScript("return window.jQuery != undefined")){
        while(!(Boolean) executor.executeScript("return jQuery.active == 0")){
            Thread.sleep(1000);
        }
    }
    return;
}

Milliseconds (1000) can be added to parameter of method.

Faa answered 22/9, 2014 at 8:32 Comment(0)
I
0

When you test for jQuery completion do not forget to add a check for jQuery being undefined else you will end up with :

ReferenceError: jQuery is not defined error.

jQuery check you should perform:

(Boolean)((JavascriptExecutor) wd).executeScript("return window.jQuery != undefined && jQuery.active == 0")

Now when you write method then I would suggest to use Fluent Wait in your selenium code rather than implicit or explicit wait. Fluent wait method will help you do operation in between the polling interval wait unlike other waits and is very useful or rather powerful.

Below is the working method which you can directly use :

public static void pageJqueryLoad(WebDriver driver, Duration waitTimeout) {
        Wait<WebDriver> wait = new FluentWait<>(driver)
                .withTimeout(waitTimeout)
                .pollingEvery(Duration.ofMillis(500))
                .ignoring(NoSuchElementException.class);
        wait.until((ExpectedCondition<Boolean>) wd -> {
            log.info("Waiting for Page jQuery to complete");
            log.info("jQuery.active value is : " + ((JavascriptExecutor) wd).executeScript("return window.jQuery != undefined && jQuery.active"));
                   (Boolean)((JavascriptExecutor) wd).executeScript("return window.jQuery != undefined && jQuery.active == 0");
        });
    }

In the above method :

  1. You need to pass your driver and waitTimeout duration as argument to this method. For ex: pageJqueryLoad(driver, Duration.ofSeconds(120));
  2. I have defined polling interval as 500 ms. You can modify as per your need.
  3. Every time a poll is done it prints the 3 statement given under log.info.
  4. Using this you can easily add code to determine the exact time your page was rendered completely before doing test operations.
Inspectorate answered 12/10, 2022 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.