Symfony2 Mink Behat Testing: The Case For a Bug

In order to run phpunit I can’t just use directly phpunit on the folder but first I need the libraries:

cd vendor/behat/mink
git submodule update --init --recursive
cp phpunit.xml.dist phpunit.xml

This will not only fetch independently all that the tests require but it will set independent sub libraries like zend and even symfony. These are important because tests have to be independent of your configuration or your project packages. This creates a unique standard environment for testing that can be reproducible anywhere.

phpunit

The tests will require us to have set certain parameters and be running the sahi client:

// running sahi client
cd ~/sahi/userdata/bin/
./start_sahi.sh
 
// setting up the test domain for mink
  <server name="WEB_FIXTURES_HOST" value="http://testmink.local" />
  <server name="WEB_FIXTURES_BROWSER" value="firefox" />
 
// and pointing the new domain to the corresponding directory
$ sudo pico /etc/hosts
127.0.0.1       testmink.local
 
// and creating the virtual host for testing mink
$ sudo pico /etc/apache2/sites-available/default
...
DocumentRoot /home/cordoval/sites-2/project/vendor/behat/mink/tests/Behat/Mink/Driver/web-fixtures
...
<Directory /home/cordoval/sites-2/project/vendor/behat/mink/tests/Behat/Mink/Driver/web-fixtures>
 
// finally restarting apache2
$ sudo service apache2 restart

After running the tests and seeing all the magic, we see an exception at the bottom below:

cordoval@ms:~/sites-2/project/vendor/behat/mink$ phpunit 
PHPUnit 3.5.14 by Sebastian Bergmann.
 
................................................................. 65 / 97 ( 67%)
................................
 
Time: 01:02, Memory: 16.50Mb
 
OK (97 tests, 406 assertions)
PHP Fatal error:  Exception thrown without a stack frame in Unknown on line 0

It must be a sahi related exception as when shutting down the sahi client we get a clean pass on the other tests:

PHPUnit 3.5.14 by Sebastian Bergmann.
 
.......EEEEEEEEEEEEEEEEE......................................... 65 / 97 ( 67%)
................................
 
Time: 2 seconds, Memory: 17.00Mb
 
There were 17 errors:
FAILURES!
Tests: 97, Assertions: 288, Errors: 17.

There were errors because of the sahi client not being active but no exception.

Goal now is to implement a fail test for finding a checkbox that has been disabled.
So we created a file elements_with_attributes.php under web-fixtures and inserted a new test method “testFindElementWithDisabledAttribute” with the following content (WIP as this runs but we are trying to match a specific use case):

public function testFindElementWithDisabledAttribute()
    {
        $this->getSession()->visit($this->pathTo('/elements_with_attributes.php'));
 
        $page = $this->getSession()->getPage();
 
        $checkbox1 = $page->findField('someid1');
        $checkbox1disabled = $page->findField('someid1disabled');
        $checkbox2 = $page->findField('someid2');
        $checkbox2checked = $page->findField('someid2checked');
 
        $this->assertEquals(1, count($checkbox1));  // there is something it found ?
        $this->assertEquals(1, count($checkbox1disabled));  // there is something it found ?
        $this->assertEquals(1, count($checkbox2));  // there is something it found ?
        $this->assertEquals(1, count($checkbox2checked));  // there is something it found ?
 
        $this->assertFalse($checkbox1->isChecked()); // can it be checked ?
        $this->assertFalse($checkbox1disabled->isChecked()); // can it be checked ?
        $this->assertTrue($checkbox2->isChecked()); // can it be checked ?
        $this->assertTrue($checkbox2checked->isChecked()); // can it be checked ?
 
        // TODO: I propose a method or helper for isEnabled() method on mink
 
    }
 
// and run it
phpunit --verbose --filter testFindElementWithDisabledAttribute tests/Behat/Mink/Driver/GoutteDriverTest.php

And here is the output proving that there is a bug here:

cordoval@ms:~/sites-2/project/vendor/behat/mink$ phpunit --verbose --filter testFindElementWithDisabledAttribute tests/Behat/Mink/Driver/GoutteDriverTest.php 
PHPUnit 3.5.14 by Sebastian Bergmann.
 
E
 
Time: 0 seconds, Memory: 9.75Mb
 
There was 1 error:
 
1) Tests\Behat\Mink\Driver\GoutteDriverTest::testFindElementWithDisabledAttribute
Behat\Mink\Exception\ElementNotFoundException: The form field with xpath "(//html/.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][(((./@id = 'someid1' or ./@name = 'someid1') or ./@id = //label[contains(normalize-space(string(.)), 'someid1')]/@for) or ./@placeholder = 'someid1')] | .//label[contains(normalize-space(string(.)), 'someid1')]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')])[1]" was found, but no form element surrounding that field could be foundnot found
 
/home/cordoval/sites-2/project/vendor/behat/mink/src/Behat/Mink/Driver/GoutteDriver.php:535
/home/cordoval/sites-2/project/vendor/behat/mink/src/Behat/Mink/Driver/GoutteDriver.php:278
/home/cordoval/sites-2/project/vendor/behat/mink/src/Behat/Mink/Driver/GoutteDriver.php:354
/home/cordoval/sites-2/project/vendor/behat/mink/src/Behat/Mink/Element/NodeElement.php:154
/home/cordoval/sites-2/project/vendor/behat/mink/tests/Behat/Mink/Driver/GeneralDriverTest.php:222
 
FAILURES!
Tests: 1, Assertions: 4, Errors: 1.

Note: On my project’s tests I get a different error, but it anyhow states that it cannot find the element because it does not exist. The error is somewhat like this:

The form field "myid[property1][property2]" does not exist

If now a test on sahi client is run the output is successful besides the typical exception:

cordoval@ms:~/sites-2/project/vendor/behat/mink$ phpunit --verbose --filter testFindElementWithDisabledAttribute tests/Behat/Mink/Driver/SahiDriverTest.php 
PHPUnit 3.5.14 by Sebastian Bergmann.
 
.
 
Time: 3 seconds, Memory: 8.75Mb
 
OK (1 test, 8 assertions)
PHP Fatal error:  Exception thrown without a stack frame in Unknown on line 0

As post data here we attach the html employed for the test:

// elements_with_attributes.php
<!doctype html>
<head> 
  <meta charset="utf-8" /> 
 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
 
  <title>Test for isCheck on element found</title>
 
</head> 
 
<body> 
 
<input type="checkbox" id="someid1" value="1" />
 
<input type="checkbox" id="someid1disabled" disabled="disabled" value="1" />
 
<input type="checkbox" id="someid2" value="1" />
 
<input type="checkbox" id="someid2checked" value="1" checked="checked"/>
 
</body> 
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *