Developing step code for CucumberJS Scenario Outlines

In my feature file, I have the following scenario outlined for testing colors:

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
             

I am struggling to create an efficient step file. Every time I run protractor, it generates code for each scenario separately. How can I simplify this process by just creating two Given steps that handle variables passed to the function? The same applies to the Then step.

Even with my TypeScript attempts below, the automation framework still requires me to write individual steps for all variations, resulting in failed tests.

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

Answer №1

After reviewing my code, I realized that there were two errors that needed to be fixed. First, I forgot to include the pipes after COLOR_THREE in the feature file. Second, I needed to use {variable_here} instead in the step file. Below is the updated feature file and code:

This is how the Feature file looks now:

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       |
         

Here is the updated Step file:

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

By updating the code, you can now retrieve various values from the table in the feature file within the variables. The tests are now passing as expected. Hopefully, this information will be useful to someone!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unlocking new perspectives with a click

Currently exploring Angular development, I have encountered a question here but couldn't find the solution I was looking for. I am seeking suggestions and ideas on how to approach this issue. Essentially, my HTML includes buttons like the ones shown ...

What could be causing the undefined value in my Many-to-Many relationship field?

Currently, I am in the process of setting up a follower/following system. However, as I attempt to add a new user to the following list, I encounter an error stating Cannot read property 'push' of undefined. This issue results in the creation of ...

What could be causing the error in Angular Universal Server Side Rendering after deployment on Firebase Hosting?

Currently immersed in Angular development utilizing third-party libraries such as Angular CLI/Angular Universal, following the guidelines laid out here. Also, integrating Firebase hosting and real-time database. The application works flawlessly on my local ...

Angular2 route-driven themes

Exploring Different Themes for Two Routes: /books and /paintings Seeking a Solution to Include Specific Stylesheet Links in index.html For the /books route, I wish to include: <link rel="stylesheet" href="/assets/css/reading-theme.css" /> And for ...

Discovering the element on which the value has decreased - a helpful guide

I am attempting to create a drag and drop feature for simple calculations. Users can drag the UP value and drop it on either BH or OT. The issue I'm facing is that I cannot identify the ID where the user dropped, and the onDrop() function is not work ...

Adding an icon to the contents of a specific column in Angular material

Struggling to figure out how to incorporate an icon into the data in the Status column using Angular material. Here is the markup of my page: <table mat-table [dataSource]="dataSource"> <ng-container *ngFor="let ...

The issue arises where Google Maps Platform rejects your request due to an invalid request with an unexpected parameter "amp%3Bkey"

Dealing with angular error Your request has been rejected by Google Maps Platform. It was flagged as invalid due to an unexpected parameter 'amp%3Bkey'. Here's the error displayed in the console: GET https://www.google.com/maps/embe ...

Encountering a TypeError while working with Next.js 14 and MongoDB: The error "res.status is not a function"

Currently working on a Next.js project that involves MongoDB integration. I am using the app router to test API calls with the code below, and surprisingly, I am receiving a response from the database. import { NextApiRequest, NextApiResponse, NextApiHandl ...

Troubleshooting problems encountered in Nest.js due to modifications made within a service.ts file

I'm currently working on a Nest.js project and here is the content of the automobile.service.ts file: import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Car } from './enti ...

Encountering the error message "Uncaught Promise (SyntaxError): Unexpected end of JSON input"

Below is the code snippet I am using: const userIds: string[] = [ // Squall '226618912320520192', // Tofu '249855890381996032', // Alex '343201768668266496', // Jeremy '75468123623614066 ...

Simulated custom error response object jasmine-Angular

Within the HTTP service, there is a single function designed to manage all HTTP errors, taking custom objects and displaying errors based on specific conditions. This is an example of the service function: get(path: string, params: HttpParams = new Http ...

Is it possible to link multiple references to a single Element/Node?

I am working on a function component that needs to pass the incoming ref from the parent to a div it is rendering. Additionally, I want to create and assign a separate ref inside the component to the same div. However, due to an element only accepting one ...

Tips for effectively setting up and structuring various entry points in an Angular application

I am currently working on an app that has 3 different entry points. For the Client App: This is a single route app. Corporate App: The Corporate App consists of a list of links to various customer apps. I would like this app to smoothly transition betw ...

What sets apart the usage of "protractor protractor.conf.js" from "npm run e2e" command?

Can you explain the difference between these two commands when running them in an Angular CLI project terminal? 'protractor protractor.conf.js' --> Launches the baseurl specified in protractor.conf.js instead of localhost 'npm run e2e&a ...

Discover the initial item in Observable that meets a certain criteria

I am trying to retrieve the first item of type avatar from payload.result within an observable: let result = this.fileService.getAvatarByUserId(2).pipe( map((payload: Payload<FileModel[]>) => payload.result), first((result: FileModel[]) => ...

Contrast between employing typeof for a type parameter in a generic function and not using it

Can you explain the difference between using InstanceType<typeof UserManager> and InstanceType<UserManager> I'm trying to understand TypeScript better. I noticed in TS' typeof documentation that SomeGeneric<typeof UserManager> ...

Angular: Observing DOM changes through observables

I have been exploring ways to track changes in the DOM of my angular component. Utilizing observables to capture those changes into a typescript variable, although uncertain if that's the right approach. Here is how I've set it up: app.componen ...

The Angular directive ng-if does not function properly when trying to evaluate if array[0] is equal to the string value 'Value'

In my code, I want to ensure that the icon is only visible if the value at array index 0 is equal to 'Value': HTML <ion-icon *ngIf="allFamily[0] === 'Value'" class="checkas" name="checkmark"></ion-icon> TS allFamily = [ ...

Tsyringe - Utilizing Dependency Injection with Multiple Constructors

Hey there, how's everyone doing today? I'm venturing into something new and different, stepping slightly away from the usual concept but aiming to accomplish my goal in a more refined manner. Currently, I am utilizing a repository pattern and l ...

I attempted to unsubscribe from an observable in Angular, but I encountered an error stating that the unsubscribe function does not exist

Here is the code snippet from a components.ts file in an Angular project. I encountered the following error during compilation: ERROR merge/merge.component.ts:75:12 - error TS2551: Property 'unsubscribe' does not exist on type 'Observable& ...