Search code examples
seleniumcodeception

How to execute Javascript with Codeception and get the returned values


Is there any way of making this work ?

    $I->amOnPage('/');

    $I->wait(2);

    $I->executeJS('
        var theCookies = document.cookie.split(\';\');
        var aString = \'\';
        for (var i = 1 ; i <= theCookies.length; i++) {
            aString += i + \' \' + theCookies[i-1] + "\n";
        }
        return aString;
    ');

Besides the fact that I get an error thrown, would something like this be possible?


Solution

  • It's documented here: https://codeception.com/docs/modules/WebDriver

    executeJS

    Executes custom JavaScript.

    This example uses jQuery to get a value and assigns that value to a PHP variable:

    $myVar = $I->executeJS('return $("#myField").val()');
    
    // additional arguments can be passed as array
    // Example shows `Hello World` alert:
    $I->executeJS("window.alert(arguments[0])", ['Hello world']);