Created
September 23, 2021 18:20
-
-
Save guizmo51/991c26b323113cf851b7518016c76150 to your computer and use it in GitHub Desktop.
communication between Cypress and Vue app
This file contains 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
// ======CYPRESS_SIDE======== | |
//$window is available with cy.window() or in Cypress.Command | |
// $window = window's object | postMessage(options) | |
// postMessage allows cross-origin communication between Window objects | |
// Direction: CYPRESS => APP | |
$window.postMessage(OBJECT); | |
// On app side, don't forget to add a check about the origin | |
// inject variable and listener for dev/test env | |
// get the data in window's variable | |
$window['debug_controls'] | |
// ======APP_SIDE======== | |
// check environment, I don't want to share it in production | |
if (process.env.NODE_ENV == 'development') { | |
// set data in window's object. (allow to be share with test, | |
// Direction: app => test | |
window['debug_controls'] = JSON.stringify([DATA]); | |
// listen message from postMessage (from your test and other emitters) | |
window.addEventListener("message", (m) => { | |
// your business, feel free to check origin of the message, | |
// in my case Webpack posted messages too. | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment