没有元素匹配 XPath "//html" (Behat\Mink\Exception\DriverException)

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

我真的想不通为什么 Mink / Behat 找不到 "Login" 文本。

我的场景很简单:

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"

并且输出看起来是正确的:

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)

这里有趣的是,看起来 Mink 找不到 HTML 节点,即使它在响应中也是如此。

这是我的 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: ~

如有任何想法,我们将不胜感激。

更新:

如果我卷曲 URL,则响应内容类型为 application/json。我认为这可能是问题所在。

$ 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=/

更新:

好的,将 fos_rest_bundle 优先级更改为 html,json 现在的响应是 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 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=/

最终的解决方案是通过添加回退 / 规则来确保 format_listener 具有正确的规则。这意味着所有非 /api 请求都将是 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

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

我偶然发现了同样的问题。结果是 curl example.com 也完全没有返回任何内容。只是空白。这意味着没有一行 HTML Behat/Mink 可以解析。

我通过修复我的安装来修复它并使该页面完全可以访问。