Symfony2 functional testing - click on elements with jQuery interaction

I am running some functional tests for an application running using Symfony2 (2.1) and I am having a problem.

I have some parts of a website that load when a user clicks a link or other element, but these actions are performed using jQuery and $ .post calls. How can I get the symfony2 crawler to make these calls?

When I do something like this:

$link = $crawler->filter('ul.line_menu a')->eq(1)->link(); $crawler = $client->click($link); 

The crawler gets the "href" of the "a" element and starts it, but the "href" is empty, and the "click ()" function is associated with this element, preventing the click action from "preventDefault ()".

Thanks everyone! :)

+8
jquery symfony web-crawler phpunit functional-testing
source share
3 answers

Symfony function tests implement your code by directly invoking the Symfony kernel. They do not run through a web browser and therefore do not support javascript (which simply does not execute).

If you are unable to run the application without javascript, you need to use another tool for functional testing. One option is to use Mink with one of the drivers that support javascript (for example, Selenium2 ).

+7
source share

I could use a headless browser for this task like PhantomJS .

PhantomJS is a mute WebKit script with a JavaScript API. It has fast and native support for various web standards: DOM processing, CSS selector, JSON, Canvas and SVG.

To make your work easier, you can use CasperJS

CasperJS is a companion to PhatomJS, which brings a significantly improved API to facilitate the creation of cleaning and automated workflows.

In your case, when the web context you are trying to bypass includes dynamic content through JQuery and AJAX, CasperJS is a great option if you want to use Javascript for this. You can use it to trigger events, add process steps, enable functions to wait and verify after each ajax call before processing the next step.

Here is an example of how to crawl a website using CasperJS and jQuery: CasperJs and jQuery with chain selection

Here is an example of how to crawl a website using CasperJS and only Javascript: CasperJS dynamic selectors

+4
source share

Symfony does not support javascript, Ajax, jQuery. It is used by phpunit to test php functionality. It was not done for this.

You can use casperjs . With CasperJS, you can perform your functional test, for example, using your symfony crawler, and you will get all the javascript and css code.

+1
source share

All Articles