CucumberJS场景概述,创建步骤代码

时间:2018-04-27 23:39:31

标签: angular typescript protractor cucumber

我有以下功能文件:

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
             

我想弄清楚如何创建步骤文件。每当我运行量角器时,它会给我自动生成的代码;但是,每个场景给我一个。例如,它希望我为每个案例编写六个Given步骤。如何使用传递给函数的变量创建两个Given步骤? Then步骤也是如此。

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

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();
});

1 个答案:

答案 0 :(得分:0)

所以我实际上有两个错误。我在功能文件中COLOR_THREE之后没有管道,我需要在步骤文件中使用{variable_here}。这是更新的功能文件和代码:

功能文件:

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       |
         

步骤文件:

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();
});

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