Master
ThoughtWorks
Menü
schließen
  • Unsere Services
    • Übersicht
    • Customer Experience, Produkt und Design
    • Data Strategy, Engineering und Analytics
    • Digitale Transformation und Operations
    • Enterprise Modernization, Plattformen und Cloud
  • Unsere Kunden
    • Übersicht
    • Automobil
    • Gesundheit
    • Öffentlicher Sektor
    • Clientech, Energie und Versorgung
    • Medien
    • Handel und E-Commerce
    • Banken und Versicherungen
    • Non-Profit
    • Reise und Transport
  • Insights
    • Übersicht
    • Unsere Empfehlungen

      • Technologie

        Ausführliche Betrachtungen neuer Technologien.

      • Business

        Aktuelle Business-Insights, Strategien und Impulse für digitale Querdenker.

      • Kultur

        Insights zu Karrieremöglichkeiten und unsere Sicht auf soziale Gerechtigkeit und Inklusivität.

    • Digitale Veröffentlichungen und Tools

      • Technology Radar

        Unser Leitfaden für aktuelle Technologietrends.

      • Perspectives

        Unsere Publikation für digitale Vordenker*innen

      • Digital Fluency Model

        Ein Modell zur Priorisierung digitaler Fähigkeiten, um für das Unvorhersehbare bereit zu sein.

      • Decoder

        Der Technology-Guide für Business Entscheider

    • Alle Insights

      • Artikel

        Expertenwissen für Ihr Unternehmen.

      • Blogs

        Persönliche Perspektiven von ThoughtWorkern aus aller Welt.

      • Bücher

        Stöbern Sie durch unsere umfangreiche Bibliothek.

      • Podcasts

        Spannende Gespräche über das Neueste aus Business und Technologie.

  • Karriere
    • Übersicht
    • Bewerbungsprozess

      Finde heraus, was dich in unserem Bewerbungsprozess erwartet.

    • Hochschulabsovent*innen und Quereinsteiger*innen

      Dein Einstieg in die IT-Welt.

    • Stellenangebote

      Finde offene Stellen in deiner Region.

    • In Kontakt bleiben

      Abonniere unsere monatlichen Updates.

  • Über uns
    • Übersicht
    • Unsere Mission
    • Awards und Auszeichnungen
    • Vielfalt, Gleichberechtigung, Inklusion
    • Management
    • Partnerschaften
    • Neuigkeiten
    • Konferenzen und Events
  • Kontakt
Germany | Deutsch
  • United States United States
    English
  • China China
    中文 | English
  • India India
    English
  • Canada Canada
    English
  • Singapore Singapore
    English
  • United Kingdom United Kingdom
    English
  • Australia Australia
    English
  • Germany Germany
    English | Deutsch
  • Brazil Brazil
    English | Português
  • Spain Spain
    English | Español
  • Global Global
    English
Blogs
Wählen Sie ein Thema
Alle Themen ansehenschließen
Technologie 
Agiles Projektmanagement Cloud Continuous Delivery  Data Science & Engineering Defending the Free Internet Evolutionäre Architekturen Experience Design IoT Sprachen, Tools & Frameworks Modernisierung bestehender Alt-Systeme Machine Learning & Artificial Intelligence Microservices Plattformen Sicherheit Software Testing Technologiestrategie 
Geschäft 
Financial Services Global Health Innovation Retail  Transformation 
Karriere 
Karriere Hacks Diversity und Inclusion Social Change 
Blogs

Themen

Thema auswählen
  • Technologie
    Technologie
  • Technologie Überblick
  • Agiles Projektmanagement
  • Cloud
  • Continuous Delivery
  • Data Science & Engineering
  • Defending the Free Internet
  • Evolutionäre Architekturen
  • Experience Design
  • IoT
  • Sprachen, Tools & Frameworks
  • Modernisierung bestehender Alt-Systeme
  • Machine Learning & Artificial Intelligence
  • Microservices
  • Plattformen
  • Sicherheit
  • Software Testing
  • Technologiestrategie
  • Geschäft
    Geschäft
  • Geschäft Überblick
  • Financial Services
  • Global Health
  • Innovation
  • Retail
  • Transformation
  • Karriere
    Karriere
  • Karriere Überblick
  • Karriere Hacks
  • Diversity und Inclusion
  • Social Change
Software TestingTechnologie

Writing Twist tests with Page Object pattern

ThoughtWorks ThoughtWorks

Published: May 6, 2013

Page object pattern maps UI pages to classes and related actions to methods in that class. This allows for better grouping of page actions. All the actions specific to each page will be in a single class. Page object pattern has few advantages:

  • Better reusability
  • Improved test readability
  • Models the UI in tests

This page explains page object pattern in detail.

Twist tests and page object pattern

In this blog post, I will explain about how to use page object pattern while writing Twist tests. Let us consider a test scenario in which we are searching for a product in Amazon and adding the product to shopping cart.

In Twist, each step lives in a fixture class and this can represent actions specific to that page. In the above example, we have two page classes, "On Homepage" and "On shopping cart page". Now before each step executes, we should navigate to the specific pages, so that the step can perform its task without worrying about navigating to the page.

Before Twist 13.1, your step would be something like this:

public void searchForAndAddToCart(String product) throws Exception {
   String url = "http://amazon.com";
   if (!browser.getCurrentUrl().equals(url)) {
   browser.get(url);
   }
   // Implementation for search and add to cart
 }

Or in your scenario, you could navigate before each step, something like this:

The trouble here is that for each step that gets added to the fixture class, the navigation has to be completed before it can perform the action. This becomes a problem when more and more actions get added to the fixture class. It can be easily solved by using "Execution hooks" which are introduced in Twist 13.1.

Twist provides global, scenario level execution hook and a fixture level hook. Fixture level hooks are local to the fixture class. In this example, we will use a fixture level hook.

public void searchForAndAddToCart(String product) throws Exception { 
      // Implementation for search and add to cart
}
@BeforeEachStep 
public void beforeEachStep() {
      String url = "http://amazon.com"; 
      if (!browser.getCurrentUrl().equals(url)) {
          browser.get(url);
      }
}

"beforeEachStep()" method will be executed before every step executes in that fixture. We will navigate to the required page in this hook. With this, the step implementation doesn’t need to worry about the navigation.

Similarly, to verify items are added to shopping cart, “before hook” ensures you are on the shopping cart page before the verification code is executed.

public void verifyShoppingCartContains(String string1) throws Exception { 
      // Code to do the verification
} 
@BeforeEachStep 
public void beforeEachStep() { 
      String url = "https://www.amazon.com/gp/cart/view.html/"; 
      if (!browser.getCurrentUrl().equals(url)) { 
      browser.get(url);
      } 
} 

This method also improves reusability of steps across scenarios. For instance, we can use "Verify shopping cart contains product" from any scenario file without thinking about its navigation. This also simplifies the scenario, as shown below,

 

See what's new in Twist 13.1 on our community site or Try Twist now.

 

 

Master
Datenschutz | Modern Slavery statement | Barrierefreies Webdesign
Connect with us
×

WeChat

QR code to ThoughtWorks China WeChat subscription account
© 2021 ThoughtWorks, Inc.