|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 Quote:
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
Methinks you're looking for this documentation:
http://msdn.microsoft.com/library/d...application.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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
Actually I was mistaken on this point: it isn't leaving the Internet Explorer process open any more.
Quote:
|
|
#10
|
||||
|
||||
|
Quote:
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 ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Using Python to control windows |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|