Automation Testing Tutorial 5 – How to Develop Maintainable Test Scripts Leveraging Frameworks

In this automation testing tutorial, we‘ll cover how to write scripts using some of the most popular test automation frameworks. Proper use of frameworks makes your checks more robust, maintainable, and scalable.

Let‘s start simple, understand the common pitfalls, then level up to frameworks!

Challenges with Linear Test Scripts

When first getting started with test automation, you may write linear scripts that execute from start to finish in sequence:

// Start app
var calc = LaunchApp("calc.exe");

// Find buttons 
var btn1 = FindButton(calc, "1");
var btnAdd = FindButton(calc, "Plus"); 

// Click buttons
Click(btn1);
Click(btnAdd);

// Verify 
var results = FindResultsText(calc);
AssertEquals(results, "1");

This is completely fine when testing simple flows! But linear scripts become problematic as we add more test cases.

According to testing experts, some drawbacks of linear scripts include:

  • Poor readability – The test logic gets lost in code
  • Lack of reuse – Duplicate code across methods
  • Fragile maintenance – Many updates on app changes

For example, if the calculator is updated and the "Plus" button is renamed to "Add", we need to update multiple tests scripts. This violates DRY principles.

According to CodeProject, up to 80% of test automation effort is spent maintaining existing scripts. We need a better way!

Improving Scripts with Test Automation Frameworks

Test automation frameworks provide standards and structure so tests are easier to maintain as they scale. Well-designed frameworks encourage:

  • Code reuse – Common functions and components
  • Separation of concerns – UI maps, page objects
  • Ease of reporting – Logical organization

Let‘s look at an example framework:

// Common UI Map
var calc = new CalcPage();

// Helper Functions
ClickButton(name) {
  var btn = calc.GetButton(name); 
  btn.Click();
}

// Test Case
LaunchApp("calc.exe");

ClickButton("1");
ClickButton("Add");
ClickButton("1");

// Assert Results
AssertEquals(calc.GetResults(), 2);

This demonstrates abstraction by using:

  • Page object to encapsulate UI details
  • Shared helper function reduces duplication
  • Test case reads clearly at a high level

So while linear scripts are okay to get started, you‘ll want to evolve tests with one of many framework options we‘ll now explore.

Top 5 Test Automation Frameworks

Experts recommend considering the following 5 frameworks as a starting point when architecting your test automation approach:

1. Linear Framework

  • All test logic written in sequence within scripts
  • Easy to write and understand
  • No abstraction, so lots of duplicated code
  • Brittle tests require frequent updating

2. Modular Framework

  • Common test components reused across scripts
  • Code segmented into libraries, helpers, page objects, etc.
  • Achieves abstraction and reduces duplication
  • Promotes easier maintenance

3. Data-Driven Framework

  • Test data stored separately from core logic
  • Values read from external sources (CSV, database, etc)
  • Allows running same test against multiple data
  • Useful for testing multiple user types

4. Keyword-Driven Framework

  • Actions described via keywords and tables/CSVs
  • Keywords implemented via code; data passed separately
  • Enables collaboration between testers and devs
  • Allows testers to define tests without coding

5. Hybrid Framework

  • Combines multiple approaches to maximize strengths
  • Attempts to optimize tradeoffs
  • Allows customization for specific test needs

Below we analyze some key benefits provided by these different options.

Comparison of Key Test Automation Framework Benefits

Framework Reuseability Maintainability Readability Collaboration
Linear Low Low Medium Medium
Modular High High High Medium
Data-Driven High Medium Low Low
Keyword-Driven Medium Medium High High
Hybrid High High High High

Let‘s analyze some notable points:

  • Linear frameworks offer simplicity but struggle with complex tests
  • Modular frameworks are very popular for striking a good balance
  • Data-driven testing enables great reusability but some don‘t like external data dependencies
  • Keyword-driven frameworks excel at cross-team collaboration
  • Hybrid frameworks let teams customize by combining different techniques

So in summary, more complex frameworks provide more power and flexibility. But they require more effort to architect and develop. Choose wisely based on your needs!

Hybrid Frameworks Balance Tradeoffs

Hybrid test automation frameworks aim to deliver the best of all worlds by combining different approaches.

For example, your team might:

  • Adopt a modular framework as the core foundation
  • Incorporate keyword-driven tables for business analysts to define test cases
  • Enable running parametrized data-driven batches at scale

This allows optimizing for reusability, collaboration, and maintenance.

However, while hybrid frameworks seem ideal in theory, beware of overcomplicating delivery. Too many abstractions can obfuscate the testing logic and create dependency hell.

According to testing pundits, those adopting hybrid frameworks should:

  • Architect with simplicity in mind from the beginning
  • Closely align framework usage to actual testing needs
  • Continually refactor as tests evolve over time

So in essence, just because you can leverage multiple frameworks doesn‘t always mean you should! Seek the right balance for long-term automation success.

Factors Impacting Framework Selection and Design

Choosing a test automation framework vitally shapes short and long-term outcomes. Consider analyzing factors like:

Application Type: Are you testing web, mobile, API, desktop apps?

Team Skills: What‘s the programming expertise across testers and developers?

Testing Needs: Will you automate different types like functional, security, performance etc?

App Complexity: How frequently does the system under test change?

Reporting Needs: What test metrics and dashboards will stakeholder require?

Budget & Timelines: Building extensive frameworks demands significant upfront effort.

Analyze these elements closely, then architect your framework deliberately to maximize ROI. And don‘t be afraid to iterate based on learnings!

In Closing: Script Like a Software Craftsman

I hope this tutorial clearly explained why intelligently incorporating test automation frameworks drastically improves outcomes.

While starting out, linear scripts help first grasp automation tools. But quickly implementing modular constructs, helpers, custom libraries etc separates software professionals from dabblers.

Great carpenters don‘t just randomly nail boards together. They purposely apply best practices to craft solid structures built to last decades rather than days.

Likewise, elevate your automated tests by thoughtfully structuring code using proven frameworks. Your future self will thank you the next time application changes arise!

Now that you know frameworks 101, consider reviewing more advanced examples using Java and Selenium. Cheers and happy scripting!

Read More Topics