您的位置:首页 > 其它

Selenium2.0与Flash(Flex)自动化实现

2014-09-08 11:09 204 查看
初步调研了一下,Selenium2.0(webdriver)还没有flash测试解决方案。网上可以搜到的还是selenium1.0时代的那一套flash测试扩展SeleniumFlexAPI

(http://code.google.com/p/flash-selenium/),而这套扩展从09年开始就停止更新了。

大致了解了SeleniumFlexAPI的设计思路:
一是在flash文件build时加入特定的.swc架包使得flash内部的函数和属性可以开放给JS调用(经确认,这个架包对于Flex3.x还是有效的,4.0以上就不得而知了);
二是提供了一套和Selenium1.0绑定的JS扩展,可以调用flash开放出来的接口。

把这套JS扩展提取出来的话,就可以摆脱对于Selenium1.0的依赖。

具体实现步骤:

【一】

SeleniumFlexAPI.swc加入到被测试flash的Compiler参数中,并重新build。



 

 

如果build成功,在网页中打开这个Flash,把鼠标hover到某个元素上,会显示这个元素的ID。



 

 

另一个验证方法:

打开浏览器自带调试器,在console中输入document.Training_Flex_demo.doFlexClick('A',''),若成功,则返回“true”

其中,Training_Flex_demo是Flash在所嵌入网页中的ID,doFlexClick是通过SeleniumFlexAPI.swc开放出来的JS接口,用以控制Flash。具体接口和参数没有现成文档,要直接查看源代码去查找。

【二】

在Dagger框架(传送门:http://qa.blog.163.com/blog/static/190147002201212775156162/)中加入相关代码如下:

/**

 * 在Flash中点击元素

 * @param flashID flash本身在网页中ID

 * @param targetName 目标元素在flash中Name

 */

public void flexClick(String flashID, String targetName) {

pause();

waitForFlexElementPresent(flashID, targetName);

js.executeScript("return document.getElementById('" + flashID + "').doFlexClick('" + targetName + "','')");

}

/**

 * 获取Flash中元素左上角的(浏览器,非屏幕)坐标

 * @param flashID flash本身在网页中ID

 * @param targetName 目标元素在flash中Name

 * @return 坐标[x,y]

这个函数的现实作用何在?可以参看这篇博文(传送门:http://qa.blog.163.com/blog/static/190147002201212775156162/)中Flash相关内容

 */

public int[] flexGetPosition(String flashID, String targetName) {

pause();

waitForFlexElementPresent(flashID, targetName);

Object posObject = js.executeScript("return document.getElementById('" + flashID + "').getFlexGlobalPosition('" + targetName + "','')");

String posString = (String) posObject;

int[] posInt = new int[2];

posInt[0] = Integer.parseInt(posString.split(",")[0]);

posInt[1] = Integer.parseInt(posString.split(",")[1]);

return posInt;

}

/**

 * Flex元素定位

 * @param flashID

 * @param targetName

 */

private void waitForFlexElementPresent(String flashID, String targetName) {

int Timeout = Integer.parseInt(GlobalSettings.Timeout);

long startTime = System.currentTimeMillis();

waitForFlexElementPresent(flashID, targetName, startTime, Timeout);

try {

Thread.sleep(2500);  // 延迟,等待元素渲染出来

catch (InterruptedException e) {

e.printStackTrace();

}

}

/**

 * Flex元素定位

 * @param flashID

 * @param targetName

 * @param startTime

 * @param Timeout

 */

private void waitForFlexElementPresent(String flashID, String targetName, long startTime, int Timeout) {

try {

js.executeScript("return document.getElementById('" + flashID + "').doFlexWaitForElementVisible('" + targetName + "','')");

catch (Exception e) {

if (System.currentTimeMillis() - startTime > Timeout) {

Assert.fail("*** Flex元素(" + targetName + ")定位失败 ***");



else {

try {

Thread.sleep(1000);

catch (InterruptedException s) {

s.printStackTrace();

}

waitForFlexElementPresent(flashID, targetName, startTime, Timeout);

}

}

}

这套代码已在Firefox10.0上运行验证。 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