Application research of Pywinauto in Windows Twain driver Automation test

Source: Internet
Author: User

Abstract: based on Python, combining the specific requirements of the TWAIN Driver Test tool, the Pywinauto is introduced into the TWAIN driver automated test. The basic concept of Pywinauto is introduced, and the concrete implementation of Pywinauto in automated testing is explained by test case. The application results show that this method can greatly improve the automation degree of testing, greatly reduce the work of TWAIN driver Test, and ensure the quality of testing.
Key Words: Python;pywinauto;twain Driver; automated testing

The scanner driver under Windows mainly uses the TWAIN protocol [1]. Due to the complexity of the TWAIN protocol, TWAIN driver typically provides a number of scanning options for user use in a graphical interface. The large number of scanning options and the dependencies between options determine that TWAIN driver testing is a daunting task. The Twain driver tests mainly include basic functional testing and regression testing. Typically a TWAIN driver contains nearly hundreds of basic functional test cases and regression test cases that increase as the number of defects increases. In particular, when the official release date of the product, each correction of a defect, will bring a great deal of work: on the one hand, to do basic functional testing check whether there are new defects, on the other hand, to do the regression test before the corrected defects are affected. Because the product is at a later stage in the development cycle, the base of the defect is usually larger, and the test case for regression testing becomes very large. More importantly, if there are multiple defects to be corrected, then the above two aspects of the test will be repeated several times, the test will multiply.
In view of the above problems, based on Python and combining with the specific requirements of TWAIN Driver Test tool, this paper proposes a solution based on Pywinauto to realize automated testing [2]. Pywinauto reduces the manual operation of testers by simulating the mouse and keyboard operation of the tester on the user interface. The application results show that the proposed scheme can greatly improve the testing efficiency, reduce the testing time and ensure the quality of the products.
1 Basic concepts of Pywinauto
Pywinauto is a third-party extension package developed in Python for automated testing of script modules that enable automated testing of Windows graphical interfaces by sending mouse and keyboard actions to Windows dialogs and controls [3].
1.1 Identifying Application Instances
Pywinauto you first need to connect the application instance to a process before using it, there are two ways to identify the two types of identities:
(1) The application is not started and the application instance does not exist: you can call Start_ (self,cmd_line,timeout=app_start_timeout) to start the application. Examples are as follows:
Gappname=ur "C:\\Program Files\\twain working group\\ TWAIN toolkit\\twack_32.exe"
App=application. Application (). Start_ (Gappname)
(2) application started: Just call connect_ (self, **kwargs) to connect to the running application. Examples are as follows:
Appname=ur "Twain_32 twakcer"
App=application. Application (). Connect_ (title_re= AppName)
1.2 Identifying the application window
Once an application instance has been obtained, the instance can be used to identify the application window, with 3 main ways of identifying it:
(1) Use the window caption. Examples are as follows:
Gwizardname= "SELECT"
Maindlg=app[gwizardname]
or the window caption is used directly as a variable, but this identification, non-English language when the window will be problematic, so this method is not recommended. Examples are as follows:
Maindlg=app. Select
(2) The window caption is combined with regular expressions, especially useful when the window title is indeterminate or changes frequently. Examples are as follows:
Dlg=app.window_ (title_re= ". *doc", class_name= "#33888")
(3) Take the topmost window directly. You need to ensure that the application window that is identified is the top-level window. Examples are as follows:
Maindlg=app.top_window_ ()
1.3 Identifying the application window control
Pywinauto's testing principle primarily simulates manual manipulation on controls, so an important part of Pywinauto automated testing is identifying the controls on the application window. Suppose the application window has a button control with an OK content, there are two main ways to identify the control:
(1) Use the window control caption. Examples are as follows:
app["Dlg" ["OK"]
Or use the caption of the control directly as a variable. Similar to the Identity window, this method is not recommended. Examples are as follows:
App.dlg.OK
(2) Use friendly class, especially if the control header content is empty. Examples are as follows:
Dialog.button1
Note that the button, Button0, and Button1 represent the second button on behalf of the first Button,button2. Standard controls are easily identified by the friendly class, but the friendly class is not obvious for non-standard controls, which can be viewed in 1 by Visual Studio's own Spy + +.

1.4 How to manipulate the mouse and keyboard
After you have obtained Windows dialogs and controls, you can send a mouse, keyboard action to the dialog box or control to implement automated testing.
Mouse operation: (1) Click action: Analog mouse Click action can be combined with specific controls, Pywinauto for different controls provide different functions. For example, a click event that simulates a Next button can be represented as dialog. ["Next"]. Click (); The selection of the simulated ComboBox control can be used in the following way: Dialog.ComboBox1.Select (1). (2) Drag operation: The main use of Pressmouse (), Movemouse () and Releasemouse () to achieve mouse down, move and release operations. Examples are as follows:
def adjustsize (wizarddlg,shiftx,shifty):
offset=10
Orgrect=wizarddlg.wiacontrol1.rectangle ()
WizardDlg.WiaControl1.PressMouse (coords= (Offset,offset))
WizardDlg.WiaControl1.MoveMouse (coords= (offset+shiftx,offset+shifty))
WizardDlg.WiaControl1.ReleaseMouse ()
Key operation: Pywinauto uses SendKeys for key handling [4]. Some programs do not assign menu items to the main UI (such as word), so that you cannot use the menus method directly, but instead use the shortcut keys, which requires the use of SendKeys to send shortcut keys. To indicate that you press the ALT+F key combination, you can write Mainwin.typekeys ("%F"). It is important to note that Typekey can also be used to accept multiple combinations of keys at a certain time interval. For example, in Word2003 open a window that imports pictures from a scanner, you need to press Alt+i first, then press ALT+P, and then press Alt+s. The SendKeys can be expressed as:
Mainwin.typekeys ("%ips", pause=0.5)
1.5 Chinese support
Pywinauto when working with menus in Chinese, the Chinese app's dialogs and controls are often not identified due to coding problems. You can use the following two ways to resolve:
(1) Use "u" or "ur" to convert a string into a UTF-formatted string. For example:
Gdialogname=u "Select Source"
Gbuttonname=u "Selected"
App[gdialogname][gbuttonname]
(2) Use the Decode function to forcibly convert the string encoding. For example:
cp= "cp936"
Gdialogname= "Select Source". Decode (CP)
Gbuttonname= "selected". Decode (CP)
App[gdialogname][gbuttonname]
2 Twain Driver Automation test Implementation
Due to the wide use of the TWAIN protocol, there are already many applications that support the protocol. Common applications under Windows are twack_32, Microsoft Word, PageManager, and Adobe Photoshop, all of which can be used as test tools for TWAIN driver. Twack_32 is one of the tools that Twain officially provides, which is the best compatibility, and it not only provides the TWAIN application routines, but also installs a virtual image input device (Twain_32samplesource) on the computer system So testers usually use TWACK_32 to test TWAIN driver. This paper also takes twack_32 as an example to realize the automatic test of TWAIN driver.
2.1 twack_32 Start-up implementation
After downloading and installing, open the Twack_32 interface, then select File->select Source, which pops up a dialog box, shown in 2.

When implemented, first gets the application instance by invoking the start function, then takes the application instance with the title of the application Instances and interface (Twain_32 Twacker), and finally acquires the control identity based on the application window instance and then manipulates the control. The Chinese menu uses "U" to convert strings. The implementation code is as follows:
Appname=′twain_32 twacker′
TWAINDS_NAME=′SP c240sf/c242sf LAN 0.59 (32-32)
Def runtwack ():
App=application. Application ()
App.start_ (ur "C:\\Program files\\twain working group\\twain Toolkit\\twack_32.exe")
App[appname]. Wait (′ready′)
App[appname]. MenuSelect ("File->select Source ...")
app[u′ Select source]. Listbox1.select (Twainds_name)
app[u′ Select Source ′][u′ selected.] Click ()
Return app
2.2 Automated Test implementations
Twack_32 successful boot, if the driver needs to be tested, the test will appear the TWAIN driver interface, Figure 3 is a driver interface, the following implementation also take this driver as an example.

The driver supports three scanning modes: Full Color, gray and black and white. Suppose you need to test whether the scan mode of the driver is implemented correctly, first you need to change mode to full Color, the other parameters are the same, scan and observe the picture correctly, then change mode to gray and Black and white and repeat the above actions. Use Pywinauto to implement automated testing of scan patterns. As far as possible, each test case is implemented with a corresponding function, which facilitates the late maintenance of the test script. The sample code is as follows:
DRIVERNAME=′SP C240SF/C242SF lan′
def setscanparameter (Scanapp,npapersource,ncolormodelitem, Ndpiitem,nsizeindex):
Waitforwindowisready (Scanapp[drivername])
Scanapp[drivername]. Combobox.select (Npapersource)
Waitforwindowisready (Scanapp[drivername])
Scanapp[drivername]. Combobox2.select (Ncolormodelitem)
Scanapp[drivername]. Combobox3.select (Ndpiitem)
Scanapp[drivername]. Combobox4.select (Nsizeindex)
Scanapp[drivername]. Scan.click ()
Time.sleep (0.5)
def scanimagewithtwackchangemode (Scanapp, Ncolormodelitem)
Setscanparameter (scanapp,1, ncolormodelitem,1,2) def testcolormode (Scanapp)
Scanimagewithtwackchangemode (scanapp,1)
Scanimagewithtwackchangemode (scanapp,2)
Scanimagewithtwackchangemode (scanapp,3)
This paper introduces the application and implementation of Pywinauto in the automatic test of graphical interface with TWAIN driver as an example. Pywinauto reduces the manual operation of testers by simulating the mouse and keyboard actions of testers in the user interface. When implemented, each test case corresponds to a function and then the corresponding function is called according to the test requirements. Because the test case is relatively stable during the basic functional test, once the script is written, it is only necessary to run the script once before each release to complete the basic functional test. In the regression testing phase, each additional test case adds a corresponding implementation function to avoid missing the test of previous version defects. In addition, the scheme of this paper can be applied to testing of other Windows graphics applications only with a little modification, especially when the manual operation of the program interface is more complicated.
Reference Documents
[1] twain-standard for image acquisition devices[db/ol].http://twain.org.2001.
[2] Sinmingjie, Gao Jianhua. An improved GUI test framework DART[J]. Computer Engineering, 2009,35 (7): 55-58.
[3] Contents-pywinauto v0.4.1 Documentation[db/ol]. http://pywinauto.googlecode.com/hg/pywinauto/docs/contents.html,2010.
[4] sendkeys| Rutherfurd.net[db/ol]. http://www.rutherfurd.net/python/sendkeys,2008.

Turn from:

Http://www.hqew.com/tech/fangan/734726.html

Application research of Pywinauto in Windows Twain driver Automation test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.