首页 > 代码库 > 自动化测试(五):自动化测试框架

自动化测试(五):自动化测试框架

框架(可复用的函数、方法)类型:

         ① 数据驱动(用测试数据去驱动脚本的运行,测试脚本和数据的分离???)

         ② 关键字驱动(object.action(param),抽象程度更高,业务逻辑、脚本、数据的分离)

结构:Automation

         ① AUT:配置文件,xml文件

         ② Config(Object Repository):对象,tsr文件(flighr.tsr)

         ③ TestData:测试数据,即测试用例,txt文件(flight_testcase.txt)

         ④ Script:测试脚本,读取Test Data,传给Execute Testcase,传给业务处理,得到结果,传给Report,vbs文件(testdriven_flight.vbs,flight文件夹)

         ⑤ Report

         ⑥ Log

         ⑦ Exception

         ⑧ Controller文件调用(runQTP.vbs,runtime.bat)

 

注:业务处理函数在QTP里面写,再复制

 

flight_testcase.txt

|mercury|Please enter agent name

mer|mercury|Agent name must be at least 4 characters long

|mercury|Please enter passwoed

mercury|mercury|null

 

testdriven_flight.vbs

Const Forreading = 1

Const ForWriting = 2

Const ForAppending= 8

 

’文件调用

Controller "C:\Automation_Flight\Config\login_flight.tsr","D:\Software\QTP10\samples\flight\app\flight4a.exe","C:\Automation_Flight\TestData\flight_testcase.txt"

’文件调用函数

Function Controller(BvVal tsrPath, ByVal appPath, ByVal datapath)

         RepositorysConnection.Add tsrPath

         Systemutil.Run appPath

         ExecuteTestCase datapath

         Window(“Flight Reservation”).close

End Function

 

’执行用例、写入测试报告函数

Function ExecuteTestCase(ByVal filepath)

         Dim fso, fil, txt, arr, test_result, result_record

         Set fso = CreateObject("Scripting.FileSystemObject")

         Set fil = fso.openTextFile(filepath, ForReading)

        

         Do while Not fil.AtEndOfStream

                  txt = Trim(fil.ReadLine)

                  arr = split(txt, "|")

                  test_result = Login(arr(0), arr(1), arr(2))

                  result_record = result_record & "username: " & arr(0) & ", password" & arr(1) & ", testresult ---->" & test_result & vbCrLf

         Loop

        

         WriteTestReport "C:\Automation\Report\testreport.txt", result_record

        

         fil.close

         set fil = nothing

         set fso = nothing

End Function

 

’报告函数

Function WriteTestReport(ByVal filepath, ByVal str)

         Dim fso, fil

         Set fso = CreateObject("Scripting.FileSystemObject")

         Set fil = fso.openTextFile(filepath, ForWriting, True)

         fil.Write str

         fil.close

         set fil = nothing

         set fso = nothing

end Function

 

’业务逻辑流程,业务处理函数在QTP里面写,再复制

Function Login(ByVal un, ByVal pw, ByVal errmsg)

         dialog("Login").WinEdit("Agent Name: ").Set un

         dialog("Login").WinEdit("Password:").Set pw

         dialog("Login").Winbutton("OK").Click

        

         If dialog("Login").Dialog("Flight Reservations").Exist Then

                  actuan_result = dialog("Login").Dialog("Flight Reservations").Static("errmsg").GetROProperty("text")

                  If action_result = errmsg Then

                           Login = "pass"

                  else

                           Login = "Fail"

                  End If

                  dialog("Login").Dialog("Flight Reservations").WinButton("确定").Click

         else

                  If window("Flight Reservations").exist and errmsg = "null" Then

                           Login = "Fail"

                  End If

         End If

End Function

 

在QTP中输入以下内容,保存在script下面的flight文件夹下

executeFile "C:\Automation\Script\testdriven_flight.vbs"

 

QTP自动化,AOM

runQTP.vbs

Dim qtpapp

 

