Selenium2学习-022-WebUI自动化实战实例-020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小)

时间:2021-09-09 13:00:23

前几篇文章中简略概述了,如何获取、设置浏览器窗口大小,那么我们该如何获取浏览器显示区域的大小呢?此文讲对此进行简略概述,敬请各位小主参阅。若有不足之处,敬请各位大神指正,不胜感激!

获取浏览器显示区域的方法,我目前想到的只有以下两种方法:

1、通过 JavaScript  获取浏览器显示区域的大小

2、通过 WebDriver 截图,获取截图的大小,从而获得浏览器显示区域的大小

此文主要以第一种方法示例演示,第二种方法进行后续更新,敬请期待!谢谢!

     /**
* Get width and height about display screen area by JavaScript
*
* @author Aaron.ffp
* @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getBrowserDisplayAreaSizeByJS, 2015-7-28 02:27:33 Exp $
*
* @return int[width,height]
*/
public int[] getBrowserDisplayAreaSizeByJS(){
int[] browserDisplayAreaSize = new int[2];
String js = "return window.innerWidth + ';' + window.innerHeight"; String[] areaSize = ((JavascriptExecutor)this.webdriver).executeScript(js).toString().split(";"); browserDisplayAreaSize[0] = Integer.valueOf(areaSize[0]);
browserDisplayAreaSize[1] = Integer.valueOf(areaSize[1]); return browserDisplayAreaSize;
}

通过浏览器的控制台,执行脚本的结果如下图所示。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAC0CAIAAACIdrt6AAAa3klEQVR4nO3d8VMTeYL3cf+B+WV+tmqr8oPTRQ1V/vI4VwdbTw239VDUXXGyc7r3PBFhT+bA9YS4LmHcM4oxicSVnX0WmRQ+O3EfhYl0ldx4UBFxLiWudfTQhqzALDLtJDEYAUkIMaGhbdLf+6GvenpDEoMdGNj5vGp+iEmnv992at7VnW+ms+v58+e7AADgjRFCvuspAADsZMgoAIAm6owSAADIDzIKAKAJMgoAoAkyCgCgCTIKAKDJ1mU0mUw2NTX19PRsxs5zmJ+fP3jw4P3797d43J6enqampmQyucXjAsAWyzej8Xh8ZGREy0gbzShN0zR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no//GCn9liv/z/0smV7Ne3iOjAPC9gowCAGiCi3oAAE2wxAQAoAm+8AQAoMnGPhvNBzIKAN8ryCgAgCbIKACAJoXPKADA9woyCgCgCTIKAKAJMgoAoAkyCgCgCTIKAKAJMgoAoAkyCgCgCTIKAKBJ5owCAMCGIaMAAJogowAAWvwXpO7PuFXkIJwAAAAASUVORK5CYII=" alt="" />

PS:在实际的使用过程中,若是浏览器窗口大小发生变化,需要重新获取浏览器显示区域的大小。

至此,WebUI 自动化功能测试脚本第 020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小) 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^