Selenium Core and IDE extension

最近发现要使用ExtJs测试其实很麻烦,因为ExtJs的id是变化的,而Selenium IDE录制完后,ExtJs的下次打开页面,就无法进行回放了。因此很麻烦,不过通过一些网友进行交流得到如下一些测试方法:
引用

  1. 使用Xpath进行定位
  2. 适当使用selenium..runScript()方法
  3.应用Selenium IDE的extensions

下面我只针对第3种方法进行总结。
  如果你想知道第三种方法的原理:请你在你的firefox浏览器上
输入
引用

chrome://selenium-ide/content/recorder-handlers.js
chrome://selenium-ide/content/locatorBuilders.js

通过上面两个js你大致可以了解Selenium IDE录制的原理。
要用Selen现在我ium IDE的extensions。
步骤:在Selenium IDE的Options进入,在点击Options显示如下一个节目

在该页面你要注意:
Selenium Core extensions(user-extensions.js)
Selenium IDE extensions
下面我们准备两个脚本来进行extesions。
Js代码 复制代码  收藏代码
  1.   Selenium.prototype.assertExtEqual = function(expression, text) {   
  2.     /**  
  3.      * the euqal assertion of ext 
  4.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  5.      * @param String target value 
  6.      */  
  7.     var result = this.extEval(expression)   
  8.     if (result != text) {   
  9.         Assert.fail("the value of [" + result + "] " + expression   
  10.                 + " is not equal with " + text);   
  11.     }   
  12. };   
  13.   
  14. Selenium.prototype.assertExtGreaterThan = function(expression, text) {   
  15.     /**  
  16.      * the greater than assertion of ext 
  17.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  18.      * @param String target value 
  19.      */  
  20.     var result = this.extEval(expression)   
  21.     if (result <= text) {   
  22.         Assert.fail("the value of [" + result + "] " + expression   
  23.                 + " is not greater than " + text);   
  24.     }   
  25. }   
  26.   
  27. Selenium.prototype.assertExtGreaterEqualThan = function(expression, text) {   
  28.     /**  
  29.      * the greater and equal than assertion of ext 
  30.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  31.      * @param String target value 
  32.      */  
  33.     var result = this.extEval(expression)   
  34.     if (result < text) {   
  35.         Assert.fail("the value of [" + result + "] " + expression   
  36.                 + " is not greater equal than " + text);   
  37.     }   
  38. }   
  39.   
  40. Selenium.prototype.assertExtLessThan = function(expression, text) {   
  41.     /**  
  42.      * the less than assertion of ext 
  43.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  44.      * @param String target value 
  45.      */  
  46.     var result = this.extEval(expression)   
  47.     if (result >= text) {   
  48.         Assert.fail("the value of [" + result + "] " + expression   
  49.                 + " is not less than " + text);   
  50.     }   
  51. }   
  52.   
  53. Selenium.prototype.assertExtLessEqualThan = function(expression, text) {   
  54.     /**  
  55.      * the less and equal than assertion of ext 
  56.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  57.      * @param String target value 
  58.      */  
  59.     var result = this.extEval(expression)   
  60.     if (result > text) {   
  61.         Assert.fail("the value of [" + result + "] " + expression   
  62.                 + " is not less equal than " + text);   
  63.     }   
  64. }   
  65.   
  66. Selenium.prototype.doExecuteExtFunction = function(expression, text) {   
  67.     /**  
  68.      * do ext function ,if the expression end with ")" ,the params is not useful 
  69.      * @param expression ext expression return a ext function, just like "button1.getText" or "text1.getValue()" 
  70.      * @param String params ,just like "a,b,c" 
  71.      */  
  72.     if (expression.lastIndexOf(")") == expression.length - 1) {   
  73.         this.extEval(expression);   
  74.     } else {   
  75.         var scopeObj = this.extEval(expression.substring(0, expression   
  76.                 .lastIndexOf(".")));   
  77.         var func = this.extEval(expression);   
  78.         if (typeof(func) != "function") {   
  79.             Assert.fail("the value of [" + func + "] " + expression   
  80.                     + " is not a function");   
  81.         }   
  82.         var params = [];   
  83.         if (text) {   
  84.             params = text.split(",");   
  85.         }   
  86.         try {   
  87.             func.apply(scopeObj, params);   
  88.         } catch (e) {   
  89.             Assert.fail("error execute function [" + func + "] " + expression);   
  90.         }   
  91.     }   
  92. }   
  93.   
  94. Selenium.prototype.assertExtTrue = function(expression) {   
  95.     /**  
  96.      * the true assertion of ext 
  97.      * @param expression ext expression , just like "button1.hidden" 
  98.      */  
  99.     var result = this.extEval(expression);   
  100.     if (result !== true) {   
  101.         Assert.fail("the value of [" + result + "] " + expression   
  102.                 + " is not true");   
  103.     }   
  104. }   
  105.   
  106. Selenium.prototype.assertExtFalse = function(expression) {   
  107.     /**  
  108.      * the false assertion of ext 
  109.      * @param expression ext expression , just like "button1.hidden" 
  110.      */  
  111.     var result = this.extEval(expression);   
  112.     if (result !== true) {   
  113.         Assert.fail("the value of [" + result + "] " + expression   
  114.                 + " is not false");   
  115.     }   
  116. }   
  117.   
  118.   
  119. Selenium.prototype.assertExtNull = function(expression, text) {   
  120.     /**  
  121.      * the null assertion of ext 
  122.      * @param expression ext expression , just like "button1.text" 
  123.      */  
  124.     var result = this.extEval(expression);   
  125.     if (result !== null) {   
  126.         Assert.fail("the value of [" + result + "] " + expression   
  127.                 + " is not null");   
  128.     }   
  129. }   
  130.   
  131.   
  132. Selenium.prototype.assertExtNotNull = function(expression, text) {   
  133.     /**  
  134.      * the not null assertion of ext 
  135.      * @param expression ext expression , just like "button1.text" 
  136.      */  
  137.     var result = this.extEval(expression);   
  138.     if (result === null) {   
  139.         Assert.fail("the value of [" + result + "] " + expression + " is null");   
  140.     }   
  141. }   
  142.   
  143.   
  144. Selenium.prototype.assertExtUndefined = function(expression, text) {   
  145.     /**  
  146.      * the undefined assertion of ext 
  147.      * @param expression ext expression , just like "button1" 
  148.      */  
  149.     var result = this.extEval(expression);   
  150.     if (result !== undefined) {   
  151.         Assert.fail("the value of [" + result + "] " + expression   
  152.                 + " is not undefined");   
  153.     }   
  154. }   
  155.   
  156.   
  157. Selenium.prototype.assertExtNotUndefined = function(expression, text) {   
  158.     /**  
  159.      * the not undefined assertion of ext 
  160.      * @param expression ext expression , just like "button1" 
  161.      */  
  162.     var result = this.extEval(expression);   
  163.     if (result === undefined) {   
  164.         Assert.fail("the value of [" + result + "] " + expression   
  165.                 + " is undefined");   
  166.     }   
  167. }   
  168.   
  169.   
  170. Selenium.prototype.assertExtPresent = function(expression, text) {   
  171.     /**  
  172.      * the present assertion of ext 
  173.      * @param expression ext expression , just like "button1" 
  174.      */  
  175.     var result = this.extEval(expression);   
  176.     if (result == null || result == undefined) {   
  177.         Assert.fail("the value of [" + result + "] " + expression   
  178.                 + " is not present");   
  179.     }   
  180. }   
  181.   
  182. Selenium.prototype.assertExtNotPresent = function(expression, text) {   
  183.     /**  
  184.      * the not present assertion of ext 
  185.      * @param expression ext expression , just like "button1" 
  186.      */  
  187.     var result = this.extEval(expression);   
  188.     if (result != null || result != undefined) {   
  189.         Assert.fail("the value of [" + result + "] " + expression   
  190.                 + " is present");   
  191.     }   
  192. }   
  193.   
  194. Selenium.prototype.assertExtMatches = function(expression, text) {   
  195.     /**  
  196.      * the matches assertion of ext 
  197.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  198.      * @param String target value 
  199.      */  
  200.     var result = this.extEval(expression);   
  201.     var reg = new RegExp(text);   
  202.     if (!reg.test(result)) {   
  203.         Assert.fail("the value of [" + result + "] " + expression   
  204.                 + " is not match " + text);   
  205.     }   
  206. }   
  207.   
  208. Selenium.prototype.assertExtContains = function(expression, text) {   
  209.     /**  
  210.      * the contains assertion of ext 
  211.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  212.      * @param String target value 
  213.      */  
  214.     var result = this.extEval(expression);   
  215.     if (typeof(result) == "undefined" || result == null) {   
  216.         Assert.fail("the value of " + expression + " dos not contains " + text);   
  217.     } else if (result.indexOf) {   
  218.         if (result.indexOf(text) < 0) {   
  219.             Assert.fail("the value of [" + result + "] " + expression   
  220.                     + " dos not contains " + text);   
  221.         }   
  222.     } else {   
  223.         Assert.fail("the value of [" + result + "] " + expression   
  224.                 + " is not a String or Array");   
  225.     }   
  226. }   
  227.   
  228. Selenium.prototype.assertExtTypeof = function(expression, text) {   
  229.     /**  
  230.      * the typeof assertion of ext 
  231.      * @param expression ext expression , just like "button1.text" or "text1.getValue()" 
  232.      * @param String target value 
  233.      */  
  234.     var type = typeof(this.extEval(expression));   
  235.     if (type != text) {   
  236.         Assert.fail("the type of [" + type + "] " + expression + " is not "  
  237.                 + text);   
  238.     }   
  239. }   
  240.   
  241. PageBot.prototype.getWrappedWindow = function(extpath) {   
  242.     var win = this.getCurrentWindow() || {};   
  243.     return win.wrappedJSObject;   
  244. }   
  245.   
  246.   
  247. Selenium.prototype.getWrappedWindow = function(extpath) {   
  248.     return this.browserbot.getWrappedWindow();   
  249. }   
  250.   
  251.   
  252. Selenium.prototype.extEval = function(expression) {   
  253.     var script = expression;   
  254.     if (expression) {   
  255.         var expArr = expression.split(".");   
  256.         expArr[0] = "(window.Ext.getCmp('" + expArr[0] + "')||window.Ext.get('"  
  257.                 + expArr[0] + "')||window.Ext.StoreMgr.lookup('" + expArr[0]   
  258.                 + "'))";   
  259.         expression = expArr.join(".");   
  260.     }   
  261.     try {   
  262.         return this.doEval(expression);   
  263.     } catch (e) {   
  264.         throw new SeleniumError("the expression " + script   
  265.                 + " is not a Ext expression !");   
  266.     }   
  267. };   
  268. // I have to rewrite the eval function to get the context of window  
  269. Selenium.prototype.doEval = function(expression) {   
  270.     /**  
  271.      * execute js ecpression 
  272.      *  
  273.      * @param {Object} 
  274.      *            expression js expression 
  275.      */  
  276.     try {   
  277.         var win = this.getWrappedWindow();   
  278.         var result = eval(expression, win);   
  279.         return result;   
  280.     } catch (e) {   
  281.         throw new SeleniumError("the expression " + expression   
  282.                 + " is not a Ext expression !");   
  283.     }   
  284. }  

