self-learner
self-learner

Reputation: 33

How to Click link in phpunit testing with Symfony 2

I am new in PHPUnit Testing with Symfony 2.

I am trying to click a link to and test whether its redirects to the page which contains some text. My code is :

$client = static::createClient();

$crawler = $client->request('GET', '/Site'); 

$link = $crawler->filter('a:contains("Click for Report")')->eq(1)->link(); 

$crawler->$client->click($link);

$this->assertEquals(200, $client->getResponse()->getStatusCode());

$this->assertContains('Detail Report',$client->getResponse()->getContent());

Every time I run this test an error says: "InvalidArgumentException: The current node list is empty." on the code >>

$link = $crawler->filter('a:contains("Detail Report")')->eq(1)->link();

I don't know why the node is empty.

Any help will be highly Appreciated ! Thanks in advance.

Upvotes: 1

Views: 3978

Answers (1)

BlueM
BlueM

Reputation: 3851

First of all: this is not unit-testing – this is a functional test.

Regarding your test: theoretically, it should work, but without the HTML, it’s difficult to say where’s the problem. Chances are it’s simply the eq(1), as the argument to eq() is 0-based, not 1-based, so you are selecting the 2nd link that contains “Click for Report”, and maybe there is only 1 such link on the page.

BTW, getting the link like this is simpler and more readable:

$crawler->selectLink('Click for Report')->link()

Upvotes: 3

Related Questions