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.
<pre lang="python">def go_to_some_page(self, success = True):
self.se.click(locators['some_locator'])
if success:
n = NewPageObject()
n.wait_until_loaded()
return n
else:
self.wait_for_element_present(locators['some_other_locator'])
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
<pre lang="python">p.go_to_some_page(False)
isn’t very descriptive in your scripts, but if you fall into this camp you could also use this syntax instead.
<pre lang="python">p.go_to_some_page(success = False)