From 2b35e9640ef874249bf445f0a4c88b8ed26ee653 Mon Sep 17 00:00:00 2001 From: Azuruce Date: Thu, 24 Sep 2020 10:49:34 -0700 Subject: [PATCH] docs(readme): add example to save har conditionally (#41) --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index efbc326..94dce08 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,24 @@ It allows overriding the value, that is specified in the `cypress.json` cy.saveHar({ outDir: './hars' }); ``` +Generate HAR file only for chrome, if it is not interactive run, and if test failed. +```js +beforeEach(() => { + const isInteractive = Cypress.config('isInteractive'); + const isChrome = Cypress.browser.name === 'chrome'; + if (!isInteractive && isChrome) { + cy.recordHar(); + } +}); +afterEach(() => { + const { state } = this.currentTest; + const isInteractive = Cypress.config('isInteractive'); + const isChrome = Cypress.browser.name === 'chrome'; + if (!isInteractive && isChrome && state !== 'passed') { + cy.saveHar(); + } +}); +``` If you want to change the path to the files, you can specify it by setting the `hars_folder` environment variable.