[TIP] Python GUI testing

Michael Foord michael at voidspace.org.uk
Wed Apr 6 04:11:31 PDT 2011


On 04/04/2011 22:43, Gary Bernhardt wrote:
> On Mon, Apr 4, 2011 at 12:33 PM, Geoff Bache<geoff.bache at gmail.com>  wrote:
>> Right, but does that include internally set names? Things like Text
>> boxes, Trees and Tables
>> don't have a title or label, and in those cases it's pretty useful to be able
>> to explicitly name them and use that name in the tests. Also, sometimes several
>> widgets have the same label.
> Ahh, you're right. My tests would just directly address the text
> fields etc. by their order in the view. I feared this at first, but it
> didn't end up being a major problem for a few reasons:
>
> - AppleScript supplies the controls in an order that seems to always
> make sense. I think it was top-left to bottom-right, rather than some
> arbitrary database-natural order.
> - You shouldn't be creating views with tons of text boxes! ;)
> - My tests never directly named fields. They always called into a
> layer of abstraction on top of appscript. Instead of saying "fill in
> text field 1 with the path", they'd say
> "set_protected_folder_path(path)". Any control change would result in
> only one change to the tests because no control was named twice.
>
> Because of AppleScript/System Events' nice ordering, you could go one
> step further and write something like:
>
> def fill_in_text_box(label_name, text):
>      skip controls until you see a label with label_name
>      skip controls until you see any text field
>      put the text in whatever we have; it's the text field to the right
> of the target label
>
> It's the same basic principle as "find the HTML label with text
> 'username' and put the username in its corresponding input" you'd do
> in web UI testing, except it relies on stable control ordering instead
> of explicit linkage between the label and text field.
>

Right, but relying on ordering makes your tests fragile to UI changes - 
which is particularly an issue when you're doing TDD and adding features 
to an app. (I'm not questioning your assertion that it worked fine for 
you Gary - just making a general point.)

Walking the controls in a view until you find the right one (with the 
right contents for example) as you describe is a good way to mitigate 
against this. On the other hand being able to name controls and then 
find them in the view by name is a useful way of not being dependent on 
ordering. This was the approach (or one of the approaches) we took at 
Resolver Systems.

All the best,

Michael Foord


>> Interesting. A quick look at AppleScript made it look like the Mac is
>> somewhat further on in this respect than other platforms from what I
>> could tell.
> Yep, it's awesome. I was very surprised. It's existed for a long time, too.
>


-- 
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html




More information about the testing-in-python mailing list