如何在 Behat 中检查多个页面的 header 状态码?

How to check header status code for multiple pages in Behat?

假设我在数据库中有一些页面 slug,我想与 Behat 检查页面是否返回 header 状态代码 200。

这意味着我想要一个将检查多个页面的测试(功能),但是我正在为如何做到这一点而苦苦挣扎。

现在我正在使用这样的东西:

Given I am on any page, I should get header status code 200

启动一个函数,每个逻辑都在该函数中。

但理想情况下,我想要这样的东西:

Given I go through all pages
When I check the header status for each page
Then I should get header status code 200

有办法吗?

注意:我使用 FeatureContext class 并尝试存储例如私有变量中的所有页面,然后尝试在功能的下一步中访问它,但这似乎不起作用。

对于单页,是这样的。

示例场景:

  Scenario: Request and Response for single page
    Given I am on "/homepage"
    When I send a GET request to "/cars/list_all"
    And the response status code should be 200

示例上下文文件: 您需要在下面添加方法。

class FeatureContext extends MinkContext implements KernelAwareInterface
{
    /**
     * @When /^I send a ([^"]*) request to "([^"]*)"$/
     */
    public function iSendARequestTo($method, $url)
    {
        $client = $this->getSession()->getDriver()->getClient();
        $client->request($method, $url);
    }
}

水貂语境: 这已经存在所以你不会碰它。

class MinkContext extends RawMinkContext implements TranslatedContextInterface
{
    /**
     * Checks, that current page response status is equal to specified.
     *
     * @Then /^the response status code should be (?P<code>\d+)$/
     */
    public function assertResponseStatus($code)
    {
        $this->assertSession()->statusCodeEquals($code);
    }
}

对于多个页面,您将使用Scenario Outlines

  Scenario Outline: Request and Response for multiple pages
    Given I am on "/homepage"
    When I send a <method> request to <end-point>
    And the response status code should be <response-code>

  Examples:
    | method | end-point     | response-code |
    | POST   | /api/page-one | 200           |
    | PUT    | /api/page-two | 200           |