Save 37% off PRO during our Black Friday Sale! »

How to write robust and easy to maintain UI tests with Python, selenium and splinter

How to write robust and easy to maintain UI tests with Python, selenium and splinter

Robustness tricks, page object model pattern, UI tests for form or CMS-like applications

D93eeac7e2b80a01bd1e1a595bac88a8?s=128

Davide Moro

April 08, 2017
Tweet

Transcript

  1. RESTRICTED PROPRIETARY INFORMATION The information disclosed herein is the exclusive

    property of Tierra and is not to be disclosed without the written consent of Tierra. No part of this publication may be reproduced or transmitted in any form or by any means including electronic storage, reproduction, execution or transmission without the prior written consent of Tierra. The recipient of this document by its retention and use agrees to respect the security status of the information contained herein. This document is intended for limited circulation. The information contained in this document is subject to change without notice and should not be construed as a commitment by Tierra unless such commitment is expressly given in a covering document. PYTHON PYTEST SELENIUM SPLINTER 2017/04/08 Floren ce ROBUST UI TESTS
  2. We are talking about... UI tests

  3. Rules of thumb • Slow (we are on the top

    of testing pyramid!!) • Flaky (unless you adopt some robustness techniques) • Hard to maintain when application changes (unless you adopt a Page Object Model pattern and parametrized selectors) • On fail, it’s hard to detect if it is a real problem and which is the affected component So: • Don’t write too many GUI tests! But • If you are working on legacy front end applications you might consider to increase a bit the GUI based tests coverage if it’s hard to write tests con UI components (not optimal) UI tests
  4. • Great tips from here by @davehunt (Mozilla) for test

    robustness: • https://github.com/mozilla/normandy/issues/625 Some robustness tips
  5. PyPOM can help you! Page Object Model pattern! • Apply

    OO programming to your web pages under test • Support for selenium waits (avoid sleeps and networking issues) • Organize your page in component and subcomponents (regions) • Support for dynamic regions (eg: list of CRUD items) • Delegate logics on regions • Add on your page object class selectors as class attributes • Add on your page object class methods • Divide et conquera approach If you are using Python: • PyPOM (Mozilla) is your friend (http://pypom.readthedocs.io/en/latest/) • Supports both selenium and splinter (https://splinter.readthedocs.io/en/latest/)
  6. • page.add_item().update(**{‘name’: ‘a name’}).save() • listing_region = page.listing_region • listing_region.filter_text

    = ‘QA-’ • listing_region.filter() # show only QA- items • listing_region.items[-1].delete() • for item in listing_region.items[0:2:]: item.delete().confirm() • listing_region.items[0].name == ‘QA-043...’ Regions and dynamic regions
  7. • pypom_form is based on PyPOM • Add an abstraction

    for dealing with page with forms or CMS-like web applications • Declarative way (based on colander - http://docs.pylonsproject.org/projects/colander/en/latest/) • You declare your edit form schema describing your form composition (for each form item you specify the field name, field type and widget) What pypom_form does: • Provides an easy way to set or retrieve the form field value • Provides API for fine grained interaction with the underlying widget Basically pypom_form hides complexity and makes interation with UI element easy! PyPOM for form tests: pypom_form!
  8. • pypom_form benefits • Simpler selectors, they are relative to

    the widget root • No more long list of selectors on page object class • Great way to deal with complex widgets (like list of dictionary data or datetimes). It makes hard things easier • If you provide the implementation for all your application widgets for your CMS- form based like application, you will speed up your testing process. Productivity increases! • Clearner code on Page Object model classes, only business logic methods! • pypom_form widgets are regions, automatic waits before interacting with the widget (eg: load time, fields depending on other fields and so on) pypom_form benefits
  9. Datetime widget Quickly set or get widget value: • page.expiration_date

    = datetime.datetime.now() # or add a timedelta • page.expiration_date -> datetime Fine grained access to widget API: • expiration_date = page.getWidgetRegion(‘expiration_date’) • expiration_date.next_year() • expiration_date.next_month()
  10. Encoded values widget Quickly set or get widget value: •

    page.encoded_values = [{‘value’:1, ‘label’: ‘ONE’}, {‘value’: 0: ‘label’: ‘ZERO’}] Fine grained access to widget API: • encoded_values = page.getWidgetRegion(‘encoded_values’) • encoded_values[0].label = ‘one’ • encoded_values[N].remove() • encoded_values.click_add() • encoded_values[-1].update(**{‘value’: 3, ‘label’: ‘THREE’}) # bulk update on last inserted row
  11. Schema definition example

  12. Complex precondition setup made easy

  13. • All QA projects migrated to pypom_form, standardization • Common

    reusable widget library • Tests runs against different skins with 100% code reuse (thanks to pytest’s fixtures parametrization, no test case duplication!) • Testers in Tierra with no programming skills are able to write automated tests if widgets are already available with a basic training • We are using Python, Pytest and pytest-bdd even for NON python software because... Python rocks! In addition: • Automated and declarative test data creation on demand (support for manual testing) • Easy way to setup complex preconditions in a data/keyword driven testing approach manner on BDD if you need more fine grained testa • High reusability of components, create your widget collections and become FAST! pypom_form in Tierra
  14. pypom_form Know more: • https://github.com/tierratelematics/pypom_form Feedback please! • I’m available

    for any question, let’s discuss together about testing! • Ping me on @davidemoro If you liked it: • GitHub star and share • Spread the word! • Let’s collaborate, it’s free software!
  15. THANK YOU FOR YOUR ATTENTION RESTRICTED PROPRIETARY INFORMATION The information

    disclosed herein is the exclusive property of Tierra and is not to be disclosed without the written consent of Tierra. No part of this publication may be reproduced or transmitted in any form or by any means including electronic storage, reproduction, execution or transmission without the prior written consent of Tierra. The recipient of this document by its retention and use agrees to respect the security status of the information contained herein. This document is intended for limited circulation. The information contained in this document is subject to change without notice and should not be construed as a commitment by Tierra unless such commitment is expressly given in a covering document. dmoro@tierratelematics.co m @davidemoro