#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12

    Using Python to control windows


    I need to be able to open an Internet Explorer window, give it an URL, maximize it, and bring it to the foreground, then run a command-line program that takes a screenshot of it.

    Can I control Windows Windows in Python? If not, what's a good language to use for this kind of stuff?

    I really need to test a bunch of webapps frequently, but it's not enough to test their HTTP status codes.

    -Antun
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Yes you can. To open a browser with a given URL you can use the webbrowser module in the standard library, which will launch the default browser for your operating system. You can run command line apps with the os.system or os.popen functions.

    For finer control of Windows, you will need to install the Windows extensions for Python, Win32All. This gives a comprehensive set of interfaces to the Windows API, including COM and a Python wrapper to the MFC class library. If you install the ActiveState version of Python then it comes with the Win32All extensions included.

    There are also extensions available for directly calling functions in any DLL. The most recent is ctypes, available at http://starship.python.net/crew/theller/ctypes/. There is also a module called calldll, but that is rather old and I think it is no longer being developed.

    between the above extensions, you can do pretty much anything in Python that you can do in VB or Visual C++. I frequently use Python to control other applications such as Word and Excel via COM, and you can also use it to create COM servers.

    If you are serious about Windows programming then I recommend the book 'Python Programming on Win32' by Mark Hammond and Andy Robinson.

    Regards,

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    By the way, testing apps by using screen captures is generally not the best way to go. You either have to examine the captures by eye, or do a file comparison with previous captures, in which case the slightest change will cause all the tests to break. You also only test the look of the page, not the functionality.

    A better alternative may be to capture the HTML and parse it to make sure that it conforms with what you expect. You can do this directly in Python, using urllib and the various parsers that are part of the standard library, but this can be tedious. Or you can use a web testing framework that does most of the work for you. There are a couple that I know of, but there are probably others:

    WebUnit: http://webunit.sourceforge.net/

    httpunit: http://www.httpunit.org/ This is a Java library, but it can be used from Jython. With it you can create an object acts like a browser, and can even execute a subset of Javascript if you have the Rhino Javascript library installed.

    Another option is to write your tests in Javascript and run them on the browser, using JS to open a page in a separate frame and interrogate the DOM. There are two JS unit testing frameworks available, confusingly both are called jsunit.

    http://www.edwardh.com/jsunit/

    http://jsunit.berlios.de/

    Both have their strengths and weaknesses, but I think the first one would be better suited to this task.

    Regards,

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12
    Thanks for your help! I'm looking into COM objects and Python at the very moment. It seems that that's what I need to be able to automate Internet Explorer. I've installed the Win32All package, and a co-worker happened to have the Python Programming on Win32 book already.

    I know what you're saying about screenshots not being ideal. Here's the problem - we have a presentation server that uses the Flash runtime as a rendering engine, so our apps don't have any HTML to parse in them. (You can see what I mean here: http://www.laszlosystems.com/demos/ ).

    Some of the apps rely on external HTTP data sources for content, (for example the Amazon one, which uses the Amazon XML web services). If it fails, the app won't look right.

    What I really want to know is if and how often there is a problem with the apps. I know I could check the HTTP data sources directly, but that's getting too deep in the implementation of each application I want to test.

    We do have WinRunner which is supposed to be able to take screenshots and compare them, but the problem is that it doesn't work very well at all, hence I'm trying to write something of my own.

    -Antun

    Originally Posted by DevCoach
    By the way, testing apps by using screen captures is generally not the best way to go.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12
    Actually, here's one thing I'm pretty stuck on:

    Code:
    # Launch Internet Explorer and make visible
    ie = win32com.client.Dispatch( "InternetExplorer.Application" )
    ie.Visible = 1
    Yay! But now where do I learn about the methods that I can call on ie? I searched http://msdn.microsoft.com/library/default.asp , but couldn't find anything that looked like it might be a summary. (I'm not familiar with MSDN docs at all). I tried the following in Python:

    Code:
    dir( ie.__class__ ) 
       and
    ie.__dict__.keys()
    ... but no luck there either. For example, the Visible attribute doesn't even appear.

    -Antun
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Methinks you're looking for this documentation:
    http://msdn.microsoft.com/library/de...pplication.asp

    This code works for me:
    Code:
    import win32com.client
    import win32api
    
    ie = win32com.client.Dispatch( "InternetExplorer.Application" )
    ie.Visible = 1
    ie.FullScreen = 1
    ie.Navigate("http://www.microsoft.com/")
    while ie.Busy == True:
        win32api.Sleep(1000)
    #ie = None
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Here is a useful tip when using COM objects from Python. If you run makepy on the type library first, then it not only speeds up access by creating stubs to the objects, but it also means that the methods become visible to Python. This includes any documentation defined in the type library. You can also access the properties with _prop_map_get_.

    Then you can do:

    Code:
    >>> import win32com.client
    >>> ie = win32com.client.Dispatch( "InternetExplorer.Application" )
    >>> dir(ie)
    ['CLSID', 'ClientToWindow', 'ExecWB', 'GetProperty', 'GoBack', 'GoForward', 'GoHome', 'GoSearch', 'Navigate', 'Navigate2', 'PutProperty', 'QueryStatusWB', 'Quit', 'Refresh', 'Refresh2', 'ShowBrowserBar', 'Stop', '_ApplyTypes_', '__call__', '__cmp__', '__doc__', '__getattr__', '__init__', '__int__', '__module__', '__repr__', '__setattr__', '__str__', '__unicode__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']
    >>> ie.__doc__
    'Web Browser Interface for IE4.'
    >>> ie.Navigate.__doc__
    'Navigates to a URL or file.'
    >>> ie._prop_map_get_.keys()
    ['ReadyState', 'Busy', 'Container', 'Silent', 'Top', 'RegisterAsDropTarget', 'LocationName', 'Application', 'Offline', 'Document', 'Type', 'ToolBar', 'MenuBar', 'FullScreen', 'Parent', 'TheaterMode', 'Path', 'Name', 'RegisterAsBrowser', 'StatusText', 'Left', 'TopLevelContainer', 'Resizable', 'Width', 'StatusBar', 'HWND', 'Height', 'Visible', 'FullName', 'LocationURL', 'AddressBar']>>>
    If you are using PythonWin and you have autocomplete and tooltips enabled, then it will also pop up information about the object as you type in the interactive window. This makes it very easy to experiment the COM objects.

    You can run makepy.py as a script from the command line or from the PythonWin Tools menu. The library for IE is called 'Microsoft Internet Controls'.

    Regards,

    Dave - The Developers' Coach
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12
    Thanks DevCoach and Scorpions4ever!

    That really helps! There's two more questions I have:

    1. How do I get to all the usual Windows methods, such as bringing the Internet Explorer window to the front and making it the active window? I can't find them amongst all the IE-specific ones. Is one of the IE properties a pointer to inherited shell properties?

    2. How do I quit the application? I tried:

    Code:
    ie.Quit()
    ... which does not throw an error, but IEXPLORE.exe is still present in the processes I see in the Task Manager.

    Thanks,

    Antun
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12
    Actually I was mistaken on this point: it isn't leaving the Internet Explorer process open any more.

    2. How do I quit the application? I tried:

    ie.Quit()

    ... which does not throw an error, but IEXPLORE.exe is still present in the processes I see in the Task Manager.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by karlovac
    1. How do I get to all the usual Windows methods, such as bringing the Internet Explorer window to the front and making it the active window? I can't find them amongst all the IE-specific ones. Is one of the IE properties a pointer to inherited shell properties?
    Use win32gui module for the window manipulation routines.
    Code:
    import win32com.client
    import win32api
    import win32gui
    
    ie = win32com.client.Dispatch( "InternetExplorer.Application" )
    ie.Visible = 1
    ie.Navigate("http://www.microsoft.com/")
    while ie.Busy == True:
        win32api.Sleep(1000)
    
    win32gui.BringWindowToFront(ie.HWND)
    Hope this helps
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0
    Originally Posted by Scorpions4ever
    Use win32gui module for the window manipulation routines.
    Code:
    import win32com.client
    import win32api
    import win32gui
    
    ie = win32com.client.Dispatch( "InternetExplorer.Application" )
    ie.Visible = 1
    ie.Navigate("http://www.microsoft.com/")
    while ie.Busy == True:
        win32api.Sleep(1000)
    
    win32gui.BringWindowToFront(ie.HWND)
    Hope this helps
    win32gui only has method BringWindowToTop(ie.HWND)
    below is my code
    ie = Dispatch("InternetExplorer.Application")
    ie.Navigate('http://www.microsoft.com')
    ie.Visible = 1
    if ie.Busy:
    win32api.Sleep(1000)
    win32gui.BringWindowToTop(ie.HWND)

    Even though I tried the above code, my IE is still not bring to the front and not always on top. Can anyone shed idea how to make the IE to the front?

IMN logo majestic logo threadwatch logo seochat tools logo