Selenium + Python · Lesson 10 of 43

0/43 done

Module 10: Python Features for Automation

10. Python Comprehensions, Pathlib & Datetime for QA

16 minlesson

Your task

Implement generate_unique_email(prefix: str = "test") -> str:

  1. Use datetime.now().strftime("%Y%m%d_%H%M%S").
  2. Return f"{prefix}_{timestamp}@targetqa.com".

Write it in the editor, then press Check answer (Ctrl + Enter).

Modern Python for QA Engineers

python
from pathlib import Path
from datetime import datetime, timedelta

# Pathlib for cross-platform file paths (no Windows backslash bugs):
report_path = Path("reports") / "test_summary.html"

# Generating unique test email:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
user_email = f"test_{timestamp}@targetqa.com"

# List comprehension to clean extracted table values:
raw_cells = ["  Row 1 ", " Row 2 ", "   "]
clean_cells = [c.strip() for c in raw_cells if c.strip()]

Key Takeaways & Walkthrough Summary

Modern Python features make test automation elegant and concise. In this lesson, you will master list comprehensions, dictionary comprehensions, pathlib for cross-platform file paths, and datetime for timestamp generation. Let's explore modern Python features!

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

Why use pathlib.Path over os.path?

pathlib is object-oriented, uses the clean '/' operator on all operating systems (Windows, macOS, Linux), and eliminates path string formatting mistakes.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip