Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, May 30, 2008

Testing OSX applications with Python

Recently I started a new wxPython based project that would be primarily be running on OSX.

I figured it would be rather useful and neat to have some tests managed by Python that would exercise the whole system so I took a stab at setting some up using some of my standard toolkit. You don't have to test exclusively Python-based application. You can use Python to write the test scripts for the application under test which could be written in any language that OSX supports.

How

I found out that Apple has already added all the hooks for scripting the UI via an AppleScript service called GUI Scripting or System Events. You can script it easily via AppleScript. Or you can access it all from Python using the appscript module.

It's Easy

Here's some code to get you started testing with GUI Scripting.
from appscript import *
import time

# some boilerplate setup
sysevents = app('System Events')
app('Finder').activate()
sysevents.processes['Finder'].menu_bars[1].menus[u'Apple'].menu_items[u'About This Mac'].click()

# some operations may need a pause
time.sleep(1)

loginwindow = sysevents.processes['loginwindow']

# Check that an About this Mac window exists
assert [ win for win in loginwindow.windows() if win.title() == 'About This Mac']

# Check that there is an All Rights Reserved present.
assert loginwindow.windows['About This Mac'].static_texts['All Rights Reserved.'].get()
The example is a little convoluted because Finder owns the "About This Mac" menu entry, but loginwindow owns the window that pops up.
The hardest part was figuring out the API to work with Apple's UI scripting support. Especially since all of Apple's examples were in AppleScript. Thankfully theres some tools that come with appscript that help. One called ASDictionary that autogenerates documentation for the various AppleScript plugins on your system. Also ASTranslate helps with the job of translating AppleScript code into equivalent appscript usage under Python.

Is that all?

There don't seem to be many Open Source GUI Functional Testing tools for OSX. Anyone know any ? Lots for Web functional testing but not for UI.

There is pywinauto for Windows.

Might be nice if OSX had a similar test layer. pymacauto anyone?

Monday, October 15, 2007

buildout recipe for installing Selenium RC

This weekend at the Plone Naples Sprint one of the things I worked on was a buildout to download and install Selenium RC. It is now available from PYPI under the name collective.recipe.seleniumrc For some reason the Selenium RC distribution zip file contains version numbers on each subdirectory so this meant I couldn't just use one of the existing download scripts. But I didn't want to re-invent the download recipe wheel, so I decided to base this recipe on a well-written existing download recipe, hexagonit.recipe.download. I asked on IRC if the author was present at the sprint and ironically enough he was sitting in the same room as me only two rows away ;) With some simple changes to his recipe I was able to derive collective.recipe.seleniumrc from it and gain features like download caching and md5 file hash verification. Along the way I learned that if you want your buildout recipe to be derived by others you should make it a new-style Python class so that super() works. Also that it helps to factor your install() method into submethods for overriding. The changes I made to hexagonit.recipe.download were cleaned up and integrated into a 1.1.0 release that we made yesterday, along with the 0.1 release of collective.recipe.seleniumrc If you want to try out the Selenium RC recipe just create a simple buildout.cfg:
[buildout]
parts=seleniumrc

[seleniumrc]
recipe=collective.recipe.seleniumrc
After you are finished running this buildout you will get a nice wrapper script in your bin folder named by the part, in this case seleniumrc. Happy testing!

Thursday, April 19, 2007

Zope 3 Book Arrived!

The book Web Component Development with Zope 3, 2nd Ed. by Philipp von Weitershausen has been on my wishlist for a while now and I finally got around to ordering it. I only got it last night and already I've made it half way through, though certainly I will re-read it all several times. The book offers very excellent descriptions of the component architecture and its applications throughout the rest of the Zope 3 framework. The progression of chapters in the book shows the reuse of the basic building blocks of interfaces, adapters & utilities. And one of the things I also liked was the "Rocky says" section where the relevant technologies of J2EE were constrasted with those of Zope. The explanation of ZCML in the book finally made it clear to me. ZCML is on purpose ;) It forces a separation of roles and make sure that things that would be considered 'configuration' are easily accessible to a non-programmer like a system administrator or integrator. My impression so far the from the book is one that really showcases Zope 3 as a enterprise-grade framework that is solidly Pythonic. For more info you can check' the website for Web Component Development with Zope 3, 2nd Ed.

Friday, February 23, 2007

Blogging live from PyCon 2007

I finally made it! Got switched to another hotel but I will be in the right place for tonight. Immediately I ran into a couple familiar faces from last October's Plone Conference, Chris Calloway and Josten Ma. Now I'm sitting in the main room waiting for the morning's keynote to start. More to come... Update Well I didn't end up blogging much at Pycon this year. But I have some pretty wonderful excuses. ;)