1

我真的不明白为什么 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=/
4

2 回答 2

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

于 2015-06-08T02:30:05.520 回答
1

我偶然发现了同样的问题。事实证明,也curl example.com没有返回任何东西。只是空白。这意味着没有一行 HTML Behat/Mink 可以解析。

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

于 2018-09-18T21:10:41.243 回答