----------------------------------------------------------------------------------------------
http://files.cnblogs.com/zhangfei/WAT_1.7.rar
change log:
1.增加一個命令行運行的all命令
2.增加多瀏覽器的支持
用法:
1.ruby CommandWAT.rb all
2.在全局的conf.yaml文件的Driver選項進行配置
功能:
1.可運行testcase文件夾下面的所有case,且在report文件夾下面生成一個總的報告report.html
2.可自行選擇是用watir/watir-webdriver,及瀏覽器
如何更新:解壓后,全部覆蓋即可。
WAT_1.7版本命令行使用方法:
1.創建project. cd到runTime目錄里,用命令ruby CommandWAT.rb -cp Demo 在testcase 文件夾下會創建一個Demo的project
2.在runTime目錄里,可以用命令ruby CommandWAT.rb list查看所有的project,用命令ruby CommandWAT.rb all運行所有的project,用命令ruby CommandWAT.rb all -y運行在runTime/run.yaml里配置的所有的project
3.還有其它的運行單個project,test 等的命令,可用ruby CommandWAT.rb -h查看
http://files.cnblogs.com/zhangfei/WAT-1.8.rar
更新說明:
1.8版本是一個gem包
安裝方法:
1.cd到存放gem包的目錄,用命令gem install WAT-1.8.gem -l 安裝(前提條件是watir>3.0)
使用方法:
1.安裝完后,用wat -h去查看命令
2.創建workspace. cd到你要創建workspace的目錄,用命令wat -cw test 會創建一個test的文件夾,該文件夾就是workspace
3.創建project. cd到workspace目錄里,用命令wat -cp Demo 在testcase 文件夾下會創建一個Demo的project
4.在workspace里,可以用命令wat list查看所有的project,用命令wat all運行所有的project,用命令wat all -y運行在config/run.yaml里配置的所有的project
5.還有其它的運行單個project,test 等的命令,可用wat -h查看
WAT的使用方法:
1.當創建完project "Demo"后,在testcase文件夾下會創建一個Demo的文件夾,Demo文件夾下會出現如下的文件:conf.yaml,Demo.rb,Demo.yaml,ExpectedData.yaml,TestData.yaml
2.conf.yaml的作用
- conf.yaml創建時為空,
- conf.yaml文件是對config/conf.yaml文件的擴展,在conf.yaml里可以自定義配置選項,如果conf.yaml與config/conf.yaml的配置項相同,config/conf.yaml里的配置項則會被conf.yaml里的配置項覆蓋。
- 在測試方法里,即Demo.rb文件里的測試方法里,調用conf.yaml與config/conf.yaml里的配置項的方法為:ConfigData("配置項名稱")。
3.Demo.yaml的作用
- Demo.yaml是存放頁面元素對象的文件。
- Demo.yaml文件在最初始時不為空,顯示了存放頁面元素對象的兩種方式。
- 第一種方式中,type是必填項.parent結點,如果有parent,則需加上,如沒有,則不加、。比如@ie.div(:class,"test").div(:index,1),示例為:
test1:
type: div
class: test
test2:
type: div
index: 1
parent: test1
test1沒有parent,所以不必要加上該結點,test2有parent "test1",所以需要加上parent,value為test1。 - 第二種方式中,寫法為:
test2: div(:class,"test").div(:index,1)
我們在項目中,其實第二種方式用的比較多。 - 第三種方式,在Demo.yaml文件中沒有演示出來,用法是:(第三種方式配合第二種方式,這樣效果會更好)
test1: div(:class,"test")
test2: %test1%.div(:index,1) - 在測試方法里,即Demo.rb文件里的測試方法里,調用Demo.yaml的頁面元素對象的方法為:AutoTest("test1"),AutoTest("test2").
4.TestData.yaml的作用
- TestData.yaml是存放測試數據的文件,格式一定要嚴格按照文件中已定義好的格式
- 由於該框架是數據驅動的模式,數據驅動的概念是指腳本或測試方法根據配置的數據的條數來循環運行,所以除common結點外,其它的結點是腳本中測試方法的名稱,比如在Demo.rb文件中有個test_Demo方法,所以在TestData.yaml的配置:
- test_Demo:
description: test for 123
inputValue: 123
- test_Demo:
description: test for 234
inputValue: 234
則test_Demo方法會運行兩次,在腳本中用inputValue的值時,第一次是123,第二次為234.
如果在TestData.yaml里的結點與Demo.rb文件中的測試方法名不一致,則會沒有測試方法被運行。 - TestData.yaml中的common結點,是配置的測試方法中的數據的公共結點,common結點里的數據,在配置的測試方法中都可以被使用,如果測試方法中與common中存在相同的數據,則common結點中的數據會被覆蓋。比如:test_Demo中的description的value值就會覆蓋common中的description的value值。TestData.yaml文件中必須存在三個數據結點:private,smoking,description。否則會報錯。
- private,smoking是兩種運行模式,在config/conf.yaml中RunTimeModule的值如果為private,則會運行private的值為y的測試方法,比如:
- test_Demo:
private: y
description: test for 123
inputValue: 123
- test_Demo:
private: n
description: test for 123
inputValue: 123
則test_Demo只會private被標記為y的一次,標記為n的則不會被運行 - description的數據則會被顯示在報告中。
- 在testcase文件夾下面有個GlobalData.yaml,也是存放測試數據的,里面的數據會被用到所有的project中,里面的數據如果與project中TestData.yaml的測試數據一樣時,則會被project中TestData.yaml的測試數據覆蓋。比如在一個系統中,可能都會有用戶名與密碼,則放在GlobalData.yaml中即可,存放數據的格式為:
loginname: test1
password: test - 總結:如果GlobalData.yaml存在loginname: test1,在ExpectedData.yaml的common結點下存在loginname: test2,在test_Demo下存在loginname: test3,則在腳本中調用loginname時,值為test3.
- 測試數據在test_Demo中的調用方式為:TestData("loginname")
5.ExpectedData.yaml的作用
- ExpectedData.yaml是存放期望值的文件
- ExpectedData.yaml存放數據的格式為:
hello: 123
world: 234 - ExpectedData.yaml中的數據在test_Demo中的調用方式為:ExpectData("hello")
6.Demo.rb的作用
- Demo.rb是存放測試方法的地方
- Demo.rb在被生成時,就已經生成好了類與所需要require的文件,格式都已定義好,只需要填寫好測試方法即可,當然類名與測試方法名也可以更改,但測試方法名如果更改了,則需記得在TestData.yaml中配置上相應測試方法名。
- setUp方法是指每個測試方法運行前必須會運行的方法,getWebApplication是指創建一個瀏覽器的對象,調用getWebApplication后會產生一個框架的內置對象@b(類似於watir中@b=Watir::IE.new)。
- tearDown是指每個測試方法運行后必須會運行的方法。@b.close指關閉瀏覽器,這是watir中的API。
- test_Demo是測試方法,測試方法必須以test開頭,否則會不被當成測試方法,這樣即使在TestData.yaml中配置了,也不會被運行。
- test_Demo中被注釋的項都是在test_Demo中可以被使用的方法
AutoTest("") 調用Demo.yaml中頁面元素對象
TestData("") 調用TestData.yaml中的測試數據
ExpectData("") 調用ExpectedData.yaml中的期望值數據
ConfigData("") 調用conf.yaml中的配置數據
LoadObject("") 在腳本中加載其它的頁面元素對象文件,其文件只能是yaml文件,格式與Demo.yaml文件格式一樣,加載的頁面元素對象如果與Demo.yaml中一致時,則會覆蓋Demo.yaml文件中的數據。此時工作路徑在testcase\Demo下,如果要加載testcase\test1.yaml中的數據,則為LoadObject("../test1.yaml")
LoadTestData("") 在腳本中加載其它的測試數據文件,其文件只能是yaml文件,格式為:
hello: 123
加載的測試數據如果與TestData.yaml中 一致時,則會覆蓋TestData.yaml文件中的數據。此時工作路徑在testcase\Demo下,如果要加載testcase\test2.yaml中的數據,則為LoadTestData("../test2.yaml")
LoadExpectData("") 在腳本中加載其它的期望值數據文件,其文件只能是yaml文件,格式與ExpectedData.yaml文件格式一樣,加載的期望值數據如果與ExpectedData.yaml中 一致時,則會覆蓋ExpectedData.yaml文件中的數據。此時工作路徑在testcase\Demo下,如果要加載testcase\test3.yaml中的數據,則為LoadExpectData("../test3.yaml")
TransferData("") 在測試方法運行完成后,會在lib/transfer.yaml(如果是1.8版本的,則在config/transfer.yaml)中保存該測試方法的返回值,這樣在其它的project中可以調用:
TransferData("test_Demo")會返回測試方法test_Demo最后一次運行的返回值(TransferData("")還不太完善,處理方式還沒有想清楚,所以大家慎用)
assert_string("","",""),assert_array("","",""),assert_hash("","",""),assert_true(true,""),assert_false(false,"") 這是五個斷方方法,這五個方法中的最后一個參數可以為空,也可以不寫,其數據為自已添加,會反應在測試報告中,比如:assert_string("123","123","should be 123")
l "" 是指在測試方法中添加log,其會反應在測試報告中,比如 l "this is the first step","this is the first step"這一句話會顯示在測試報告中。
7.大家在使用的過程中,如果有任何疑問或建議,可直接聯系我(QQ群號:254285583),我會第一時間給出答案。希望大家試用愉快。共同學習,共同進步。
WAT示例
准備條件:
1.創建project "Demo"后,在testcase文件夾下會創建一個Demo的文件夾,Demo文件夾下會出現如下的文件:conf.yaml,Demo.rb,Demo.yaml,ExpectedData.yaml,TestData.yaml
2.我們創建一個登錄百度,並輸入值"Hello World",搜索,並在搜索出來的結果頁面的輸入框的值是否是"Hello World",最后關閉瀏覽器的示例。
開始寫用例:
1.寫Demo.yaml
input: text_field(:id,"kw")
button: button(:id,"su")
2.寫TestData.yaml
- common:
private: y
smoking: y
description: ""
- test_Demo:
description: test for baidu search
inputValue: Hello World
url: "http://baidu.com"
3.寫ExpectedData.yaml
inputValue: Hello World
4.寫Demo.rb
class Demo < TestKlass
def setUp
getWebApplication
end
def tearDown
@b.close
end
def test_Demo
#AutoTest("")
#LoadObject("")
#TestData("")
#ExpectData("")
#LoadTestData("")
#LoadExpectData("")
#TransferData("")
#ConfigData("")
#assert_string("","","")
#assert_array("","","")
#assert_hash("","","")
#assert_true(true,"")
#assert_false(false,"")
#l ""
l "this is a test for demo"
@b.goto TestData("url")
@b.wait
AutoTest("input").set TestData("inputValue")
AutoTest("button").click
assert_string(AutoTest("input").text,ExpectData("inputValue"),"Input value should be correct")
end
end
5.按上面的步驟寫完后,直接運行,在report/Demo文件夾下面,打開Demo.html,就可以看到對應的報告。
6.開始體驗吧!
----------------------------------------------------------------------------------------------