该代码命名为user-extensions.js通过Selenium Core extensions(user-extensions.js)后的Browser选择中自己写的user-extensions.js.或者也可以不要改js但是下面这个js是一定要的(放入Selenium IDE extensions)。

Js代码 复制代码  收藏代码
  1. var EXT_PREFIX = "ext-gen";   
  2. function findExtLocator(e) {   
  3.     function getElementIndex(el, p) {   
  4.         var childs = p.childNodes;   
  5.         for (var i = 0; i < childs.length; i++) {   
  6.             var curr = childs[i];   
  7.             if (curr == el) {   
  8.                 return "[" + (i + 1) + "]";   
  9.             }   
  10.         }   
  11.     }   
  12.     if (e.id) {   
  13.         var elId = e.id;   
  14.         if (elId.indexOf(EXT_PREFIX) == 0) {   
  15.             var currNode = e;   
  16.             var locator = "";   
  17.             while (currNode && currNode.tagName.toLowerCase() != "body") {   
  18.                 parentNode = currNode.parentNode;   
  19.                 locator = this.relativeXPathFromParent(currNode) + locator;//  
  20.                 if (parentNode.id && parentNode.id.length > 0   
  21.                         && parentNode.id.indexOf(EXT_PREFIX) != 0) {   
  22.                     locator = "//" + parentNode.tagName + "[@id='"  
  23.                             + parentNode.id + "']" + locator;   
  24.                     return locator;   
  25.                 }   
  26.                 currNode = currNode.parentNode;   
  27.             }   
  28.         }   
  29.     }   
  30.     return null;   
  31. }   
  32. LocatorBuilders.add('ext', findExtLocator);   
  33. // You can change the priority of builders by setting LocatorBuilders.order.  
  34. LocatorBuilders.order = ['ext''id''link''name''dom:name''xpath:link',   
  35.         'xpath:img''xpath:attributes''xpath:href''dom:index',   
  36.         'xpath:position'];  

