Your given example is indeed a long page where the Element in question is loaded in the DOM but is not visible in the browser view port. However, when using the ... should be visible
keywords only look at the DOM and from that perspective it is visible. Just not view-able.
There are small javascript functions that can make the assessment if a visible element is indeed view-able. On this Go Make Things article the isInViewPort(elem)
function is found.
IsInViewPort.js
/*
* Obtained from: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
*/
var isInViewport = function (elem) {
var bounding = elem.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
In Robot Script we can then proceed to fetch the element and then pass it to this function with the custom keyword: Element Should Be In View
which returns a boolean
when an element is true and which is not.
Is Element In View.robot
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Element Should Be in View Test
[Documentation]
... Test to validate that an element isn't view-able and then is view-able.
Open Browser https://en.wikipedia.org/wiki/Selenium_(software) Chrome
${elem} Get Webelement xpath=//*[@id="Selenium_Remote_Control"]
${bool} Element Should Be In View ${elem}
Should Not Be True ${bool}
Click Element //*[@href="#Selenium_Remote_Control"]
${elem} Get Webelement xpath=//*[@id="Selenium_Remote_Control"]
${bool} Element Should Be In View ${elem}
Should Be True ${bool}
[Teardown] Close All Browsers
*** Keywords ***
Element Should Be In View
[Documentation]
... Partially Inspired by: https://stackoverflow.com/a/28709012/6152737
[Arguments] ${element}
${s2l}= Get Library Instance Selenium2Library
${js}= Get File IsInViewport.js
${visible}= Call Method
... ${s2l._current_browser()}
... execute_script ${js} return isInViewport(arguments[0]); ${element}
[Return] ${visible}
When the view changed, i.e. because of the clicked link, then the element needs to be retrieved again. Otherwise the old status is not returned.