There is no element matching XPath "//html" (Behat\Mink\Exception\DriverException)

1.8k views Asked by At

I really can't figure out why Mink / Behat cannot find "Login" text.

My scenario is simple enough:

Scenario: Valid Registration
    Given I am on "/register/"
    Then the response status code should be 200
    Then print current URL
    Then show last response
    Then I should see "Login"

And the output looks correct:

Scenario: Valid Registration                  # features/registration.feature:9
  Given I am on "/register/"                  # Behat\MinkExtension\Context\MinkContext::visit()
  Then the response status code should be 200 # Behat\MinkExtension\Context\MinkContext::assertResponseStatus()
  Then print last response                    # Behat\MinkExtension\Context\MinkContext::printLastResponse()
    │ http://127.0.0.1:8000/app_test.php/register/
    │ 
    │ <!DOCTYPE html>
    │ <html>
    │     <head>
    │         <meta charset="UTF-8" />
    │     </head>
    │     <body>
    │         <div>
    │                             <a href="/app_test.php/login">Login</a>
    │                     </div>
    │     </body>
    │ </html>
  Then I should see "Login"                   # Behat\MinkExtension\Context\MinkContext::assertPageContainsText()
    There is no element matching XPath "//html" (Behat\Mink\Exception\DriverException)

The interesting thing here is that it looks like Mink cannot find the HTML node even though it's in the response.

Here's my behat.yml

default:
  suites:
    default:
      contexts:
        - Behat\MinkExtension\Context\MinkContext
        - AppBundle\Behat\WebApiContext
        - AppBundle\Behat\HookContext
        - AppBundle\Behat\UserContext
        - AppBundle\Behat\WorkspaceContext
        - AppBundle\Behat\WorkspaceAccessContext
        - AppBundle\Behat\OrganisationContext
        - AppBundle\Behat\ProjectContext
        - AppBundle\Behat\ProjectAccessContext
        - AppBundle\Behat\TaskContext
        - AppBundle\Behat\ActivityContext
  extensions:
    Behat\WebApiExtension:
        base_url: http://127.0.0.1:8000/app_test.php
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      base_url: http://127.0.0.1:8000/app_test.php
      show_cmd: 'open %s'
      goutte: ~
#      sessions:
#        default:
#          symfony2: ~

Any thoughts would be appreciated.

Update:

If I curl the URL the response Content-Type is application/json. I think this could be the issue.

$ curl http://127.0.0.1:8000/app_test.php/register/ -i
HTTP/1.1 200 OK
Host: 127.0.0.1:8000
Connection: close
X-Powered-By: PHP/5.6.6
Cache-Control: no-cache
Date: Mon, 08 Jun 2015 02:11:28 GMT
Content-Type: application/json
X-Debug-Token: e8497a
Set-Cookie: MOCKSESSID=3a989c1f8f24288959ac3cbac811374f1e9e1b2173ab68915f6c5e9bd766bd96; path=/

Update:

Ok changing the fos_rest_bundle priorities to html, json the response is now Content-Type: text/html

fos_rest:
    disable_csrf_role: ROLE_API
    routing_loader:
        default_format: json,
        include_format: true
    body_listener: true
    param_fetcher_listener: force
    format_listener:
        rules:
            -
                path: '^/'
                priorities: ['html', 'json', 'xml']
                fallback_format: html
                prefer_extension: true
    view:
        formats:
            rss: false
            json: true
            xml: true
        templating_formats:
            html: true
        force_redirects:
            html: false
    serializer:
      serialize_null: true

Curl output:

$ curl http://127.0.0.1:8000/app_test.php/register/ -i
HTTP/1.1 200 OK
Host: 127.0.0.1:8000
Connection: close
X-Powered-By: PHP/5.6.6
Cache-Control: no-cache
Date: Mon, 08 Jun 2015 02:15:44 GMT
Content-Type: text/html; charset=UTF-8
X-Debug-Token: 6827bd
Set-Cookie: MOCKSESSID=806389b88e7a2acf1f1a8d4c7089e7b93e10ed6b28f3035f0367d36b5871807b; path=/
2

There are 2 answers

0
Leevi Graham On BEST ANSWER

The solution in the end was to make sure the format_listener had the correct rules by added a fallback / rule. This will mean all non /api requests will be html.

fos_rest:
    disable_csrf_role: ROLE_API
    routing_loader:
        default_format: json,
        include_format: true
    body_listener: true
    param_fetcher_listener: force
    format_listener:
        rules:
            -
                path: '^/api'
                priorities: ['json', 'html', 'xml']
                fallback_format: json
                prefer_extension: true
            -
                path: '^/'
                priorities: [ 'text/html', '*/*']
                fallback_format: html
                prefer_extension: true
    view:
        formats:
            rss: false
            json: true
            xml: true
        templating_formats:
            html: true
        force_redirects:
            html: false
    serializer:
      serialize_null: true

See: https://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html

0
leymannx On

I stumbled upon the same issue. It then turned out that also curl example.com returned absolutely nothing. Just blank. Meaning that there was not a single line of HTML Behat/Mink could parse through.

I fixed it by fixing my installation and make the page accessible at all.