My website has an infinite scroll (lazy loading). I am trying to check if an element is visible to the user, meaning if it is on the viewport. But I couldn't find a method in testcafe for this purpose.
The .exists
and .visible
methods seem to be not bound by viewport.
Is there any special method or function that could be used here?
Update: I just tried the client function from the linked answer. I am not sure if am doing it right, especially the selector part. Please help.
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);
})
Error Log (at line const elementVisible = await isElementInViewport()):
1) An error occurred in ClientFunction code:
ReferenceError: Can't find variable: _exportableLib