doh robot作界面自动化测试

之前我介绍过doh用于UI单元测试,这篇文章介绍如何使用doh robot作界面自动化测试。

按照之前的介绍,读者会发现要使用doh robot必须作如下两件事情

1)编写测试页面,在页面中写入doh的测试代码

2)上面的测试所有的页面都是停留在当前页面,没有发生页面提交的动作

那么能不能在利用doh robot的界面动作模拟来测试一个应用呢?答案是肯定的

doh robot停工如下两个API来实现界面的自动化测试

initRobot: function(/*String*/ url){ // summary: // Opens the application at the specified URL for testing, redirecting dojo to point to the application environment instead of the test environment. // // url: // URL to open. Any of the test's dojo.doc calls (e.g. dojo.byId()), and any dijit.registry calls (e.g. dijit.byId()) will point to elements and widgets inside this application. // }

waitForPageToLoad: function(/*Function*/ submitActions){ // summary: // Notifies DOH that the doh.robot is about to make a page change in the application it is driving, // returning a doh.Deferred object the user should return in their runTest function as part of a DOH test. // // description: // Notifies DOH that the doh.robot is about to make a page change in the application it is driving, // returning a doh.Deferred object the user should return in their runTest function as part of a DOH test. // Example: // runTest:function(){ // return waitForPageToLoad(function(){ doh.robot.keyPress(dojo.keys.ENTER, 500); }); // } // // submitActions: // The doh.robot will execute the actions the test passes into the submitActions argument (like clicking the submit button), // expecting these actions to create a page change (like a form submit). // After these actions execute and the resulting page loads, the next test will start. // }

第一个API用于将应用中的连接加入到测试页面,请看下面的代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>doh.robot Spinner Test</title> <mce:style><!-- @import "../../../../util/doh/robot/robot.css"; --></mce:style><mce:style mce_bogus="1"><!-- @import "../../../../util/doh/robot/robot.css"; --></mce:style><mce:style mce_bogus="1" mce_bogus="1"><!-- @import "../../../../util/doh/robot/robot.css"; --></mce:style><style mce_bogus="1" mce_bogus="1" mce_bogus="1"> @import "../../../../util/doh/robot/robot.css"; </style> <!-- required: dojo.js --> <mce:script type="text/javascript" src="../../../../dojo/dojo.js" mce_src="dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true"></mce:script> <mce:script type="text/javascript"><!-- dojo.require("dijit.dijit"); // optimize: load dijit layer dojo.require("dijit.robotx"); // load the robot dojo.addOnLoad(function(){ // declare variables but do not assign them values var spin1; var spin2; var spin3; var safeClick; var delta=1; // redefine with doh.robot.mouseWheelSize when it is available // the initRobot call goes here doh.robot.initRobot('../test_Spinner.html'); doh.register("setUp",{ name: "setUp", timeout: 15000, setUp:function(){ // assign variables HERE spin1=dijit.byId('integerspinner1'); spin2=dijit.byId('integerspinner2'); spin3=dijit.byId('realspinner1'); safeClick=dojo.byId('form1'); }, runTest: function(){ // assert onChange not fired doh.is("not fired yet!",dojo.byId('oc1').value); doh.is(1,spin1.smallDelta); var s=": 900/n" +"integerspinner1: 900/n" +": not fired yet!/n" +": 1,000/n" +"integerspinner2: 1000/n" +": /n" +"integertextbox3: NaN/n" +": 1.0/n" +"realspinner1: 1/n"; doh.is(s, dojo.doc.displayData().replace(/[a-zA-Z0-9_]*_displayed_/g, "")); } }); doh.register("arrowButton",{ name: "spinner1_invalid", timeout: 15000, runTest: function(){ // assert invalid works var d=new doh.Deferred(); doh.robot.mouseMoveAt(spin1.focusNode,500); doh.robot.mouseClick({left:true},500); doh.robot.sequence(function(){ spin1.focusNode.value=""; },500); doh.robot.typeKeys("0.5",500,300); doh.robot.sequence(function(){ try{ doh.is(false,spin1.isValid()); d.callback(true); }catch(e){ d.errback(e); } },500); return d; }, tearDown:function(){ spin1.attr('value',1); } }); // ... some more tests // all tests registered; notify DOH doh.run(); }); // --></mce:script> </head>

通过上面的代码,可以看出通过doh robot.initRobot,可以将外部的页面引入doh测试框架。

如果我们观察一下测试页,可以发现其实现的时候,是用一个IFRANE将页面框起来。

虽然有了IFRAME, dojo.doc指向的是你页面的document,dojo.byId()可直接根据你页面元素的Id获得页面中的控件,如果你的项目是dojo项目,可以使用dijit.byId获得widget对象。不过document,以及window对象指向的不是应用中页面,而是测试代码所在页面。

waitForPageToLoad的样例例代码如下,也比较容易让人明白

doh.register('user_story1',{ name: 'login_pagechange', timeout: 60000, runTest: function(){ return doh.robot.waitForPageToLoad(function(){ // click login doh.robot.mouseMoveAt(function(){ return dojo.doc.getElementsByTagName('input')[2]; }, 1623, 801); doh.robot.mouseClick({left:true, middle:false, right:false}, 992); }); } });

 

最后说一下doh测试结果获得的接口

首先你的测试要纳入整个doh的测试框架,包括要定义module.js以及测试套文件

再次可以修改runner.html中的代码,增加如下代码

doh._onEnd = function() { //这里加入你的处理 }

doh对象存在如下属性,可以方便的获得测试结果

_testCount,_errorCount,_failureCount

 

 

 

 

你可能感兴趣的:(function,测试,application,import,returning,variables)