Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Muting Noisy XHR Logs in Cypress
Sam E. Lawrence
Sam E. Lawrence

Posted on • Edited on

     

Muting Noisy XHR Logs in Cypress

(This guide has been updated multiple times, especially since the release of Cypress 10)

For a while now, our Cypress runner has been quite noisy, generating lots of XHR requests in the log as our tests run. A bug was introduced into Cypress in the last few versions that has made it quite difficult to mute these. Fortunately, I found a solution thatSimen Brekken helpfully postedhere and I felt like I'd share it with the Dev audience in case it was helpful to someone else.

For the fix, we add a flag to ourcypress.config.ts to allow us to enable or disable rich logging as needed.

e2e:{hideXHRInCommandLog:true}
Enter fullscreen modeExit fullscreen mode

In our/cypress/support/e2e.ts file, we have:

// Hide fetch/XHR requests from command logif(Cypress.config("hideXHRInCommandLog")){constapp=window.top;if(app&&!app.document.head.querySelector("[data-hide-command-log-request]")){conststyle=app.document.createElement("style");style.innerHTML=".command-name-request, .command-name-xhr { display: none }";style.setAttribute("data-hide-command-log-request","");app.document.head.appendChild(style);}}
Enter fullscreen modeExit fullscreen mode

And then lastly, in/support/cypress.d.ts, we add:

declarenamespaceCypress{interfaceResolvedConfigOptions{hideXHRInCommandLog?:boolean;}}
Enter fullscreen modeExit fullscreen mode

This solution uses CSS to prevent the XHR requests from being picked up in the DOM and thereby reported to the Cypress runner. It's not the most elegant solution, but it'll work for now until we get a better fix from Cypress. Thanks, Simen!

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jprealini profile image
Juan Pablo
Originally a .Net developer for 10+ years, I've turned to the dark side since 2017, becoming an SDET
  • Location
    Buenos Aires, Argentina
  • Work
    Sr. Software Engineer in Testing
  • Joined

Hi! I found this article very helpful, but for some reason I faced an issue when adding the code to the support/index.js file... Cypress was telling me that there was no override for that option... I figured that probably I needed to change something in the Cypress core code to allow it to identify the custom key in the cypress.json file. So what I did was adding the hideXHR key to my "env": { } section, and then in the code instead of

if (Cypress.config('hideXHR'))
Enter fullscreen modeExit fullscreen mode

I put

if (Cypress.env('hideXHR'))
Enter fullscreen modeExit fullscreen mode

And it worked as expected

Thanks!

CollapseExpand
 
samelawrence profile image
Sam E. Lawrence
Hi, I'm Sam and I'm an advocate for quality software. I'm a Cypress Ambassador and I work as the QA Lead at Pointivo where we're digitizing real-world infrastructure for analysis and maintenance.
  • Location
    ATL
  • Education
    Georgia Tech
  • Pronouns
    He/him/his
  • Work
    QA Lead @ Pointivo
  • Joined

If you're placing the config inside yourenv object, then yes that's necessary. I keep my config flag outside of theenv object, in the maine2e config object. I believe it's optional where you keep it, as long as both pieces of code match.

CollapseExpand
 
matonski profile image
Anton de la Fuente
  • Joined

Thank you! I used this verbatim at my job and it worked.

CollapseExpand
 
jonathansth profile image
Jonathan Thomas
  • Joined

Really cool wait to handle this. What about seeing intercepts?

CollapseExpand
 
stuaylward profile image
Stu
  • Joined
• Edited on• Edited

This is how I handle different types. Choose between show only types or hide only types. This does mean you need to dig in and look at thecommmand-name-X class name. Just list the X name in the environment variable (space seperated) and it'll show only those types, or hide only those types.

// Show or hide command log types via environment variablesif (Cypress.env('showCommandLogType')) {    const app = window.top    if (app && !app.document.head.querySelector('[data-hide-command-log-request]')) {        const style = app.document.createElement('style')        style.innerHTML = `.command${Cypress.env('showCommandLogType')            .split(' ')            .map(type => `:not(.command-name-${type})`)            .join('')} { display: none }`        style.setAttribute('data-hide-command-log-request', '')        app.document.head.appendChild(style)    }} else if (Cypress.env('hideCommandLogType')) {    const app = window.top    if (app && !app.document.head.querySelector('[data-hide-command-log-request]')) {        const style = app.document.createElement('style')        style.innerHTML = `${Cypress.env('hideCommandLogType')            .split(' ')            .map(type => `.command-name-${type},`)            .join('')            .slice(0, -1)} { display: none }`        style.setAttribute('data-hide-command-log-request', '')        app.document.head.appendChild(style)    }}
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I'm Sam and I'm an advocate for quality software. I'm a Cypress Ambassador and I work as the QA Lead at Pointivo where we're digitizing real-world infrastructure for analysis and maintenance.
  • Location
    ATL
  • Education
    Georgia Tech
  • Pronouns
    He/him/his
  • Work
    QA Lead @ Pointivo
  • Joined

More fromSam E. Lawrence

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp