Skip to content

Instantly share code, notes, and snippets.

@Yuyz0112
Created January 17, 2019 09:53
Show Gist options
  • Save Yuyz0112/091de9510d75cab379e6da469bdf2842 to your computer and use it in GitHub Desktop.
Save Yuyz0112/091de9510d75cab379e6da469bdf2842 to your computer and use it in GitHub Desktop.
rrweb + cypress
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/rrweb@0.7.4/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