Skip to content

API req & res in mochawesome (not entirely redundant) #169

@aubincc

Description

@aubincc

Pretty similar to #165 I guess, but this is a request for advice.

I am using nasty functions in order to store and reuse data between requests.

In order to compensate with the fact that the logs give only the content of the it(), I added a conditional screenshot upon failure of the test. (see code and screenshot below and maybe this helps @kivysyar with his own problem)

I made it conditional because otherwise it requires more disk space and time to execute, but the downside is, I only have the content of the failing request, not that of the couple of previous requests.

I would love to replace the content of the it() with the actual request and the actual response, just like it shows in the html.

My question is: should I look into modifying my code, cy-api or the mochasome reporter instead?


// in the test file
afterEach(() => {
  cy.takeScreenshotIfAutomatedTests();
});
// my custom command
import addContext from "mochawesome/addContext";

Cypress.Commands.add("takeScreenshotIfAutomatedTests", (capture) => {
  let screenshot;
  if (Cypress.env("CI") && Cypress.state().ctx.currentTest.state === "failed") {
    cy.screenshot("screenshot", {
      capture: capture, // Captures "fullpage" if undefined
      overwrite: true,
      onAfterScreenshot($el, props) {
        screenshot = props.path;
      },
    }).then(() => {
      cy.once("test:after:run", (test) => {
        addContext({ test }, screenshot);
      });
    });
  }
});

image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions