Last active
April 5, 2024 21:22
-
-
Save arjunattam/c08f16959d0d825717a2205583a40e44 to your computer and use it in GitHub Desktop.
Redux store testing with Playwright
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
const pw = require('playwright'); | |
(async () => { | |
const browser = await pw.chromium.launch(); | |
const context = await browser.newContext(); | |
// set addInitScript to run this on every page load in this context | |
// in the app, use window.IS_PLAYWRIGHT to set window.store = store; | |
await context.addInitScript('window.IS_PLAYWRIGHT = true;') | |
const page = await context.newPage(); | |
await page.goto('http://localhost:3000'); | |
// evaluate in page context to get window.store, and | |
// then assert values in nodejs | |
console.log(await page.evaluate(() => window.store.getState())); | |
// dispatching actions to modify redux store | |
await page.evaluate(() => { | |
window.store.dispatch({ type: 'ADD_TODO', text: 'Test dispatch' }); | |
}); | |
console.log(await page.evaluate(() => window.store.getState())); | |
await browser.close(); | |
})(); |
I guess you would expose the redux store through the window object from the app for this to work.
That's awesome, Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @arjunattam thanks for sharing this, are you using component testing or UI testing? Because I can't find window.store in mdn documentation, so I'm starting to think that this is not a browser concept but a Redux concept, and wondering if I should be able to ask for this, because I try your snippet without sucess.