Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selenium2TestCase->execute() not very obvious #160

Closed
xrstf opened this issue Aug 5, 2012 · 5 comments
Closed

Selenium2TestCase->execute() not very obvious #160

xrstf opened this issue Aug 5, 2012 · 5 comments

Comments

@xrstf
Copy link

xrstf commented Aug 5, 2012

I tried to execute some JavaScript in my page and therefore the execute() method, as it is described in v1.2.7:

@method string execute($javaScriptCode) Injects arbitrary JavaScript in the page and returns the last

This made me write:

<?
protected function injectErrorHandler() {
    return $this->execute("
window._errors = [];
window.onerror = function(args) {
    var args = arguments, len = args.length, i = 0;

    for (; i < len; ++i) {
        window._errors.push(args[i]);
    }
};

return 1;
");
}

This led to exceptions about invalid JSON parameters. Parameters should be an arry, so I changed my code to

<?
protected function injectErrorHandler() {
    return $this->execute(array("/* snip */"));
}

This worked in PHPUnit, but made Selenium crash because it couldn't convert the list to a map. After (finally) consuling the WireProtocol, it was clear that I had to write

<?
protected function injectErrorHandler() {
    return $this->execute(array('script' => "/* snip */"));
}

Now the JavaScript is executed, but directly followed by another exception in Selenium. It turns out that I must define the arguments, even if there are none.

<?
protected function injectErrorHandler() {
    return $this->execute(array(
        'script' => "/* snip */",
        'args' => array()
    ));
}

Was I supposed to know that all magic GenericPost commands need to have a proper params array that I have to get from the WireProtocol specs? Is this just an oversight? Is this implementation simply not yet finished and therefore no dedicated command class exists?

@giorgiosironi
Copy link
Owner

The annotations are incorrect and are now fixed. All the implemented functionalities are documented in the unit tests, like here:
https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php
GenericPost is used when either a command has no arguments, or when a more specific implementation does not exist yet. If you want you can contribute an Execute class that takes as an argument a single string containing the code (but also accept this call for retrocompatibility or custom parameters).
For an example of Command, see:
https://github.com/giorgiosironi/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/SessionCommand/MoveTo.php

@chrisplusplus
Copy link

Anyone know why I get this exception when trying to use the execute method?
command:
$this->execute(array(
'script' => $js,
'args' => array()
));

error given:
Method execute not defined.

I suspect it has something to do with the proper classes not being included, but I was under the impression that they were included because I am able to run other Selenium commands just fine.
I'm using PHPUnit+Selenium within within Yii. Thanks!

@giorgiosironi
Copy link
Owner

Which class are you extending?

@chrisplusplus
Copy link

I'm using yii. Which I finally discovered had an abstract class extending
SeleniumTestCase rather than Selenium2TestCase (which extends phpunit).
After changing that, and then realizing the code I wrote for selenium RC no
longer works, the execute command works as expected. Sorry for the noob
questions here. I'm just now beginning my journey using Selenium/PHPUnit.
On Oct 10, 2015 4:14 AM, "Giorgio Sironi" notifications@github.com wrote:

Which class are you extending?


Reply to this email directly or view it on GitHub
#160 (comment)
.

@giorgiosironi
Copy link
Owner

Glad this was solved for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants