Selenium2学习-030-WebUI自动化实战实例-028-获取元素位置及大小

自动化测试过程中,有时需要获取元素的位置、大小,以获取元素的位置,通过 Actions 模拟鼠标,进行相对坐标操作。例如,有些元素定位不方便,或者需要对某一元素相对区域范围进行暴力点击测试,此时就需要获取相应的坐标。

获取元素位置和大小的源码比较简单,直接上码了......敬请参阅!

 1     /**
 2      * Get element position, and return integer Array [left, top, width, height]
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getElementPositionAndSize, 2015-7-27 23:38:31 Exp $
 6      * 
 7      * @param webdriver : WebDriver
 8      * @param by        : By
 9      * 
10      * @return int[left,top,width,height]
11      */
12     public int[] getElementPositionAndSize(WebDriver webdriver, By by){
13         // store element position
14         int[] elementPosition = new int[4];
15         
16         // get element
17         WebElement element = webdriver.findElement(by);
18         
19         // get location of element
20         Point e_location = element.getLocation();
21         
22         // get size of element
23         Dimension e_size = element.getSize();
24         
25         elementPosition[0] = e_location.getX();
26         elementPosition[1] = e_location.getY();
27         elementPosition[2] = e_size.width;
28         elementPosition[3] = e_size.height;
29         
30         return elementPosition;
31     }
32     
33     /**
34      * Get element position, and return integer Array [left, top, width, height]
35      * 
36      * @author Aaron.ffp
37      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java getElementPositionAndSize, 2015-7-27 23:35:31 Exp $
38      * 
39      * @param by  : By
40      * 
41      * @return int[left,top,width,height]
42      */
43     public int[] getElementPositionAndSize(By by){
44         // store element position
45         int[] elementPosition = new int[4];
46         
47         // get element
48         WebElement element = this.webdriver.findElement(by);
49         
50         // get location of element
51         Point e_location = element.getLocation();
52         
53         // get size of element
54         Dimension e_size = element.getSize();
55         
56         elementPosition[0] = e_location.getX();
57         elementPosition[1] = e_location.getY();
58         elementPosition[2] = e_size.width;
59         elementPosition[3] = e_size.height;
60         
61         return elementPosition;
62     }

 

至此,WebUI 自动化功能测试脚本第 028-获取元素位置及大小 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

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

 

posted @ 2015-08-03 16:51  范丰平  Views(381)  Comments(0Edit  收藏  举报