最初我认为isDisplayed可能有所帮助,并编码如下:
@H_502_13@element(by.id(postId)).isDisplayed().then((isDisplayed) => { expect(isDisplayed).toBe(true); });仔细阅读documentation之后,isDisplayed不会检查元素是否在视口内.
一个hacky方法是计算各种元素的位置,从可滚动的父级(在我的情况下不是窗口)开始.
检查这个是最好的做法吗?
解决方法
>创建类MyRect扩展DOMRect
>基于getBoundingClientRect创建函数getBoundingClientMyRect
>基于(0,wWidth,wHeight)创建函数getWindowMyRect
让wHeight = window.innerHeight || document.documentElement.clientHeight;
让wWidth = window.innerWidth || document.documentElement.clientWidth;
>在MyRect中创建方法isInsideMyRect – 基于https://gist.github.com/davidtheclark/5515733
>在MyRect中创建方法isPartOfMyRect – 基于https://gist.github.com/davidtheclark/5515733#gistcomment-2113166
el.getWindowMyRect().isInsideMyRect(getWindowMyRect())
Secand理念:
> elm.getBoundingClientRect()并记下该值>如果需要,滚动到元素:browser.executeScript(“arguments [0] .scrollIntoViewIfNeeded();”,elm.getWebElement());> elm.getBoundingClientRect() – 如果值已更改,则再次与之前的值进行比较,然后滚动是必要的