首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CucumberJS场景大纲,创建步骤代码

CucumberJS场景大纲,创建步骤代码
EN

Stack Overflow用户
提问于 2018-04-27 23:39:30
回答 1查看 1.2K关注 0票数 3

我有以下特性文件:

代码语言:javascript
复制
Feature: Color feature

    @test
    Scenario Outline: Test color
        Given the first color is <COLOR_ONE>
        And the second color is <COLOR_TWO>
        When the user loads page
        Then the <COLOR_THREE> is displayed

        Examples:
            | COLOR_ONE | COLOR_TWO | COLOR_THREE
            | red       | white     | pink
            | blue      | black     | black
            | green     | purple    | white

我正在努力弄清楚如何创建step文件。每当我运行量角器时,它都会给我自动生成的代码;然而,它为每个场景提供了一个代码。例如,它要求我为每一种情况编写六个Given步骤。如何使用传递给函数的变量创建两个Given步骤?Then步骤也是一样的。

我尝试了以下(使用类型记录),但它仍然希望我创建所有不同的步骤用例,当然,下面的例子都没有通过。

代码语言:javascript
复制
import { browser, element, by } from 'protractor';
const { Given, When, Then, defineSupportCode } = require('cucumber');

defineSupportCode(function ({ setDefaultTimeout }) {
    setDefaultTimeout(120 * 1000);
});

Given(/^ the first color is "([^"]*)" $/, (color, next) => {
    next();
});

Given(/^ the second color is "([^"]*)" $/, (color, next) => {
    next();
});

When(/^ the user loads page $/, (next) => {
    next();
});

Then(/^ the "([^"]*)" is displayed $/, (color, next) => {
    next();
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-28 00:13:45

所以我犯了两个错误。我在特性文件中没有COLOR_THREE之后的管道,我需要在step文件中使用{variable_here}。下面是更新的特性文件和代码:

特征文件:

代码语言:javascript
复制
Feature: Color feature

    @test
    Scenario Outline: Test color
        Given the first color is <COLOR_ONE>
        And the second color is <COLOR_TWO>
        When the user loads page
        Then the <COLOR_THREE> is displayed

        Examples:
        | COLOR_ONE | COLOR_TWO | COLOR_THREE |
        | red       | white     | pink        |
        | blue      | black     | black       |
        | green     | purple    | white       |

步骤文件:

代码语言:javascript
复制
import { browser, element, by } from 'protractor';
const { Given, When, Then, defineSupportCode } = require('cucumber');

defineSupportCode(function ({ setDefaultTimeout }) {
    setDefaultTimeout(120 * 1000);
});

Given('the first color is {color}', (color, next) => {
    next();
});

Given('the second color is {color}', (color, next) => {
    next();
});

When('the user loads page', (next) => {
    next();
});

Then('the {color} is displayed', (color, next) => {
    next();
});

现在,您可以从变量内的特性文件中的表中获得各种值。测试通过了(当然,在上面的例子中没有测试任何东西)。希望它能帮到别人!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50071490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档