Handling planned failure

While artfully dodging the label of ‘Negative Testing’, there are times when you want to check the error messages of a page for things like field validation rules, etc. In the Page Object pattern you could create two Actions; one that synchronizes correctly for success and one that synchronizes correctly for errors. But thats a bunch of unnecessary code — some of which is only going to be run in small percentage of scripts.

Instead, I prefer to put in a success flag on the the Action.

def go_to_some_page(self, success = True):
    if success:
        n = NewPageObject()
        return n

One of the more important parts of this is that the flag defaults to the main state — in this case success. This means you can call the Action without a parameter and get the default behaviour. It is only necessary to pass a parameter when you want the unusual behaviour.

And you don’t have to put the condition into the script — which is somewhere it really shouldn’t belong.

Some could argue that having


isn’t very descriptive in your scripts, but if you fall into this camp you could also use this syntax instead.

p.go_to_some_page(success = False)

Post a Comment

Your email is never published nor shared. Required fields are marked *