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

feat(cypress): command log steps rework #1205

Merged
merged 6 commits into from
Dec 5, 2024

Conversation

delatrie
Copy link
Collaborator

@delatrie delatrie commented Dec 3, 2024

Context

Allure Cypress automatically converts commands into Allure steps. It does so by listening command:start and command:end,

The PR reworks this mechanism to be based on the command log instead. It aligns with our general attitude to represent things as they are reported by the framework itself: the command log is the main reporting mechanism that Cypress provides to users in interactive mode.

Note

The Cypress command log supports events, e.g., the fetch events, which allow tracking HTTP requests made with the fetch API. Events like this are not the subject of this PR. The implementation currently doesn't create steps from them. We may support such events in the future.

Features

Child commands and assertions

Previously, Allure Cypress didn't include child commands, e.g., assertions. Now, it includes them as substeps of their parent command.

Example
it("assertions", () => {
  cy.wrap(1 + 1)
    .should("eq", 2)
    .and((v) => {
      expect(v).lessThan(3);
      return v + 1;
    })
    .and("eq", 3);
});
The Comman log

image

Allure Report before image
Allure Report after image

Properties of command log entries

Previously, Allure Cypress used to create step parameters from the attributes.args list of a command object. Now it uses the attributes.consoleProps function of a command log entry. This is the function Cypress uses to print the properties in the console when a user clicks on an entry in interactive mode.

The main difference is that consoleProps provides key-value pairs instead of a list of values, which allows as to use the actual names instead of syntetic ones. Additionally, many Cypress commands include extra useful data in such props.

Example
it("properties", () => {
  cy.visit("/");
  cy.get("input[type=text]")
    .should("have.attr", "placeholder")
    .and("eq", "Search without being tracked");
});
The Comman log and the console image image
Allure Report before image
Allure Report after image

Custom command log entries

Cypress allow users to add extra entries to the command log with Cypress.log. Allure Cypress now correctly converts them into steps. Child entries (type: "child") are included as substeps of the nearest running parent step (if any).

Example
it("custom log", () => {
  Cypress.log({ name: "start", message: "asserting", type: "parent" });
  cy.wrap(1 + 1).should((v) => {
    expect(v).eq(2);
    Cypress.log({ name: "done", message: "asserting", type: "child" });
  });
});
The Comman log image
Allure Report image

Hidden commands and log entries

Some commands take the { log: true } option. Cypress precludes such commands from the command log. Allure Cypress doesn't convert them to steps as well.

Example
it("hidden logs", () => {
  cy.wrap(1 + 1, { log: false }).should("eq", 2);
  cy.wrap(1 + 2, { log: false }).should("eq", 3);
});
The Comman log image
Allure Report image

Command log groups

Command log entries can be grouped by providing the { groupStart: true } option to Cypress.log. Allure Cypress converts such entries to steps. Every step created until log.endGroup is called becomes a substep. This applies to steps from the command log as well as to steps created with allure.step.

Example
it("log group", () => {
  const group = Cypress.log({
    name: "asserting",
    message: "the result of 1 + 1",
    type: "parent",
    groupStart: true,
  });
  cy.wrap(1 + 1).should("eq", 2).then(() => {
    group.endGroup();
  });
});
The Comman log image
Allure Report image

Note

Since @badeball/cypress-cucumber-preprocessor uses the Cypress.log API to group command log entries of a cucumber step, such grouping is automatically preserved by Allure Cypress. This wasn't the case earlier (see #1059).

Screenshots

Allure Cypress now uses the attachment step feature to report screenshots created with cy.screenshot or created by Cypress automatically when a test fails.

Example
it("screenshot", () => {
  cy.visit("/");
  cy.get(".is-not-mobile-device")
    .screenshot()
    .find("input[type=text]")
    .screenshot("the input");
});
Allure Report before image
Allure Report after image

Other changes

  • serialize (in allure-js-commons/sdk/utils) now supports a new replacer option. A value must be a function with the following signature (this: unknown, key: string, value: unknown) => unknown. If provided, it's used by serialize to pre-process object property values.
  • Allure Cypress now works better with other listeners of Cypress.on("fail", ...), which are created by a user or a 3rd party plugin. The exception is throws only if there are no subsequent listeners. Otherwise, it passes the responsibility of throwing to the next listener.

Checklist

Closes #1059.

@delatrie delatrie added the type:new feature New feature or request label Dec 3, 2024
@github-actions github-actions bot added the theme:api Javascript API related issue label Dec 3, 2024
@delatrie delatrie changed the title Cypress command log steps rework feat(cypress): command log steps rework Dec 3, 2024
@delatrie delatrie marked this pull request as ready for review December 3, 2024 14:41
packages/allure-cypress/src/browser/utils.ts Show resolved Hide resolved
packages/allure-cypress/src/browser/utils.ts Outdated Show resolved Hide resolved
packages/allure-cypress/src/browser/patching.ts Outdated Show resolved Hide resolved
packages/allure-cypress/src/browser/patching.ts Outdated Show resolved Hide resolved
@delatrie delatrie merged commit c66d87d into main Dec 5, 2024
12 checks passed
@delatrie delatrie deleted the cypress-command-log-steps-rework branch December 5, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme:api Javascript API related issue type:new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cypress + Cucumber] Execution commands are not grouped into Given-When-Then steps
2 participants