Skip to content

Instantly share code, notes, and snippets.

@arjunattam
Last active April 5, 2024 21:22
Show Gist options
  • Save arjunattam/c08f16959d0d825717a2205583a40e44 to your computer and use it in GitHub Desktop.
Save arjunattam/c08f16959d0d825717a2205583a40e44 to your computer and use it in GitHub Desktop.
Redux store testing with Playwright
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();
})();
@Juanicanete10
Copy link

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.

@Jsijbel
Copy link

Jsijbel commented Feb 7, 2023

I guess you would expose the redux store through the window object from the app for this to work.

@filepi
Copy link

filepi commented Apr 5, 2024

That's awesome, Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment