Wednesday, November 28, 2012

Our first Autopilot testcase

So last time we learned some basics for autopilot testcases. We're going to use the same code branch we pulled now to cover writing an actual testcase.

bzr branch lp:~nskaggs/+junk/autopilot-walkthrough

As a practical example, I'm going to convert our (rather simple and sparse) firefox manual testsuite into an automated test using autopilot. Here's a link to the testcase in question.

If you take a look at the included firefox/test_firefox.py file you should recognize it's basic layout. We have a setup step that launches firefox before each test, and then there are the 3 testcases corresponding to each of the manual tests. The file is commented, so please do have a look through it. We utilize everything we learned last time to emulate the keyboard and mouse to perform the steps mentioned in the manual testcases. Enough code reading for a moment, let's run this thing.

autopilot run firefox

Ok, so hopefully you had firefox launch and run through all the testcases -- and they all, fingers-crossed, passed. So, how did we do it? Let's step through the code and talk about some of the challenges faced in doing this conversion.

Since we want to test firefox in each testcase, our setUp method is simple. Launch firefox and set the focus to the application. Each testcase then starts with that assumption. Inside test_browse_planet_ubuntu we simply attempt to load a webpage. Our assertion for this is to check that the application title changes to "Planet Ubuntu" - - in other words that the page loaded. The other two testcases expand upon this idea by searching wikipedia and checking for search suggestions.

The test_search_wikipedia method uses the keyboard shortcut to open the searchbar, select wikipedia and then search for linux. Again, our only assertion for success here is that the page with a title of Linux and wikipedia loaded. We are unable to confirm for instance, that we properly selected wikipedia as the search engine (although the final assertion would likely fail if this was not the case).

Finally, the test_google_search_suggestions method is attempting to test that the "search suggestions" feature of firefox is performing properly. You'll notice that we are missing the assertion for checking for search suggestions while searching. With the knowledge we're gained up till now, we don't have a way of knowing if the list is generated or not. In actuality, this test cannot be completed as the primary assertion cannot be verified without some way of "seeing" what's happening on the screen.

In my next post, I'll talk about what we can do to overcome the limitations we faced in doing this conversion by using "introspection". In a nutshell by using introspection, autopilot will allow us to "see" what's happening on the screen by interacting with the applications data. It's a much more robust way of "seeing" what we see as a user, rather than reading individual screen pixels. With any luck, we'll be able to finish our conversion and look at accomplishing bigger tasks and tackling larger manual testsuites.

I trust you were able to follow along and run the final example. Until the next blog post, might I also recommend having a look through the documentation and try writing and converting some tests of your own -- or simply extend and play around with what you pulled from the example branch. Do let me know about your success or failure. Happy Testing!

1 comment:

  1. I had to modify test_firefox.py to get it to work on Ubuntu 14.10

    from autopilot.testcase import AutopilotTestCase
    from autopilot.matchers import Eventually
    from testtools.matchers import Equals, Contains

    #register firefox as an application so we can call it
    AutopilotTestCase.register_known_application("Firefox", "firefox.desktop", "firefox")

    class FirefoxTests(AutopilotTestCase):

    def setUp(self):
    super(FirefoxTests, self).setUp()
    self.app = self.start_app_window("Firefox")

    #make sure firefox is up and loaded
    self.app.set_focus()
    self.assertTrue(self.app.is_focused)


    BECAME:

    from autopilot.testcase import AutopilotTestCase
    from autopilot.matchers import Eventually
    from testtools.matchers import Equals, Contains



    class FirefoxTests(AutopilotTestCase):

    def setUp(self):
    super(FirefoxTests, self).setUp()

    if "Firefox" not in self.process_manager.KNOWN_APPS:
    self.process_manager.register_known_application("Firefox", "firefox.desktop", "firefox")

    self.app = self.process_manager.start_app_window("Firefox")

    #make sure firefox is up and loaded
    self.app.set_focus()
    self.assertTrue(self.app.is_focused)


    ReplyDelete