假设我在数据库中有几个页面,我想检查一下这些页面是否返回页头状态码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类,我尝试存储例如所有页面在一个私有变量,然后试图访问它在功能的下一步,但这似乎不工作。
对于单页,是这样做的。
示例场景:
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);
}
}
MinkContext:
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 |