简体   繁体   English

ReferenceError:在e2e测试中未定义窗口

[英]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. 我正在使用TS + Puppeteer编写新的e2e测试,我需要向下滚动页面以单击按钮。

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? 我希望向下滚动页面,但会捕获到错误: ReferenceError: window is not defined -您能说,我做错了什么吗?

Use page.evaluate function to execute javascript in the page context. 使用page.evaluate函数在页面上下文中执行javascript。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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