Last active
August 25, 2023 07:04
-
-
Save patrick-made/5d6ca96e0752992fd8d7dc13096699d8 to your computer and use it in GitHub Desktop.
Puppeteer MUI Material Select Programatically Select Option
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
// struggled to figure this out, hopefully this helps you! | |
// component | |
import { Select, MenuItem, FormControl } from '@material-ui/core' | |
const SelectRamen = () => ( | |
<FormControl id="select-ramen"> | |
<InputLabel>Pick Your Ramen</InputLabel> | |
<Select> | |
{options.map((opt, i) => <MenuItem key={i} value={opt}>{opt.name}</MenuItem>)} | |
</Select> | |
</FormControl> | |
) | |
// e2e test | |
const puppeteer = require('puppeteer') | |
const browser = puppeteer.launch() | |
const page = browser.newPage() | |
async function muiSelectOption(buttonSelector, optionValue) { | |
await page.waitForSelector(buttonSelector) | |
await page.$eval(buttonSelector, el => { | |
const event = new MouseEvent('mousedown') | |
event.initEvent('mousedown', true, true) | |
return el.dispatchEvent(event) | |
}) | |
const liSelector = `li[data-value="${optionValue}"]` | |
await page.waitForSelector(liSelector) | |
await page.$eval(liSelector, e => e.click()) | |
} | |
muiSelectOption('#select-ramen[role="button"]', 'Mi-So-Good') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment