Symfony2 Functional Testing - Click on elements wi

2019-03-20 04:38发布

问题:

I'm doing some functional tests for an application done with Symfony2 (2.1) and I'm stuck with a problem.

I have some parts of the website that load when the 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 do 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 launches it, but the "href" is empty, and a "click()" function is associated with this element, preventing the click action with "preventDefault()".

Thank you everyone!! :)

回答1:

Symfony functional tests exercise your code by directly calling the Symfony kernel. They're not run through a web browser and therefore don't support javascript (which is simply not executed).

If it's not possible to run your application without javascript, than you have to use another tool for functional testing. One of the options is to use Mink with one of the drivers supporting javascript (like Selenium2).



回答2:

I could use a "headless" browser for this task as PhantomJS .

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

In order to ease your work, You could use CasperJS

CasperJS is a companion for PhatomJS which brings a greatly improved API to ease the creation of scraping and automation workflows.

In your case, where the web context that you are trying to crawl includes dynamic content through JQuery and AJAX, CasperJS is an excellent option if you want to use Javascript in order to achieve that. You can use it to trigger events, add process steps, include functions to wait and validate after each ajax call before to process any next step.

Here an example how crawl a website with CasperJS and JQuery: CasperJs and Jquery with chained Selects

Here an example how crawl a website with CasperJS and just Javascript: CasperJS dynamic selectlists



回答3:

Symfony doesn't support javascript, Ajax, Jquery. It's used by phpunit for testing php functionnalities. It wasn't made for this.

You can use casperjs. With CasperJS you could do your functional test like with your crawler on Symfony and you get all javascript and css code.