allure-js icon indicating copy to clipboard operation
allure-js copied to clipboard

[AllureJs Cucumber] Can't run this.attach inside axios interceptor that initialized in Before hooks in more than one scenario

Open rudyaditya opened this issue 2 years ago • 2 comments

Describe the bug

I created a API test automation project using Axios, cucumber js and allurejs-cucumber. Here is the repository https://github.com/rudyaditya/cucumberAllureSample I want to include API logs in the allure report, so inject the axios interceptor and put it inside the Before hook

Before(async function() {
  //setup dotenv
  dotenv.config();
  // perform some shared setup
  axios.interceptors.request.use(async (request) => {
    await this.attach(JSON.stringify(request, null, 2), ContentType.JSON);
    return request;
  });
  axios.interceptors.response.use(
    async (response) => {
      await this.attach(
        `Status: ${JSON.stringify(response.status, null, 2)} ${JSON.stringify(
          response.statusText,
          null,
          2
        )}\n${JSON.stringify(response.data, null, 2)}`,
        ContentType.JSON
      );
      return response;
    },
    async (error) => {
      if (error.response) {
        await this.attach(
          JSON.stringify(error.response, null, 2),
          ContentType.JSON
        );
      } else {
        await this.attach(JSON.stringify(error, null, 2), ContentType.JSON);
      }
      return Promise.reject(error);
    }
  );
});

Everything looks normal until I add the second scenario inside the feature file

@sample
Feature: Sample feature for Cucumber API test

    Scenario: Get user list
        When I request to get users list
        Then I should receive a 200 status code

    Scenario: Get user list 2
        When I request to get users list
        Then I should receive a 200 status code

I always these error every time I run the test, this doesn't happen when I use the same approach in allurejs-mocha

Error: Cannot attach when a step/hook is not running. Ensure your step/hook waits for the attach to finish.
           at AttachmentManager.onAttachment (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:67:17)
           at AttachmentManager.createStringAttachment (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/attachment_manager/index.ts:152:10)
           at AttachmentManager.create (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/attachment_manager/index.ts:92:14)
           at file:///Users/rudy/Workspace/apollo-test/test/cucumber/step_definitions/api/setup.ts:52:12
           at processTicksAndRejections (node:internal/process/task_queues:95:5)
           at async World.<anonymous> (file:///Users/rudy/Workspace/apollo-test/test/examples/cucumber/step_definitions/api/sampleApiRequestSteps.ts:9:3)
           at async wrapPromiseWithTimeout (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/time.ts:55:10)
           at async Object.run (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/user_code_runner.ts:86:16)
           at async Object.run (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/step_runner.ts:50:20)
           at async TestCaseRunner.invokeStep (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:137:12)
           at async TestCaseRunner.runStep (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:334:20)
           at async /Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:240:34
           at async TestCaseRunner.aroundTestStep (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:170:28)
           at async TestCaseRunner.runAttempt (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:218:7)
           at async TestCaseRunner.run (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/test_case_runner.ts:188:29)
           at async Runtime.runTestCase (/Users/rudy/Workspace/apollo-test/node_modules/@cucumber/cucumber/src/runtime/index.ts:89:20)

To Reproduce Steps to reproduce the behavior:

1. clone my repo https://github.com/rudyaditya/cucumberAllureSample
2. npm install
3. npm run api-test:cucumber:sample

Expected behavior The second scenario and scenario after that can be run normally

Screenshots image

Desktop (please complete the following information):

  • OS: MacOS 13.5.1

rudyaditya avatar Sep 07 '23 16:09 rudyaditya

Hello, @rudyaditya! Will investigate the issue soon 🙏

epszaw avatar Sep 07 '23 19:09 epszaw

@epszaw any update?

rudyaditya avatar Nov 27 '23 04:11 rudyaditya

Starting with allure-cucumber 3.0.0, you can add attachments using attachment method, exposed by the commons:

import { attachment } from "allure-js-commons";

// anywhere within the test or fixture context:

await attachment("name", "content", "text/plain");

baev avatar Jul 05 '24 14:07 baev