Selenium + Python · Lesson 5 of 43

0/43 done

Module 5: Functions

5. Reusable Automation Functions, *args, **kwargs & Lambdas

15 minlesson

Try it yourself

Implement build_api_url(endpoint, base_url="https://targetqa.com", **params):

  1. Strip leading slash from endpoint.
  2. If params are provided, append "?param=val&..." to the URL.
  3. Return the full URL string.

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

Designing Automation Helper Functions

python
# Default arguments & type hints:
def wait_and_click(locator: str, timeout: int = 10, check_visible: bool = True):
    print(f"Waiting up to {timeout}s for {locator}")

# Variable keyword arguments (**kwargs) for flexible parameters:
def create_test_user(username: str, **kwargs):
    user = {"username": username, "role": kwargs.get("role", "STANDARD")}
    return user

Key Takeaways & Walkthrough Summary

Functions encapsulate reusable actions and assertions. In this lesson, you will master default arguments, keyword arguments, variable-length *args and **kwargs for flexible locator helpers, and lambda functions. Let's design clean automation functions!

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

Common Doubts & Lesson Q&A

What are *args and **kwargs used for in test automation?

*args passes positional arguments as a tuple, while **kwargs passes arbitrary keyword arguments as a dictionary, perfect for flexible test configuration.

Finished reading?

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

Python
Skip