谁说QTP不能多线程 - 当Python遇上QTP

news/2024/7/3 6:18:54

谁说QTP不能多线程 - 当Python遇上QTP

作者:Wally Yu (微博:http://weibo.com/quicktest)

经常有人问我一个问题:QTP可以同时做多个项目的自动化吗?我每次都回答说“不行,QTP不支持多线程,VBS本身就不是一门多线程的语言!”

最近在反思...QTP真的不能多线程吗?

好吧...如果一定要QTP可以多线程,咋办?我硬是想出了两个办法:

1. 需要在Windows可以多个QTP进程 - 可以吗?不行!有木有办法?没办法...

2. 需要QTP弃用VBS作为其脚本语言,改用多线程语言作为脚本语言(如Java、Python...) - 可以吗?不行!有木有办法?没办法...

且慢,假设QTP采用支持多线程的脚本语言,如何实现多线程?对,可以这样实现:

Function testScenario_1()

...

End Function


Function testScrnario_2()

...

End Function

...


Threading (testScenario_1).start()

Threading (testScenario_1).start()

...

如上结构可以实现QTP的多线程测试,这样岂不是大大提高了QTP的执行效率了么

可是回到现实中来,QTP的脚本语言是VBS,如何才能实现以上的愿望呢?呵呵,何不用AOM + DP!!!

(注:AOM - Automation Object Model; DP - Descriptive Programming)

以下代码采用Python,借鉴了“假装不在”的思路,用了Google和Yahoo搜索作为测试实例:

尝试一:

1. 打开两个IE,分别打开Google和Yahoo的首页

2. 将其Browser和Page的信息写入环境变量,并使其停止于断点

3. 打开Python编辑器,首先先把测试代码写入分别的Python函数:

注:函数首先调用QTP的AOM,而后取出环境变量的值,接着加上DP完成一条完整的测试语句

import thread, win32com.client
from time import ctime

def TestGoogle():
    shellWnd1 = win32com.client.DispatchEx('QuickTest.Application')
    P = shellWnd1.Test.Environment("google")
    print P.GetROProperty('title')
    P.WebEdit("name:=q").Set("Mercer")
    P.WebButton("name:=Google Search").Click()

def TestYahoo():
    shellWnd2 = win32com.client.DispatchEx('QuickTest.Application')
    Q = shellWnd2.Test.Environment("yahoo")
    print Q.GetROProperty("title")
    Q.WebEdit("html id:=p_13838465-p").Set("Mercer")
    Q.WebButton("html id:=search-submit").Click()

def main():
    print 'Start at:', ctime()
    TestGoogle()
    TestYahoo()
    
    print 'End at:', ctime()

if __name__ == '__main__':
    main()

4. 分别执行两个函数,测试其脚本是否正确


如上图,脚本被分别执行了,Python脚本正确无误,QTP也执行了相应的搜索操作

尝试一完成!


尝试二

这次就要把脚本放入多线程里面去执行了,同样的操作,以下是Python代码:

import threading
import win32com.client
import pythoncom
from time import ctime

class SearchTest(threading.Thread):
    def __init__(self, testObj):
        threading.Thread.__init__(self)
        self.testObj = testObj
        self.thread_stop = False

    def run(self):
        if not self.thread_stop:
            if self.testObj == "Google":
                print 'Google test start at '+ ctime()+"\n"
                pythoncom.CoInitialize()
                shellWnd1 = win32com.client.DispatchEx('QuickTest.Application')
                P = shellWnd1.Test.Environment("google")
                P.WebEdit("name:=q").Set("Mercer")
                P.WebButton("name:=Google Search").Click()
                pythoncom.CoUninitialize()
                print 'Google test finish at ' + ctime() + "\n"
            elif self.testObj == "Yahoo":
                print 'Yahoo test start at '+ctime()+"\n"
                pythoncom.CoInitialize()
                shellWnd2 = win32com.client.DispatchEx('QuickTest.Application')
                Q = shellWnd2.Test.Environment("yahoo")
                Q.WebEdit("html id:=p_13838465-p").Set("Mercer")
                Q.WebButton("html id:=search-submit").Click()
                pythoncom.CoUninitialize()
                print 'Yahoo test finish at ' + ctime() + "\n"
            else:
                print "input para error"
    def stop(self):
        self.thread_stop = True

def test():
    thread1 = SearchTest("Google")
    thread2 = SearchTest("Yahoo")
    thread1.start()
    thread2.start()
    #time.sleep(10)
    thread1.stop()
    thread2.stop()
    return

if __name__ == '__main__':
    test()

测试成功,下面是截图:


测试成功,皆大欢喜!

注:目前本人还没有发现这样操作有什么特别的优势。说节省测试时间,但是这样会造成调试上的极大不便。本博权当开拓一下眼界,抛砖引玉,愿大家集思广益 :)


http://www.niftyadmin.cn/n/3649264.html

相关文章

验证手机号码工具类

/*** 验证手机号码&#xff08;支持国际格式&#xff0c;86135xxxx...&#xff08;中国内地&#xff09;&#xff0c;00852137xxxx...&#xff08;中国香港&#xff09;&#xff09;* param mobile 移动、联通、电信运营商的号码段*<p>移动的号段&#xff1a;134(0-8)、1…

Android中使用隐藏API图文解析

Android SDK的很多API是隐藏的&#xff0c;我无法直接使用。但是我们通过编译Android系统源码可以得到完整的API。编译Android系统源码后可以在out\target\common\obj\JAVA_LIBRARIES目录可以看到它的所有API。当然对于一般情况&#xff0c;out\target\common\obj\JAVA_LIBRARI…

QTP测试框架之_报表

QTP测试框架之_报表 作者&#xff1a;Wally Yu (微博&#xff1a;http://weibo.com/quicktest) 自己在开发QTP测试框架的时候一些对于报表的经验&#xff1a; Excel报表&#xff1a; 下载Report Manager:http://www.advancedqtp.com/knowl ... ut-reportermanager/ 修改…

git克隆github_如何使用Git和Github分叉,克隆和推送更改

git克隆github介绍 (Introduction) Github is an excellent resource for finding useful code. In order to contribute changes back to a project, you’ll need to make a local copy and then upload your changes. In this tutorial, you’ll fork and clone an existing…

LBS模块架构的封装

对地图进行一个封装 public interface ILbsLayer {/*** 获取地图*/View getMapView();/*** 设置位置变化监听*/void setLocationChangeListener(CommonLocationChangeListener locationChangeListener);/*** 设置定位图标*/void setLocationRes(int res);/*** 添加&#xf…

[收藏]张翼轸:在Outlook的名义下

张翼轸&#xff1a;在Outlook的名义下 -------------------------------------------------------------------------------- http://www.sina.com.cn 2005年03月08日 14:48 ChinaByte   文/张翼轸Plaxo、Teleo、Skype这几个不同的SNS(Social Network Software&#xff0c;…

如何构建和安装Go程序

介绍 (Introduction) So far in our How To Code in Go series, you have used the command go run to automatically compile your source code and run the resulting executable. Although this command is useful for testing your code on the command line, distributing…

如何得到包含隐藏API的Android类库

Android SDK的很多API是隐藏的&#xff0c;我无法直接使用。但是我们通过编译Android系统源码可以得到完整的API库。编译Android系统源码后可以在out\target\common\obj\JAVA_LIBRARIES目录中有它所有API库(java)。当然对于一般情况&#xff0c;out\target\common\obj\JAVA_LIB…