Skip to content

Commit

Permalink
Adding blocking hosts example (#120)
Browse files Browse the repository at this point in the history
* Update README.md

* Create BlockHosts.md

* Update README.md

* Update BlockHosts.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update BlockHosts.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
dmundra and pre-commit-ci[bot] authored Nov 27, 2024
1 parent d0dc283 commit 2090bf9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ For now, the examples exist in markdown files that you can read and copy into yo
- [SSO Redirects and Basic Auth](examples/HandleSSORedirectAndBasicAuth.md) -
- [Log Into Drupal and Create a Node](examples/LogIntoDrupalAndCreateANode.md) -
- [Lighthouse Audits](examples/LighthouseAudits.md) -
- [Block Hosts](examples/BlockHosts.md) -
43 changes: 43 additions & 0 deletions docs/examples/BlockHosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Blocking Hosts in Tests

The most common host you want to block in the tests in Google Analytics. To do that use the [`blockHosts`](https://docs.cypress.io/app/references/configuration#blockHosts) configuration option. Here is an example of a list of hosts we block including Google Analytics:

```javascript
blockHosts: [
'*doubleclick.net',
'*google-analytics.com',
'*tealiumiq.com',
'*quantummetric.com',
'g*qualtrics.com',
'*nr-data.net',
'*youtube.com',
'*tiqcdn.com',
'*touchpoints.app.cloud.gov',
'*script.crazyegg.com',
'*googletagmanager.com',
'*googleapis.com'
],
```

While the hosts are blocked you still might see fetch/xhr requests in the Cypress logs/screenshots/videos. To hide those add the following to the `cypress/support/e2e.js` file:

```javascript
// Hide fetch/XHR requests
const cypressLogOriginal = Cypress.log
const cypressLogDomainsHidden = ['https://www.google-analytics.com']
Cypress.log = function (opts, ...other) {
const logFetchIs = ['fetch'].includes(opts.displayName)
const logFetchDomainMatch =
logFetchIs && cypressLogDomainsHidden.find((d) => opts.url.includes(d))
if (logFetchDomainMatch) return

return cypressLogOriginal(opts, ...other)
}
```

[Credit for the code snippet](https://gist.github.com/simenbrekken/3d2248f9e50c1143bf9dbe02e67f5399?permalink_comment_id=4190114#gistcomment-4190114).

## Useful references

- https://docs.cypress.io/app/references/configuration#blockHosts
- https://gist.github.com/simenbrekken/3d2248f9e50c1143bf9dbe02e67f5399?permalink_comment_id=4190114#gistcomment-4190114

0 comments on commit 2090bf9

Please sign in to comment.