execute javascript with behat and phantomjs
Asked Answered
C

3

6

I am working on a solution for functional test with javascript support.

Naturally, using Symfony Framework, I choose Behat with Sahi driver, and I had my test suites green. The problem was that sahi is quite slow, and not stable enough and that is why I turn to PhantomJs as this blog post mentions: -> http://shashikantjagtap.net/running-behat-scenarios-with-pahntomjs/

Some of my tests stay green, but when I use $this->getSession()->getDriver()->evaluateScript(), there is no return and I can't evaluate javascript.

If someone ever deal with and found a solution ...

++

Capua answered 16/12, 2013 at 10:33 Comment(1)
For testing, there is sample project: github.com/Shashikant86/BehatDemoCapua
I
5

You have not solved the problem? Try it:

$this->getSession()->getDriver()->evaluateScript(
    "function(){ return 'some expression'; }()"
);
Idolah answered 20/1, 2014 at 22:13 Comment(1)
please add your additional questions to the comments section, not as answersHilary
D
3

Inside your method you can use:

$result = $this->getSession()->evaluateScript("$('input').val();");
var_dump($result);

In my use case will return the value of the first input tag in the code. As you see, you can use jQuery if you have it in your page.

the "Scenario" must run with the tag @javascript

Drafty answered 3/12, 2014 at 12:40 Comment(0)
T
0

I was trying to fill in a Drupal textfield using the built in step definition fillField() (i.e "I fill in "textfield" with "my value") and in the value I had an opening parenthesis which would get strip off. I then decided to write a custom function to append the value programmatically as shown below:

$element = $session->getPage()->find(
        'xpath',
        $session->getSelectorsHandler()->selectorToXpath('css',     $cssSelector) // just changed xpath to css
    );
    if (null === $element) {
        throw new \InvalidArgumentException(sprintf('Could not evaluate CSS Selector: "%s"', $cssSelector));
    }

    $element->focus();
    $element->setValue("(2090)");

But this also had the same issue (the opening parenthesis would get strip off and leave the value as "2090)"

Finally, I managed to get it to work using your jquery example:

$this->getSession()->evaluateScript("jQuery('#edit-og-group-ref-und-0-default').val("(2090)");"); 

And voila it did work. Thanks.

Teetotum answered 23/5, 2016 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.