用scripting.dictionary实现简单的测试随机动作模型

在使用QTP做测试的时候,遇到一个问题,就是有一堆动作要随机执行,动作个数不确定并且动作执行的比率也是会调整的。

而实际上使用Scriptdictionary对象能很容易的解决该问题。

首先初始化数据:

 

Function initOpration() Set FavOprations = CreateObject("Scripting.Dictionary") FavOprations.Add "addRightMouse", "10" '通过右键菜单添加 FavOprations.Add "addHotKey", "20" '通过快捷键添加 …… End Function   

第二步建立一个函数,进行随机动作,当每次动作被随机到,那么后面的权值减1,当减到0时,从Dictionary 中删除。

 

Function generateOpration() Randomize If GroupBase.Count<1 Then Call initOpration() End If randScope = FavOprations.Count ‘通过keys()方法,能以数组的形式返回dictionary中的所有key的集合 a=FavOprations.Keys() selectNumber = Int(randScope * Rnd) strOpration = a(selectNumber) If FavOprations.Exists(strOpration)=True Then odlItem = FavOprations.Item(strOpration) FavOprations.Item(strOpration) = odlItem-1 If FavOprations.Item(strOpration) <1 Then FavOprations.Remove(strOpration) End If generateOpration=strOpration Else If FavOprations.Count <1 Then ‘如果全部动作已经随机完毕,那么重新初始化 Call initOpration() End If End If End Function   

最后我们可以根据每次随机到的动作别名然后映射到相应的执行函数,如:

Sub Operations(strOperation) Select Case strOperation Case "addRightMouse " DoaddRightMouse Case "addHotKey " DoaddHotKey …… End sub

你可能感兴趣的:(function,测试,Dictionary)