Skip to content

Commit

Permalink
Handle browser / page crash gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
badeball committed Mar 7, 2024
1 parent 145dde3 commit 9558b77
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
65 changes: 65 additions & 0 deletions features/browser_crash.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Feature: browser crash

Background:
Given additional preprocessor configuration
"""
{
"json": {
"enabled": true
}
}
"""

Rule: report generation should fail gracefully in the event of a browser crash

Scenario: Chromium process crash
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given, attach } = require("@badeball/cypress-cucumber-preprocessor");
Given("a step", function() {
new Cypress.Promise(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Browser.crash",
});
});
});
"""
When I run cypress with "--browser chromium"
Then it fails
And the output should contain
"""
Due to browser crash, no reports are created for cypress/e2e/a.feature.
"""
And the JSON report shouldn't contain any specs

Scenario: Renderer process crash
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given, attach } = require("@badeball/cypress-cucumber-preprocessor");
Given("a step", function() {
new Cypress.Promise(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Page.crash",
});
});
});
"""
When I run cypress with "--browser chromium"
Then it fails
And the output should contain
"""
Due to browser crash, no reports are created for cypress/e2e/a.feature.
"""
And the JSON report shouldn't contain any specs
34 changes: 34 additions & 0 deletions lib/plugin-event-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,40 @@ export async function afterSpecHandler(
return;
}

/**
* This pretty much can't happen and the check is merely to satisfy TypeScript in the next block.
*/
switch (state.state) {
case "uninitialized":
case "after-run":
throw createError("Unexpected state in afterSpecHandler: " + state.state);
}

const browserCrashExprCol = [
/We detected that the .+ process just crashed/,
/We detected that the .+ Renderer process just crashed/,
];

const error = results.error;

if (error != null && browserCrashExprCol.some((expr) => expr.test(error))) {
console.log(
chalk.yellow(
` Due to browser crash, no reports are created for ${spec.relative}.`
)
);

state = {
state: "after-spec",
pretty: state.pretty,
messages: {
accumulation: state.messages.accumulation,
},
};

return;
}

switch (state.state) {
case "test-finished": // This is the normal case.
case "before-spec": // This can happen if a spec doesn't contain any tests.
Expand Down

0 comments on commit 9558b77

Please sign in to comment.