Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question:How do i get the "Scenario" details? #261

Open
prasadroks opened this issue Dec 27, 2024 · 5 comments
Open

Question:How do i get the "Scenario" details? #261

prasadroks opened this issue Dec 27, 2024 · 5 comments
Labels
question Further information is requested

Comments

@prasadroks
Copy link

I want to fetch the steps that are written in feature file and print before the test starts.

when i tried to implement below code its giving error for me on scenario

// Print all steps of the scenario
const steps = scenario.getSteps();
steps.forEach(step => {
const stepText = step.getText();
const hasDataTable = step.getArgument() instanceof DataTable;
console.log(Step: ${stepText});
console.log(Contains Data Table: ${hasDataTable});
});

image

@prasadroks prasadroks added the question Further information is requested label Dec 27, 2024
@vitalets
Copy link
Owner

Feature files are not loaded inside test workers for performance reason.
Could you share the final goal, why do you want to print steps?

@prasadroks
Copy link
Author

Based on steps, if step has data table then i want to set up the values in some variables before the start of the test.

@vitalets
Copy link
Owner

So, no built-in methods to achieve that for now.
If it's a particular step, maybe you can set up these values inside that step? E.g.:

Given('my step with data table', async function (dataTable) {
  // set up values for step with data table
  // ...
});

Another more tricky option is to read test title and file from $testInfo fixture, then read feature file and get all steps of the scenario:

Before(async ({ $testInfo }) => {
  console.log('Starting test:', $testInfo.title, $testInfo.file);
  // 1. get location of feature file by $testInfo.file
  // 2. read and parse feature file
  // 3. find scenario by $testInfo.title
  // 4. iterate through steps and check for data table
});

@prasadroks
Copy link
Author

how would you iterate through steps ? while iteration we may get the steps text ?
Before(async ({ $testInfo }) => {
console.log('Starting test:', $testInfo.title, $testInfo.file);

// 4. iterate through steps and check for data table
});

@vitalets
Copy link
Owner

how would you iterate through steps ? while iteration we may get the steps text ?

The approach is like here:

import path from 'node:path';
import fs from 'node:fs';
import * as Gherkin from '@cucumber/gherkin';
import * as Messages from '@cucumber/messages';

const uuidFn = Messages.IdGenerator.uuid();
const builder = new Gherkin.AstBuilder(uuidFn);
const matcher = new Gherkin.GherkinClassicTokenMatcher(); // or Gherkin.GherkinInMarkdownTokenMatcher()
const parser = new Gherkin.Parser(builder, matcher);

Before(async ({ $testInfo }) => {
  const featureFile = './features/' + path.basename($testInfo.file).replace('.spec.js', '');
  const featureContent = fs.readFileSync(featureFile, 'utf-8');
  const gherkinDocument = parser.parse(featureContent);
  const pickles = Gherkin.compile(gherkinDocument, featureFile, uuidFn);
  const pickle = pickles.find((p) => p.name === $testInfo.title);

  console.log(pickle?.steps);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants