Road to create test script using Pywinauto

Pywinauto is python based GUI automation tool, using this we can automate windows application.

Set up:

To set up pywinauto you need to install some prerequisite in you machine as mentioned below.
 1. Python
 2. SendKeys (SendKeys-0.3.win32-py2.6.exe)
 3. setuptools-0.6c11.win32-py2.6.exe.

Download pywinauto from link http://sourceforge.net/projects/pywinauto/files/ and unzip in your machine.
Open command prompt and go to unzip folder “pywinauto” and run command

 python setup.py install

To check you have it installed correctly


run Python
>>> import application
>>> app = application.Application().start_("notepad")
>>> app.notepad.TypeKeys("%FX")


Here is a simple (Notepad .py) notepad script of pywinauto.

   import time
   import unittest, time, re
   from pywinauto import application
   from pywinauto import findwindows
   class Notepad(unittest.TestCase):
     def setUp(self):       
        self.app = application.Application()       
        self.app.start_(ur"notepad.exe")
        self.app['Notepad'].Wait('ready')
     def test_Save_Notepad(self):      
        app = self.app       
        app.Notepad.MenuSelect("Help->About Notepad")
        time.sleep(2)
        app.AboutNotepad.OK.Click()       
        app.notepad.TypeKeys("pywin auto notepad script")       
        app['Notepad'].MenuSelect("File->Save As...")      
        app.SaveAs.FileNameEdit.SetEditText("TestScript.txt")       
        app.SaveAs.Save.Click()
        time.sleep(2)      
        if app.SaveAs.Yes.Exists():
            app.SaveAs.Yes.Click()      
        app['Notepad'].MenuSelect("File->Exit")
   if __name__ == "__main__":
      unittest.main()


run above test using command 

 python Notepad.py




4 comments:

  1. Hello,
    I have a script that tries to upload a file from my PC to a web application.I press via WebDriver the specific upload button in the browser and then a Win7 explorer window opens for me to navigate and select the desired file to upload.
    How could I manipulate this window with pywinauto?
    optional: could this be done in linux as well (with an appropriate library I suppose) ?

    ReplyDelete
    Replies
    1. In this type scenario, no need to use pywinauto, you can do this using webdriver.
      Just use sendKeys command on file element locator with path of your file.

      Delete
  2. Here is the scenario.

    1. Open microsoft lync.
    2. login to the lync
    3. make a call

    o From Conversation window

    o From Main Lync Window using dial-pad

    o From Contact-card
    oFrom Contact right-click context menu

    I am getting below ERROR message.

    Traceback (most recent call last):
    File "C:\Users\pkumarasami\Desktop\ShoreTel\Workspace\WinAuto\Lync.py", line 6, in
    app.Lync.Edit.SetEditText("parasuraman@gmail.com")
    File "C:\Python27\lib\site-packages\pywinauto-0.5.0-py2.7.egg\pywinauto\application.py", line 234, in __getattr__
    ctrls = _resolve_control(self.criteria)
    File "C:\Python27\lib\site-packages\pywinauto-0.5.0-py2.7.egg\pywinauto\application.py", line 806, in _resolve_control
    raise e.original_exception
    pywinauto.findbestmatch.MatchError: Could not find 'Lync' in '[u'Opening - MsoSplash', u'Opening - ', u'MsoSplash']'

    Here is the code:

    from pywinauto import application

    app = application.Application()
    app.Start_("C:\Program Files (x86)\Microsoft Office\Office15\lync.exe",10)
    print(app.process)
    app.Lync.Edit.SetEditText("parasuraman@gmail.com")

    ReplyDelete
  3. Please let me know the syntax for selecting Radio button (Remove) using the pywinauto

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution