首页 > 解决方案 > 运行黄瓜测试改变变量?

问题描述

请允许我向您展示我在学校平台上进行的测试中的一小段代码:

  Background:
    I Enter the school page
    In Schools I navigate to:
      | Hierarchical level | Action      | Value          |
      | District           | expand      | District-A     |
      | School             | right click | Saint John's   |
    And Choose "Go to" on the Popup Menu
    And Zoom In To See More Options in Control Bar

所以你看,我使用这个背景在网站的一棵小树中导航。我的问题是,我是否可以代替“District-A”和“Saint John's”有两个变量,例如“district”和“school”,所以当我在命令行上运行测试时,我可以有一些东西就像一个额外的参数说:我希望这一轮测试将我的“地区”作为“地区-B”,并让“学校”变量成为其中一所学校。首先,这可能吗?其次,如果是的话,有人可以给我一个关于如何做到这一点的快速想法吗?

提前非常感谢你-

标签: javaseleniumcucumber

解决方案


在 cucumber 中,您编写场景来描述您正在尝试做的事情,并可能解释为什么它很重要。您不应该编写解释您如何做某事的步骤。任何有关单击、扩展等的步骤最终都会导致您遇到的问题。

所以第一件事是描述这个背景试图实现什么,以及为什么它很重要。在考虑如何将其下推到您的步骤定义和辅助方法中。这样做之后,你可能会得到类似的东西

Scenario: View a school
  Given there is a school
  When I view the school
  Then I should see the school

现在这似乎很简单,但这就是重点。您应该使您的网站易于使用。因此,您需要做的第一件事就是能够看到一所学校。在你有了这个之后,你可能想与很多学校打交道,并考虑找到一所特定的学校。然后你可能会得到类似的东西

Feature: Search for a school
  We want to be able to find a particular school

Scenario: Find a school
  Given there are lots of schools with one searchable
  When I search for the school
  Then I should see search results with one school

您可能会对地区采取类似的措施

Feature: Districts
  Schools are organised by districts. We would like to view all the schools in a district

  Scenario: View district
    Given there is a district
    When I view the district 
    Then I should see the district

  Scenario: See schools in a district
    Given there is a district
    And the district has some schools
    When I view the district
    Then I should see some schools

等等 ...

请注意,这些场景都没有关于页面、点击等的任何内容。这都是什么和为什么不怎么做。还要注意一切都是多么简单。


推荐阅读