【发布时间】:2019-09-30 21:23:36
【问题描述】:
如何使用 TestCafe 向上和向下滚动?
我试过window.scroll(),
window.scrollTo()并抛出错误窗口未定义。
【问题讨论】:
标签: testing scroll automated-tests e2e-testing testcafe
如何使用 TestCafe 向上和向下滚动?
我试过window.scroll(),
window.scrollTo()并抛出错误窗口未定义。
【问题讨论】:
标签: testing scroll automated-tests e2e-testing testcafe
更新: 在v1.14.0及以后的版本中,可以使用以下滚动方式:t.scroll、t.scrollBy、t.scrollIntoView。
旧答案:
根据您的情况,您可以创建自己的ClientFunction:
import { ClientFunction } from 'testcafe';
fixture `Fixture`
.page `https://github.com/DevExpress/testcafe`;
const scroll = ClientFunction(function() {
window.scrollBy(0,1500);
});
test(`test`, async t => {
await scroll();
await t.wait(1000);
});
t.hover 动作示例:
// scroll to the "#some-element-scroll-to" element
await t.hover(Selector('#some-element-scroll-to'));
【讨论】:
我在 Mac 上遇到了类似的问题,并使用以下内容来滚动查看页面中的已知元素。
import { ClientFunction, Selector } from 'testcafe';
const scrollIntoView = ClientFunction( (selector: Selector) => {
const element = selector() as unknown as HTMLElement;
element.scrollIntoView();
});
fixture`HTMLElement`
.page('https://example.com');
test('Scroll element into view', async t => {
const bottomOfPage = Selector('#bottom-div');
await scrollIntoView(bottomOfPage);
});
【讨论】: