RC 代码展示在清单 3 中。
清单 3. 自动化 Dojo 组合框中选择操作的 RC 代码
selenium.clickAt("//div[@id='widget_offeringType']/div/div",””);
selenium.waitForCondition("selenium.isElementPresent(\"offeringType_popup2\")", "2000");
selenium.clickAt("offeringType_popup2",””);
图 4. 日期选择器
对于图 4 中的日期选择器例子,执行的操作可能不会被 IDE 记录。编写如下面清单 4 所示的 RC 代码。
清单 4. 自动化选择的 RC 代码
//click on the date field by id you defined; selenium.clickAt("dateBox",""); //wait for the drop down date box by id; selenium.waitForCondition("selenium.isElementPresent(\"widget_dateBox_dropdown\")", \ "2000"); //click previous year 2008; selenium.clickAt("//span[contains(@class,'dijitCalendarPreviousYear')]", ""); //click on the month increase; //previous month would contains ‘dijitCalendarIncrease’. selenium.clickAt("//img[contains(@class,'dijitCalendarIncrease')]",""); //click on the date such as 28 of current month; If you do not specify //the td with the attribute of current month class, it will click \ on the //first 28 of previous month; selenium.click("//td[contains(@class,'dijitCalendarCurrentMonth')]/span[text()='28']"); |
如本例所示,Dojo 应用程序不能通过简单的 IDE 记录进行测试。这些脚本有可能不能通过测试。脚本中有一些丢失的操作,或者操作并不真正工作。脚本应该调整成能够在 IDE 和 RC 中顺利地执行。对于复杂的 Dojo 小部件,一种可能的解决方案是使用 runScript(String) 函数,因为 Selenium 对 JavaScript 提供很好的支持。清单 5 提供一个 JavaScript 语句来模拟组合框选择。
清单 5. 运行 JavaScript 语句在组合框上进行选择
selenium.runScript("dijit.byId(\"offeringType\").setValue(\"Stack(SWG)");");
如何利用 Ant 构建 Selenium 测试
诸如 Ant 这样的集成工具可以方便地构建 Selenium 测试和顺畅地运行测试用例,无需单独启动 Selenium 服务器。如果 Selenium 测试由 TestNG 驱动,那么定义清单 6 所示 TestNG Ant 任务。清单 6 中假设 classpath 是 TestNG.jar 文件的文件路径。
清单 6. TestNG Ant 任务
<taskdef resource="testngtasks" classpath="testng.jar"/>
主要的目标是启动服务器、运行测试,然后停止服务器。这些任务按照 bulid.xml 中定义的顺序实现在清单 7 中。
清单 7. 启动服务器、运行测试用例并停止服务器的 Ant 任务
<target name="run_test" description="start,run and stop" depends="dist"> <parallel> <antcall target="start-server" /> <sequential> <echo taskname="waitfor" message="Waitforproxy server launch" /> <waitfor maxwait="2" maxwaitunit="minute" checkevery="100"> <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" /> </waitfor> <antcall target="runTestNG" /> <antcall target="stop-server" /> </sequential> </parallel> </target> |