Playwright + Java · Lesson 5 of 38

0/38 done

Module 5: Methods

5. Methods, Overloading & Access Modifiers

14 minlesson

Try it yourself

Create a method formatUrl:

  1. Accept baseUrl (String) and path (String).
  2. If baseUrl ends with '/' and path starts with '/', remove one slash.
  3. Return the clean concatenated URL string.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Method Design in Test Utilities

java
public class NavigationHelper {
    // Overloaded method without timeout:
    public static void navigateTo(String path) {
        navigateTo(path, 30000);
    }

    // Overloaded method with custom timeout:
    public static void navigateTo(String path, int timeoutMs) {
        System.out.println("Navigating to " + path + " with timeout " + timeoutMs);
    }
}

Access Modifiers

  • public: Accessible anywhere in the test framework.
  • protected: Accessible within package and subclasses.
  • private: Encapsulated inside the page or helper class.
  • default (package-private): Accessible inside the same package.

Key Takeaways & Walkthrough Summary

Methods encapsulate reusable test actions, locator strategies, and assertions. In this lesson, you will learn parameters, return types, method overloading, and access modifiers to design clean test helpers. Let's build reusable methods!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

When should helper methods be static vs instance methods?

Static methods are great for stateless utilities (e.g. string formatting, date generators). Instance methods are needed when holding page state or browser instances.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

Java
Skip