isDisplayedInViewport
Overview
Use the isDisplayedInViewport
command to determine whether a given DOM element is displayed within the viewport.
The command returns true
if the selected DOM element is partially visible and within the viewport; otherwise, it returns false
.
Usage
await browser.$(selector).isDisplayedInViewport();
Usage Examples
index.html
<div id="notDisplayed" style="display: none"></div>
<div id="notVisible" style="visibility: hidden"></div>
<div id="notInViewport" style="position:absolute; left: 9999999"></div>
<div id="zeroOpacity" style="opacity: 0"></div>
isDisplayedInViewport.js
it("should detect if an element is visible", async ({ browser }) => {
let isDisplayedInViewport = await browser.$("#notDisplayed").isDisplayedInViewport();
console.log(isDisplayedInViewport); // outputs: false
isDisplayedInViewport = await browser.$("#notVisible").isDisplayedInViewport();
console.log(isDisplayedInViewport); // outputs: false
isDisplayedInViewport = await browser.$("#notExisting").isDisplayedInViewport();
console.log(isDisplayedInViewport); // outputs: false
isDisplayedInViewport = await browser.$("#notInViewport").isDisplayedInViewport();
console.log(isDisplayedInViewport); // outputs: false
isDisplayedInViewport = await browser.$("#zeroOpacity").isDisplayedInViewport();
console.log(isDisplayedInViewport); // outputs: false
});
References
We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.