I’m working with Puppeteer to test a React application and running into issues with controlled form elements. When I change input values directly through DOM manipulation, React doesn’t update its internal state or virtual DOM.
I found a workaround that manually dispatches events, but I want to simplify it using jQuery methods instead of custom event functions.
test('form interaction test', async () => {
const browser = await puppeteer.launch({ headless: false });
const webpage = await browser.newPage();
await webpage.goto('http://localhost:3000/dashboard');
await webpage.waitForSelector('.form-control', { visible: true });
await webpage.addScriptTag({ url: 'https://code.jquery.com/jquery-3.2.1.min.js' });
const elementVisible = await webpage.evaluate(() => {
const $ = window.$;
function triggerEvent(element, eventType) {
const customEvent = new Event(eventType, { target: element, bubbles: true });
return element ? element.dispatchEvent(customEvent) : false;
}
const dropdown = $('[data-testid="category-select"]')[0];
dropdown.value = 'ShowHiddenContent';
triggerEvent(dropdown, 'change');
return $('.hidden-content-panel').length;
});
expect(elementVisible).toBe(1);
await browser.close();
});
Is there a way to replace the triggerEvent function with native jQuery event methods?