简体   繁体   中英

ReferenceError: window is not defined at e2e test

I'm writing a new e2e test using TS + Puppeteer and I need to scroll a page down, to click on a button.

it('user create request at homepage', async () => {
    await page.goto(`${global.HOST}`, { waitUntil: 'networkidle0' });
    const postRequestBtn = 'qa-id="dummybtn"';
    await window.scrollBy(0, document.body.scrollHeight);
    //also tried await window.scrollTo(0,100);
    await page.waitForSelector(postRequestBtn);
});

I expect to scroll the page down, but it catches an error: ReferenceError: window is not defined - can you please say,what I'm doing wrong?

Use page.evaluate function to execute javascript in the page context.

Scroll to the element:

await page.$eval('qa-id="dummybtn"', el => el.scrollIntoView());

Scroll to the bottom:

await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM