Windows應用程式的自動化測試 : RobotFramework + AutoIT 的結合

Windows應用程式的自動化測試 : RobotFramework + AutoIT 的結合

 Image result for Windows AutoIT

安裝 Pywin32

pip install pywin32

https://github.com/mhammond/pywin32

安裝AutoItLibrary

http://code.google.com/p/robotframework-autoitlibrary/

或是

https://github.com/qitaos/robotframework-autoitlibrary

下載後解壓縮

Python    setup.py     install

PS. 如果是 64bit 電腦還需要額外安裝 AutoIT 64bit DLL, 可以透過安裝AutoIT 時安裝

安裝 AutoIT 套件

https://www.autoitscript.com/site/autoit/downloads/
https://www.autoitscript.com/site/autoit/downloads/https://www.autoitscript.com/site/autoit/downloads/https://www.autoitscript.com/site/autoit/downloads/https://www.autoitscript.com/site/autoit/downloads/
安裝完之後有幾個輔助工具
  • 1. 說明文件 AutoItX.chm
  • 2. AutoITlibrary.html   這個文件主要記錄 AutoIT RobotFramework 提供的關鍵字, 求於必須下載才可以閱讀, 因此筆者附錄在本篇文章
  • 3. Au3Info.exe 這個工具主要用來定位Windows UI 上元件資訊, 如下圖所示

Image result for Au3Info.exe

AutoIT 處理文件下載範例

*** Settings ***
Test Setup                  打開網頁
Test Teardown          關閉網頁
Library AutoItLibrary
Resource resource.txt
*** Test Cases ***
upload-case
Choose File               file                                    ${CURDIR}${/}text.rar
${file}                    Get Value                             file
Sleep                          3s
Download-testcase
判斷平台
Sleep                   3s
Click Link           下載測試
Sleep 3s
Win Wait              文件下載              \                    15
Win Activate        文件下載
Control Click             \                      \                   Button2
Sleep 3s
Win Wait                另存為                     \                 15
Win Activate              另存為
Control Set Text         \                            \               Edit1 ${CURDIR}${/}text2.rar
Control Click              \                             \               Button1
Sleep 3s
${confirm} Win Exists 确认另存为
Run Keyword If ${confirm}==1 Win Activate 确认另存为
Run Keyword If ${confirm}==1 Control Click \ \ Button1
Sleep                                4s
Win Wait                     下載完畢
Win Activate               下載完畢
Win Close                    下載完畢
Sleep 2s
  範例改編來源 https://github.com/qitaos/rf-demos/blob/master/AutoItDemo/tests/testsuite-updown.txt

附錄 AutoItLibrary – Documentation

https://github.com/qitaos/robotframework-autoitlibrary/blob/master/doc/AutoItLibrary.html

https://github.com/zheli/robotframework-autoitlibrary-forked/blob/master/doc/AutoItLibrary.html

Introduction

AutoItLibrary is a Robot Framework keyword library wrapper for for the freeware AutoIt tool (http://www.autoitscript.com/autoit3/index.shtml) using AutoIt’s AutoItX.dll COM object. The AutoItLibrary class provides a proxy for the AutoIt keywords callable on the AutoIt COM object and provides additional high-level keywords implemented as methods in this class.

In the following documentation on each keyword, the keywords whose documentation is simply method <KeywordName> (e.g. “method ControlClick“) are the native AutoIt keywords.  The detailed documentation for these keywords is available in the AutoItX documentation file: AutoItX.chm.  This file is installed as part of the installation of AutoItLibrary as:

C:\RobotFramework\Extensions\AutoItLibrary\AutoItX.chm

In order to discover the control identifiers in a given Windows GUI, AutoIt provides a standalone tool called the AutoIt Window Info Tool which is installed as part of the installation of AutoItLibrary as:

C:\RobotFramework\Extensions\AutoItLibrary\Au3Info.exe

This tool is documented here: http://www.autoitscript.com/autoit3/docs/intro/au3spy.htm

Importing

Arguments Documentation
OutputDir=., TimeOut=60, CaptureScreenOnError=False
OutputDir=<path> Output directory for captured screenshots. Should set to ${OUTPUTDIR}
Timeout=<seconds> Default TimeOut value in seconds.
This is used in other methods when their TimeOut parameter is not used.
CaptureScreenOnError=True Defaults to False.  Set to ${True} to capture the PC screen on any
AutoItLibrary keyword failure.

Shortcuts

Auto It Set Option  Block Input  CD Tray  Clip Get  Clip Put  Control Click  Control Command Control Disable

Control Enable  Control Focus  Control Get Focus  Control Get Handle Control Get Pos Height

Control Get Pos Width  Control Get Pos X  Control Get Pos Y  Control Get Text

Control Hide  Control List View  Control Move  Control Send  Control Set Text

Control Show Control Tree View  Drive Map Add  Drive Map Del  Drive Map Get

Get Active Window Image Get Auto It Version

Get Screen Image  Get Version  Ini Delete  Ini Read  Ini Write  Init  Is Admin Mouse Click  Mouse Click Drag  Mouse Down  Mouse Get Cursor

Mouse Get Pos X  Mouse Get Pos Y Mouse Move  Mouse Up  Mouse Wheel  Opt  Pixel Checksum

Pixel Get Color  Pixel Search Process Close  Process Exists

Process Set Priority  Process Wait  Process Wait Close  Reg Delete Key Reg Delete Val  Reg Enum Key  Reg Enum Val  Reg Read  Reg Write

Run  Run As Set  Run Wait  Send Shutdown  Statusbar Get Text  Tool Tip  Wait For Active Window  Win Activate  Win Active  Win Close

Win Exists  Win Get Caret Pos X  Win Get Caret Pos Y  Win Get Class List  Win Get Client Size Height Win Get Client Size Width  Win Get Handle

Win Get Pos Height  Win Get Pos Width  Win Get Pos X Win Get Pos Y  Win Get Process  Win Get State  Win Get Text  Win Get Title

Win Kill  Win List Win Menu Select Item  Win Minimize All  Win Minimize All Undo  Win Move  Win Set On Top Win Set State

Win Set Title  Win Set Trans  Win Wait  Win Wait Active  Win Wait Close Win Wait Not Active

Keywords

Keyword Arguments Documentation
Auto It Set Option strOption=, nValue= method AutoItSetOption
Block Input nFlag= method BlockInput
CD Tray strDrive=, strAction= method CDTray
Clip Get method ClipGet
Clip Put strClip= method ClipPut
Control Click strTitle=, strText=, strControl=, strButton=LEFT, nNumClicks=1, nX=-2147483647, nY=-2147483647 method ControlClick
Control Command strTitle=, strText=, strControl=, strCommand=, strExtra= method ControlCommand
Control Disable strTitle=, strText=, strControl= method ControlDisable
Control Enable strTitle=, strText=, strControl= method ControlEnable
Control Focus strTitle=, strText=, strControl= method ControlFocus
Control Get Focus strTitle=, strText= method ControlGetFocus
Control Get Handle strTitle=, strText=, strControl= method ControlGetHandle
Control Get Pos Height strTitle=, strText=, strControl= method ControlGetPosHeight
Control Get Pos Width strTitle=, strText=, strControl= method ControlGetPosWidth
Control Get Pos X strTitle=, strText=, strControl= method ControlGetPosX
Control Get Pos Y strTitle=, strText=, strControl= method ControlGetPosY
Control Get Text strTitle=, strText=, strControl= method ControlGetText
Control Hide strTitle=, strText=, strControl= method ControlHide
Control List View strTitle=, strText=, strControl=, strCommand=, strExtra1=, strExtra2= method ControlListView
Control Move strTitle=, strText=, strControl=, nX=, nY=, nWidth=-1, nHeight=-1 method ControlMove
Control Send strTitle=, strText=, strControl=, strSendText=, nMode=0 method ControlSend
Control Set Text strTitle=, strText=, strControl=, strControlText= method ControlSetText
Control Show strTitle=, strText=, strControl= method ControlShow
Control Tree View strTitle=, strText=, strControl=, strCommand=, strExtra1=, strExtra2= method ControlTreeView
Drive Map Add strDevice=, strShare=, nFlags=0, strUser=, strPwd= method DriveMapAdd
Drive Map Del strDevice= method DriveMapDel
Drive Map Get strDevice= method DriveMapGet
Get Active Window Image FilePath Capture an image of the active window into the given FilePath. The given FilePath must be relative to Robot Framework output directory, otherwise the embedded image will not be shown in the log file.
Get Auto It Version Returns a string with the version of the AutoItX COM object.
Get Screen Image FilePath Capture a full screen image into the given FilePath. The given FilePath must be relative to Robot Framework output directory, otherwise the embedded image will not be shown in the log file.
Get Version Returns a string with the version of the AutoItLibrary.
Ini Delete strFilename=, strSection=, strKey= method IniDelete
Ini Read strFilename=, strSection=, strKey=, strDefault= method IniRead
Ini Write strFilename=, strSection=, strKey=, strValue= method IniWrite
Init method Init
Is Admin method IsAdmin
Mouse Click strButton=LEFT, nX=-2147483647, nY=-2147483647, nClicks=1, nSpeed=-1 method MouseClick
Mouse Click Drag strButton=, nX1=, nY1=, nX2=, nY2=, nSpeed=-1 method MouseClickDrag
Mouse Down strButton=LEFT method MouseDown
Mouse Get Cursor method MouseGetCursor
Mouse Get Pos X method MouseGetPosX
Mouse Get Pos Y method MouseGetPosY
Mouse Move nX=, nY=, nSpeed=-1 method MouseMove
Mouse Up strButton=LEFT method MouseUp
Mouse Wheel strDirection=, nClicks=1 method MouseWheel
Opt strOption=, nValue= method Opt
Pixel Checksum nLeft=, nTop=, nRight=, nBottom=, nStep=1 method PixelChecksum
Pixel Get Color nX=, nY= method PixelGetColor
Pixel Search nLeft=, nTop=, nRight=, nBottom=, nCol=, nVar=0, nStep=1 method PixelSearch
Process Close strProcess= method ProcessClose
Process Exists strProcess= method ProcessExists
Process Set Priority strProcess=, nPriority= method ProcessSetPriority
Process Wait strProcess=, nTimeout=0 method ProcessWait
Process Wait Close strProcess=, nTimeout=0 method ProcessWaitClose
Reg Delete Key strKeyname= method RegDeleteKey
Reg Delete Val strKeyname=, strValuename= method RegDeleteVal
Reg Enum Key strKeyname=, nInstance= method RegEnumKey
Reg Enum Val strKeyname=, nInstance= method RegEnumVal
Reg Read strKeyname=, strValuename= method RegRead
Reg Write strKeyname=, strValuename=, strType=, strValue= method RegWrite
Run FileName, WorkingDir=, Flag= Direct wrapper for AutoIt’s Run method.

This is required in order to do error code translation into exceptions for Robot Framework.

Run As Set strUser=, strDomain=, strPassword=, nOptions= method RunAsSet
Run Wait strRun=, strDir=, nShowFlags=1 method RunWait
Send strSendText=, nMode=0 method Send
Shutdown nFlags= method Shutdown
Statusbar Get Text strTitle=, strText=, nPart=1 method StatusbarGetText
Tool Tip strTip=, nX=-2147483647, nY=-2147483647 method ToolTip
Wait For Active Window WindowTitle, WindowText=, TimeOut=-1 Wait up to TimeOut seconds for the window with the given WindowTitle and optional WindowText to appear. Force this to be the active window after it appears.  Optionally do a full screen capture on failure.

Parameters:

WindowTitle=<string> Title of the application window expected to appear
[WindowText=<string>] Optional text on the window expected to appear
[TimeOut=<seconds>] Optional overide to the default timeout set in __init__
Win Activate strTitle=, strText= method WinActivate
Win Active strTitle=, strText= method WinActive
Win Close strTitle=, strText= method WinClose
Win Exists strTitle=, strText= method WinExists
Win Get Caret Pos X method WinGetCaretPosX
Win Get Caret Pos Y method WinGetCaretPosY
Win Get Class List strTitle=, strText= method WinGetClassList
Win Get Client Size Height strTitle=, strText= method WinGetClientSizeHeight
Win Get Client Size Width strTitle=, strText= method WinGetClientSizeWidth
Win Get Handle strTitle=, strText= method WinGetHandle
Win Get Pos Height strTitle=, strText= method WinGetPosHeight
Win Get Pos Width strTitle=, strText= method WinGetPosWidth
Win Get Pos X strTitle=, strText= method WinGetPosX
Win Get Pos Y strTitle=, strText= method WinGetPosY
Win Get Process strTitle=, strText= method WinGetProcess
Win Get State strTitle=, strText= method WinGetState
Win Get Text strTitle=, strText= method WinGetText
Win Get Title strTitle=, strText= method WinGetTitle
Win Kill strTitle=, strText= method WinKill
Win List strTitle=, strText= method WinList
Win Menu Select Item strTitle=, strText=, strItem1=, strItem2=, strItem3=, strItem4=, strItem5=, strItem6=, strItem7=, strItem8= method WinMenuSelectItem
Win Minimize All method WinMinimizeAll
Win Minimize All Undo method WinMinimizeAllUndo
Win Move strTitle=, strText=, nX=, nY=, nWidth=-1, nHeight=-1 method WinMove
Win Set On Top strTitle=, strText=, nFlag= method WinSetOnTop
Win Set State strTitle=, strText=, nFlags= method WinSetState
Win Set Title strTitle=, strText=, strNewTitle= method WinSetTitle
Win Set Trans strTitle=, strText=, nTrans= method WinSetTrans
Win Wait WindowTitle, WindowText=, TimeOut=-1 Direct wrapper for AutoIt’s WinWait method.

This is required in order to do return code translation into exceptions for Robot Framework. On failure, optionally captures the full screen image to FAIL_WinWait_<n>.png.

Win Wait Active WindowTitle, WindowText=, TimeOut=-1 Direct wrapper for AutoIt’s WinWaitActive method.

This is required in order to do return code translation into exceptions for Robot Framework. On failure, optionally captures the full screen image to FAIL_WinWaitActive_<n>.png.

Win Wait Close WindowTitle, WindowText=, TimeOut=-1 Direct wrapper for AutoIt’s WinWaitClose method.

This is required in order to do return code translation into exceptions for Robot Framework. On failure, optionally captures the full screen image to FAIL_WinWaitClose_<n>.png.

Win Wait Not Active strTitle=, strText=, nTimeout=0 method WinWaitNotActive

Leave a Reply

Your email address will not be published.