SilkTest天龍八部系列1-初始化和構造函數 軟件測試
SilkTest沒(méi)有提供專(zhuān)門(mén)的構造函數機制,但是在類(lèi)對象生成的過(guò)程中,會(huì )先初始化在類(lèi)中申明的變量。我們可以在初始化該變量的時(shí),
調用某些函數完成對象初始化工作,看上去好像是調用了構造函數一樣。不過(guò)要記住的是,這只是用來(lái)模擬構造函數而已。
下面是一個(gè)例子:view plaincopy to clipboardprint?01.[-] winclass myClass 02. [ ] boolean bConstructed = Constructor (SubStr (WindowTag(this), 2)) 03. [ ] string sSetMe 04. [ ] integer i 05. [ ] 06. [-] boolean Constructor (string sConstructorData) 07. [-] if (!IsSet(bConstructed)) 08. [ ] sSetMe = sConstructorData 09. [ ] return true 10. [ ] 11. [-] property SetMe 12. [-] string Get() 13. [ ] return sSetMe 14.[ ] 15.[ ] // test that the myClass constructor works 16.[-] testcase testClass () appstate none 17. [ ] window myObject = myClass ("Set property") 18. [ ] print(WindowTag(myObject)) 19. [ ] print ("myObject.SetMe = {myObject.SetMe}") 20. [ ] print ("myObject.i = {myObject.i}") [-] winclass myClass
[ ] boolean bConstructed = Constructor (SubStr (WindowTag(this), 2))
[ ] string sSetMe
[ ] integer i
[ ]
[-] boolean Constructor (string sConstructorData)
[-] if (!IsSet(bConstructed))
[ ] sSetMe = sConstructorData
[ ] return true
[ ]
[-] property SetMe
[-] string Get()
[ ] return sSetMe
[ ]
[ ] // test that the myClass constructor works
[-] testcase testClass () appstate none
[ ] window myObject = myClass ("Set property")
[ ] print(WindowTag(myObject))
[ ] print ("myObject.SetMe = {myObject.SetMe}")
[ ] print ("myObject.i = {myObject.i}")
我們來(lái)分析一下這段代碼。
一開(kāi)始聲明了一個(gè)myClass類(lèi),他有三個(gè)變量,其中bConstructed的初始化調用了類(lèi)的一個(gè)成員函數Constructor(),這個(gè)函數其實(shí)
可以叫任何名字。所以,window myObject = myClass ("Set property")這句直接會(huì )導致
成員函數Constructor()被調用。那么,為什么要給該函數傳一個(gè)參數SubStr (WindowTag(this), 2)進(jìn)去呢,這需要了解一下
WindowTag()的意義,WindowTag()會(huì )返回對象的tag,而不管該對象是否存在。一般的Tag都是以斜線(xiàn)/開(kāi)頭的,
window myObject = myClass ("Set property")這句的意思你就可以理解為(其實(shí)這不太容易理解,沒(méi)辦法,4Test當初就是這么設計的)
獲取對象"Set property"的Tag,它的Tag就是"/Set property"。所以在初始化bConstructed的過(guò)程中,同時(shí)初始化了成員變量
bConstructed,而成員變量i并沒(méi)有被初始化,所以當你運行整個(gè)腳本的時(shí)候,會(huì )得到如下的輸出:
view plaincopy to clipboardprint?01.[-] Testcase testClass - 1 error 02. [ ] /Set property 03. [ ] myObject.SetMe = Set property 04. [ ] *** Error: Variable (i) has not been set 05. [ ] Occurred in testClass at test3.t(20)
文章來(lái)源于領(lǐng)測軟件測試網(wǎng) http://kjueaiud.com/