Testcafe:有没有一种方法可以检查视口内可见的元素?

时间:2020-06-22 14:04:31

标签: testing automation automated-tests e2e-testing testcafe

我的网站具有无限滚动(延迟加载)。我正在尝试检查某个元素是否对用户可见,这意味着它是否在视口中。但我无法为此目的在testcafe中找到一种方法。

.exists.visible方法似乎不受视口的约束。

这里有没有可以使用的特殊方法或功能?

更新: 我只是从链接的答案中尝试了客户端功能。我不确定这样做是否正确,尤其是选择器部分。请帮忙。

const isElementInViewport = ClientFunction(() => {
    const getBoundValues = Selector('.grid-x').getBoundingClientRect();
    const windowHeight = window.innerHeight;
    const windowWidth = window.innerWidth;
    return getBoundValues.bottom > 0 && getBoundValues.right > 0 && getBoundValues.left < (windowWidth || document.documentElement.clientWidth) && getBoundValues.top < (windowHeight || document.documentElement.clientHeight);
});

    test(urlEnv, async t => {
        const elementVisible = await isElementInViewport();
        console.log(elementVisible);
    })

错误日志(在const elementVisible行=等待isElementInViewport()):

1) An error occurred in ClientFunction code:
ReferenceError: Can't find variable: _exportableLib

0 个答案:

没有答案