最终版本
以下是测试用例001和002的完整的(经过充分重构的)测试脚本。
load File.dirname(__FILE__) + '/test_helper.rb' test_suite "Complete Test Script" do include TestHelper before(:all) do open_browser "http://newtours.demoaut.com" end before(:each) do home_page = expect_page HomePage home_page.login end after(:each) do click_link("SIGN-OFF") goto_page("/") end test "[001] one way trip" do select_flight_page = expect_page SelectFlightPage select_flight_page.select_trip_oneway select_flight_page.select_from_new_york select_flight_page.select_to_sydney select_flight_page.click_continue assert_text_present("New York to Sydney") end test "[002] round trip" do select_flight_page = expect_page SelectFlightPage select_flight_page.select_trip_round select_flight_page.select_from_new_york select_flight_page.select_to_sydney select_flight_page.click_continue assert_text_present("New York to Sydney") assert_text_present("Sydney to New York") end end |
适应变化
我们的世界并不完美。在软件开发行业,事物频繁发生变更。幸运的是,以上的工作使得测试脚本不仅仅更易读,而且也更容易适应变化。
1. 客户修改了术语
众所周知,项目使用同一套语言是一个好的实践,即使在测试脚本里面也是如此。举例来说,客户现在更倾向于使用“Return Trip”这个名词,而不再是“Round Trip”。借助于重构测试脚本,这很容易做到。
把光标移到“SelectFlightPage”类(pages\select_flight_page.rb)的“select_trip_round”函数,在“Refactoring”菜单下选择“Rename ...”项(Shift+F6)
图 7. “Refactor”菜单-“Rename”
然后输入新的函数名字“select_return_trip”。
图 8. “Rename Function”对话框
测试脚本其他引用“select_trip_round”的地方就都更改为
select_flight_page.select_return_trip
2. 应用程序的修改
应用程序(来自程序员)的修改就更普遍了。举例来说,程序员基于某些原因修改了航班选择页面,导致HTML页面上出发城市的属性从
<select name="fromPort">
改成
<select name="departurePort">
虽然用户不会察觉到任何变化,测试脚本(任何访问这个页面的测试用例)现在却会失败。如果你直接用录制的脚本文件作为测试脚本,修改的操作将会非常乏味,而且易于引入错误。
定位到“SelectFlightPage”的“select_from_new_york”方法(使用快捷键Ctrl+T选中“select_flight_page”,再输入快捷键Ctrl+F12选择“select_from_xx”),把“fromPort”改成“departurePort”。
def select_from_new_york |
看上去还不赖!
结论
本文我们介绍了在自动化功能测试中使用Page对象,以使测试脚本易于理解和维护。通过一个使用iTest2 IDE改善测试脚本过程的实际例子,我们演示了其提供的丰富的重构功能。
相关阅读: