Search code examples
javascriptphpjqueryseleniumcodeception

jQuery in Codeception is undefined


I'm using Codeception to write acceptance tests for an app that relies on jQuery. I got the WebDriver to open FireFox and login. JavaScript is enabled, and I can do $I->executeJS('return document.getElementById("some-element")'); which returns the element. For the life of me, I can't access jQuery at all. I know it's loaded because the jQuery plugins on the page are working. I need to interact with then.

So far, I've tried two approaches. The first is that I create a hidden div with an ID when the DOM is ready. Then in my test, I do this:

public function foo(AcceptanceTester $I) {
    $I->waitForElement('#dom-ready');
    $var = $I->executeJS('return jQuery.fn.jquery');
    var_dump($var);
}

That var_dump should return the jQuery version. It's returns null.

I've also tried this:

public function foo(AcceptanceTester $I) {
    $I->wait(20); // Some really high number of seconds
    $var = $I->executeJS("return window.jQuery != undefined && jQuery.active == 0");
    var_dump($var);
}

That var_dump should be true. It's always false. I'm totally at a loss. What am I no seeing here?

Here is my acceptance.suite.yaml file

class_name: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: 'http://site.local/' # put your local url
            browser: firefox
            capabilities:
                javascriptEnabled: true
                handlesAlerts: true
                webStorageEnabled: true
        - \Helper\Acceptance

Solution

  • Got it! The issue was indeed with Firefox, not Selenium. Thanks Florent B for suggesting the idea that the geckodriver could be the issue. Short answer is you need at least FF >= 48 to access jQuery.

    https://github.com/mozilla/geckodriver/issues/108

    Hope this helps someone. Took me almost a week to resolve this.