该段代码是必须的。产生的效果是让Selenium IDE在录制Extjs的时候能够回放成功。


总结下使用步骤:
引用

  第一:将第一段js代码放入Selenium Core extensions(user-extensions.js)
  第二:将第二段js代码放入Selenium IDE extensions 这是必须的。

现在你可以使用Selenium IDE来测试ExtJs了。你可以看看是不是可以正常进行录制和回放。ExtJs没有ZK厚道,ZK官网提供了一个接口来实现Selenium IDE的测试。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium Core 是一个用来测试 Web 应用的测试工具。Selenium Core的测试直接运行在浏览器中,就像真实的用户在操作一样。它可以分别运行在 Windows,Linux 和 Macintosh 系统的 Internet Explorer,Mozilla 和 Firefox 浏览器中。 浏览器兼容性测试 测试你的应用看是否可以在不同的操作系统的不同浏览器上正常工作。同样的脚本可以运行在任何 Selenium 平台上。< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> 系统功能测试 创建回归测试用来检验应用的功能以及用户的满意度。 Selenium Core 使用了一个独特的机制让它可以运行在如此多的平台。测试脚本采用纯JavaScript或DHTML写成,你将 Selenium Core 的测试脚本直接放置到你的应用程序所在的 Web 服务器中,就可以在客户端使用任何受支持的浏览器运行测试了。 Selenium 使用 JavaScript 和 Iframe 自动化的测试引擎嵌入到你的浏览器中。这项技术应该可以工作在任何启用 JavaScript 的浏览器中。因为不同的浏览器处理 JavaScript 有些不同,所以我们通常不得不调整引擎以便更广泛的支持 Windows,Mac OS X 和 Linux 上的不同浏览器。 下面说下运行Selenium Core需要的环境: l 去Open QA下载最新的Selenium Coreselenium-core-< xmlnamespace prefix ="st1" ns ="urn:schemas-microsoft-com:office:smarttags" />0.8.2.zip l 把下载的selenium-core-0.8.2.zip解压到你的web服务器中,下面我将在Tomcat中演示。 安装步骤: 第一步:去apache.org下载tomcat安装包。 第二步:正确安装tomcat 第三步:去Open QA下载Selenium Core包 第四步:解压Selenium Core包到D:\selenium下 第五步:配置tomcat文件,配置一个sc.xml,其他配置项为<Context path="/sc" docBase="D:/selenium/selenium-core-0.8.2" debug="0" privileged="true"> 第六步:运行tomcat服务 第七步:打开浏览器,输入:http://localhost:8080/sc/ 这时你能看到下图界面: < xmlnamespace prefix ="v" ns ="urn:schemas-microsoft-com:vml" />

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值