给学生讲解一个Web Automation Testing的例子,做了两个脚本。大家看看要是喜欢就顶一下,还有就是
转贴一定注明出处!
51Testing软件测试网ccA[ }jAH5w#}4i|
现在一淘的广告铺天盖地,决定用这个例子结合
QTP做个Demo。
先把题目的需求列出来:
打开一淘,在输入框输入“htc”,这时会弹出一个提示的下拉框(Ajax实现),共有10个项。用QTP实现将下拉框的内容全部打印出来(用print),并随机的选择一个进行购物搜索。
用两种方法实现,一种是基于对象库,一种是描述性编程。
.p;}z1?H.u0
常规解决思路:
肯定会想到加载 Web Add-in 来做,但是录制的时候会意外的发现后续的动作根本录不进去。特别是对象的抓取成为了一个棘手问题。好不容易抓到了对象,又不知道如何抓取里面哪些特征属性作为我们的识别依据。这时候困扰的问题就是没有办法知道HTML加载了Ajax之后的源代码(即Dynamic HTML Object)。
51Testing软件测试网#lE [
n^;e(v
对于查看DHTML Object这个问题,我建议安装
谷歌的Chrome浏览器,里面有个“审查元素”功能非常好用。当然用FireFox的FireBug或者IE的IE Developer也行。
51Testing软件测试网;Hw7{+R?k8MR
解决方案1 -- 基于对象库(下面只提供代码,不提供对象库和全套脚本,读者自行添加)
以下代码由songfun提供:
Dim iCount,iChild,sKeyword
SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
Browser("Browser").Page("Page").Sync
Browser("Browser").Navigate "http://www.etao.com/"
Setting.WebPackage("ReplayType") = 2
Browser("Browser").Page("Page").WebEdit("q").Set "htc"
' 请注意下对象库里的WebElement对象不能采用默认的innertext作为识别属性,而应该用html tag和class两个属性
iCount = Browser("Browser").Page("Page").WebElement("htc g11").Object.parentElement.parentElement.childNodes.length
Print iCount
i = RandomNumber(0,iCount-1)
sKeyword = Browser("Browser").Page("Page").WebElement("htc g11").Object.parentElement.parentElement.childNodes.item(i).firstChild.innerText
For Each iChild In Browser("Browser").Page("Page").WebElement("htc g11").Object.parentElement.parentElement.childNodes
Print iChild.firstChild.innerText
Next
Browser("Browser").Page("Page").WebEdit("q").Set sKeyword
Browser("Browser").Page("Page").WebEdit("q").Submit
Setting.WebPackage("ReplayType") = 1
yv?Q*g0
解决方案2 -- 基于描述性编程
以下代码由songfun提供:
Dim oDesc,oWebElement,sKeyword
SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
Browser(":=").Page(":=").Sync
Browser(":=").Navigate "http://www.etao.com/"
Setting.WebPackage("ReplayType") = 2
Browser(":=").Page(":=").WebEdit("html tag:=INPUT","name:=q").Set "htc"
Setting.WebPackage("ReplayType") = 1
Set Desc = Description.Create()
oDesc("micclass").value = "WebElement"
oDesc("html tag").value = "SPAN"
oDesc("class").value = "ks-suggest-key"
Set WebElement = Browser(":=").Page(":=").ChildObjects(oDesc)
iCount = oWebElement.Count
For iLoop = 0 To iCount-1
Print iLoop & " " & oWebElement(iLoop).object.innerHTML
Next
sKeyword = oWebElement(RandomNumber(0,iCount-1)).object.innerHTML
Browser(":=").Page(":=").WebEdit("html tag:=INPUT","name:=q").Set sKeyword
Setting.WebPackage("ReplayType") = 2
Browser(":=").Page(":=").WebEdit("html tag:=INPUT","name:=q").Submit
Setting.WebPackage("ReplayType") = 1
Set Desc = Nothing
Set WebElement = Nothing
xia
j1Y0
关于 解决方案 ,我这里只提供了两种,其实还有很多,只能算抛砖引玉。Kevin给了很多好点子,我这里没有给出他的代码和绝招(他的著作权),大家想要可以找他。这里也要特别感谢他在其中给出了很多好意见。
51Testing软件测试网"~#[4i&y0N
里面的代码需要结合HTML的源代码做参照,以下提供一淘的HTML源代码(rar附件)——注意,我这里的HTML代码是加在了Ajax之后的代码,不是你们在一淘网首页的HTML代码那么简单!
51Testing软件测试网|
y:a {`SH^s