首页 > 解决方案 > 当给定状态有多个验证时如何编写 Gherkin 场景

问题描述

我是黄瓜的新手——小黄瓜世界。我正在尝试编写一个功能文件。我计划测试一个静态网页,其中包含我需要验证的大量静态内容(比如说 100 个部分)。理想情况下,如果我根据对小黄瓜的理解开始写作,它看起来如下所示:

场景:测试参与页面内容

鉴于我是余额 > 10000 的用户

当我登陆我的订婚页面时

然后section1应该正确显示

并且 section2 应该正确显示

并且 section3 应该正确显示

并且 section4 应该正确显示

- 等等 - - -

并且 section100 应该正确显示。

这绝对看起来很难看。我怎样才能把它分解成多个场景。进入该页面后,我正在测试所有内容。我没有在页面上进行任何活动。登陆后,我只需要验证所有部分。

提前致谢

标签: cucumbercucumber-jvmgherkincucumberjs

解决方案


有很多方法可以做到这一点,但假设这些部分共享元素和断言,场景大纲可能是您的最佳选择。

Scenario Outline: Verify the display of all sections on the Engagement Page
Given I am a user with >10000 balance
When I land of Engagement Page
Then the header of <section> should read "<headerText>"
And the icon of <section> should be displayed
And the body of <section> should read "<bodyText>"

Examples:
  | section   | headerText         | bodyText        |
  | Section 1 | This is Header #1  | This is Body #1 |
  | Section 2 | Header Text of #2  | Body Text of #2 |
... etc

如果这些部分在结构上是独一无二的,那么您将面临为每个部分编写一个场景(或更多,取决于您的风格 - 我个人不喜欢在一个案例中拥有多个断言):

比如说,section1有一个标题、图标和正文,你最终会遇到以下三种情况:

Scenario: Verify display of header in Section 1
Given I am a user with >10000 balance
When I land of Engagement Page
Then the header of Section 1 should read "text"

Scenario: Verify display of icon in Section 1
Given I am a user with >10000 balance
When I land of Engagement Page
Then the icon of Section 1 should be displayed

Scenario: Verify display of body text in Section 1
Given I am a user with >10000 balance
When I land of Engagement Page
Then the body of Section 1 should read "text"

如果您对每个测试的多个断言感到满意:

Scenario: Verify display of Section 1
Given I am a user with >10000 balance
When I land of Engagement Page
Then the header of Section 1 should read "text"
And the icon of Section 1 should be displayed
And the body of Section 1 should read "text"

推荐阅读