Created
January 17, 2019 09:53
-
-
Save Yuyz0112/091de9510d75cab379e6da469bdf2842 to your computer and use it in GitHub Desktop.
rrweb + cypress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sessionId = Cypress.env('sessionId'); | |
const appFrame = window.parent.document.querySelectorAll('iframe')[0]; | |
Cypress.on('window:load', () => { | |
appFrame.contentWindow.eval(` | |
const request = new XMLHttpRequest(); | |
request.open('GET', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js', false); | |
request.send(''); | |
const script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.text = request.responseText; | |
document.head.appendChild(script); | |
if (!window.__events__) { | |
window.__events__ = []; | |
} | |
rrweb.record({ | |
emit(e) { | |
e.sessionId = '${sessionId}' | |
window.__events__.push(e) | |
} | |
}) | |
`) | |
}) | |
Cypress.on('fail', (error) => { | |
appFrame.contentWindow.eval(` | |
if (window.__events__) { | |
const body = JSON.stringify({ events: window.__events__ }) | |
window.__events__ = [] | |
const request = new XMLHttpRequest(); | |
request.open('POST', YOUR_BACKEND_API, false); | |
request.setRequestHeader('content-type', 'application/json'); | |
request.send(body); | |
} | |
`) | |
throw error | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment