首页 > 解决方案 > 重用功能文件中的变量(javascript、wdio、黄瓜)

问题描述

我有一个用 javascript 编写的测试设置,使用 cucumber 和 webdriverio。

我在我的功能文件中定义我的测试场景: example1.featureexample2.feature

Mx example1.feature 文件如下所示:

Feature: Submit search term
  As a user who is logged in
  I want to submit a search term
  Because I want to see the search results

Background:
  I am logged in
  And I am on the search view

Scenario outline:
  When I enter and submit the search term <someString>
  Then I can see the results of <someString>

Examples:
  |someString|
  |bananas|

这非常有效,我的变量被 example1.page.js 和 example1.steps.js 拾取

现在我想在我的example2.feature(以及example3.feature,example4.feature,...)中重用来自example1.feature(包括变量some​​String)的这个场景大纲

我的 example2.feature 看起来像这样:

Feature: Navigate to details page 
  As a user who is logged in
  I want to submit a search term
  Because I want to see the details of my search term

Background:
  I am logged in
  And I am on the search view

Scenario outline:
  When I enter and submit the search term <someString>
  Then I can see the results of <someString>
  When I click on the details
  Then I get redirected to the details page

Examples:
  |someString|
  |bananas|

我尝试重用 example1.page.js 中的部分,但这破坏了我的测试。example1 和 example2 都不再运行。当我删除 example2 时,example1 有效。我尝试删除和重命名(someString 和香蕉);这没用。

我想出的解决这个问题的两种方法是将整个部分从示例 1 复制到示例 2(包括定位器和 page.js 中的部分)或将示例 2 包含在示例 1 中(使示例 1 更大)。

但是,由于我也想在 example3 和 example4 等中重用此代码部分,所以我既不喜欢复制所有内容,也不喜欢制作一个巨大的测试用例......

有人能想出一个更好的方法来解决这个问题吗?

标签: javascriptautomated-testscucumber

解决方案


功能文件的格式似乎不正确,这可能会解决问题。Given背景的第一步(将其转换为背景的描述)中缺少一个,outline应该是大写的:Scenario Outline:

希望这可以解决您的问题:

示例1.功能:

Feature: Submit search term
  As a user who is logged in
  I want to submit a search term
  Because I want to see the search results

Background:
  Given I am logged in
  And I am on the search view

Scenario Outline:
  When I enter and submit the search term <someString>
  Then I can see the results of <someString>

Examples:
  |someString|
  |bananas   |

example2.feature

Feature: Navigate to details page 
  As a user who is logged in
  I want to submit a search term
  Because I want to see the details of my search term

Background:
  Given I am logged in
  And I am on the search view

Scenario Outline:
  When I enter and submit the search term <someString>
  Then I can see the results of <someString>
  When I click on the details
  Then I get redirected to the details page

Examples:
  |someString|
  |bananas   |

推荐阅读