Set qtpapp = CreateObject("QuickTest.Application")

qtpapp.Launch

qtpapp.visiable = True

qtpapp.open "C:\Automation\Script\flight"

qtpapp.Test.Run, True

qtpapp.Quit

 

set qtpapp = nothing

runtime.bat 批处理文件

at 11:54 /interactive cscript C:\Automation\Controller\runQTP

 

Exercise1windows计算器的简单自动化测试框架练习

结构:Automation_calc

         ① Config:windows计算器的对象文件tsr

         ② Controller:runQTP.vbs

         ③ Report:生成报告

         ④ Script:testdriven_clac.vbs  calc文件夹

         ⑤ TestData:testcase.xls

 

runQTP.vbs

Dim qtpapp

 

Set qtpapp = CreateObject("QuickTest.Application")

qtpapp.Launch

qtpapp.visible = True

qtpapp.open "C:\Automation_calc\Script\calc"

qtpapp.Test.Run, True

qtpapp.Quit

 

set qtpapp = nothing

 

testdriven_clac.vbs

Controller "C:\Automation_calc\Config\calc.tsr","C:\WINDOWS\system32\calc.exe","C:\Automation_calc\TestData\testcase.xls"

 

‘文件调用函数

Function Controller(ByVal tsrpath,ByVal apppath,ByVal datapath)

         RepositoriesCollection.Add tsrpath

         SystemUtil.Run apppath

         ExecuteTestCase datapath

         Window("计算器").Close

End Function

 

‘执行用例函数

Function ExecuteTestCase(ByVal filepath)

         dim xlapp, xlworkbook, xlsheet

         dim irowcount, iloop, num1,op,num2,expect_result,test_result, result_record

         set xlapp = createobject("excel.application")

         ‘xlapp.visible = true

         set xlworkbook = xlapp.workbooks.open(filepath)

         set xlsheet = xlworkbook.sheets("calc")

         irowcount = xlsheet.usedrange.rows.count

         for iloop = 2 to irowcount

                   num1 = xlsheet.cells(iloop, 1)

                   op = xlsheet.cells(iloop, 2)

                   num2 = xlsheet.cells(iloop, 3)

                   expect_result = xlsheet.cells(iloop, 4)

                   test_result = Calculation(num1,op,num2,expect_result)

                   result_record = result_record & num1 & op & num2 & "=" & expect_result & ",testresult---->" & test_result & vbcrlf

         next

        

         WriteTestReport "C:\Automation_calc\Report\testreport.txt", result_record

        

‘        xlworkbook.save

         xlworkbook.close

         xlapp.quit

         set xlsheet = nothing

         set xlworkbook = nothing

         set xlapp = nothing

End function

 

‘业务逻辑函数

Function Calculation(ByVal num1, ByVal op, ByVal num2, expect_result)

   Dim actual , expect

         window("计算器").WinEdit("Edit").Type(num1)

         window("计算器").WinButton(op).Click

         window("计算器").WinEdit("Edit").Type(num2)

         window("计算器").WinButton("=").Click

         actual_result = window("计算器").WinEdit("Edit").GetROProperty("text")

         actual = trim(actual_result)

         arr = split(actual,  ".")

         If arr(0) = trim(expect_result) Then

                   Calculation = "Pass"

         else

                   Calculation = "Fail"

         End If

End Function

 

‘报告函数

Function WriteTestReport(ByVal filepath, ByVal str)

         Dim fso, fil

         Set fso = CreateObject("Scripting.FileSystemObject")

         Set fil = fso.openTextFile(filepath, 2, True)

         fil.Write str

         fil.close

         set fil = nothing

         set fso = nothing

end Function

QTP输入以下语句,保存到C:\ Automation_calc\Script\calc文件夹下

executeFile "C:\Automation_calc\Script\ testdriven_clac.vbs"

 

testcase.xls

num1

op

num2

expect_result

20

*

3

60

35

-

5

30

40

+

21

61

55

/

11

5

自动化测试(五):自动化测试框架