没有元素匹配XPath" // html" (贝哈特\水貂\异常\ DriverException)

时间:2015-06-07 01:17:04

标签: symfony xpath behat mink symfony-2.7

我真的无法弄清楚为什么Mink / Behat无法找到"登录"文本。

我的情景很简单:

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,则响应Content-Type是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=/

2 个答案:

答案 0 :(得分:1)

最后的解决方案是通过添加后备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

答案 1 :(得分:1)

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

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