Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute javascript with behat and phantomjs

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 ...

++

like image 735
fxlacroix Avatar asked Dec 16 '13 10:12

fxlacroix


2 Answers

You have not solved the problem? Try it:

$this->getSession()->getDriver()->evaluateScript(
    "function(){ return 'some expression'; }()"
);
like image 77
Stanislav Butsenko Avatar answered Nov 15 '22 15:11

Stanislav Butsenko


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

like image 41
Manuel K. Avatar answered Nov 15 '22 14:11

Manuel K.