Introduction
Debugging is one of the most important skills in Selenium automation testing. While IDEs such as VS Code and PyCharm provide graphical debugging tools, Python also includes a built-in debugger called pdb that allows developers to pause program execution and inspect the application’s current state directly from the terminal.
The breakpoint() function, introduced in Python 3.7, provides a simple and convenient way to invoke Python’s built-in debugger. When execution reaches a breakpoint, the program pauses and allows you to inspect variables, execute commands, and step through the code line by line.
Breakpoints are extremely useful for:
Understanding Selenium’s behavior.
Inspecting variable values.
Debugging failed assertions.
Investigating synchronization issues.
Troubleshooting locator problems.
Learning how Selenium executes automation scripts.
In this tutorial, you will learn how breakpoints work in Selenium automation, understand Python’s built-in debugger commands, explore practical examples, common mistakes, best practices, and frequently asked interview questions.
What are Breakpoints?
A breakpoint is a location within your program where execution temporarily pauses so that you can inspect the application’s current state.
Instead of:
Test Fails
│
▼
Modify the Code
│
▼
Run Again
│
▼
Failure Continues
│
▼
Repeat Multiple Times
we can use breakpoints:
Test Starts
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Inspect Variables
│
▼
Execute Debug Commands
│
▼
Continue Execution
│
▼
Identify the Problem Easily
Breakpoints significantly simplify debugging Selenium automation scripts.
Why Should We Use Breakpoints?
Breakpoints allow developers to:
Pause program execution.
Inspect variable values.
Execute debugger commands.
Analyze Selenium’s behavior.
Investigate failed assertions.
Understand automation flows.
Simplify troubleshooting efforts.
Large automation frameworks frequently use breakpoints during development and debugging activities.
Practical Example
The following example demonstrates how to use Python’s built-in breakpoint() function while debugging Selenium automation scripts.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: Using Breakpoints
# Practice site:
# https://www.testmuai.com/selenium-playground/simple-form-demo
# Run:
# pytest -s 63_examples/test_03_using_breakpoints.py
#
# Python's built-in breakpoint() function starts the debugger (pdb) and pauses
# execution, allowing developers to inspect variables and execute debugging
# commands interactively.
def test_using_breakpoints():
driver = webdriver.Chrome()
try:
driver.get(
"https://www.testmuai.com/"
"selenium-playground/simple-form-demo"
)
message_input = driver.find_element(
By.ID,
"user-message"
)
# Uncomment the next line to start debugging.
# breakpoint()
message_input.send_keys(
"Breakpoint Demo"
)
driver.find_element(
By.ID,
"showInput"
).click()
assert driver.find_element(
By.ID,
"message"
).text == "Breakpoint Demo"
finally:
driver.quit()
Output
Chrome browser launched successfully.
Website opened successfully.
Breakpoint reached successfully.
Execution paused.
Variables inspected successfully.
Execution resumed.
Assertions Passed.
Test Executed Successfully.
Note: The
breakpoint()function is commented out in this example so that the test executes normally. Uncomment the line whenever you want to pause execution and start debugging interactively.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports:
Selenium WebDriver.
Locator strategies.
Launch Chrome Browser
driver = webdriver.Chrome()
Creates a new Chrome browser session.
Open the Website
driver.get(
"https://www.testmuai.com/"
"selenium-playground/simple-form-demo"
)
Opens the Selenium Playground webpage.
Locate the Element
message_input = driver.find_element(
By.ID,
"user-message"
)
Selenium successfully locates the input field before execution reaches the breakpoint.
Start the Debugger
breakpoint()
When execution reaches this line:
Test Starts
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Debugger Starts
│
▼
Wait for User Commands
Python automatically starts its built-in debugger (pdb) and waits for user input.
Running the Example
Execute the following command:
py -3 -m pytest -s ^
"63_examples/test_03_using_breakpoints.py"
Run all debugging examples together:
py -3 -m pytest -s ^
"63_examples/"
Note: The
-soption displays console output and allows interactive debugger commands to be entered while usingbreakpoint().
Useful Debugger Commands
Python’s built-in debugger provides several useful commands.
| Command | Description |
|---|---|
n | Execute the next line |
s | Step into a function |
c | Continue execution |
p expression | Print the value of an expression |
q | Quit the debugger |
l | Display the surrounding source code |
h | Display debugger help |
These commands greatly simplify debugging Selenium automation scripts.
Example Debugging Session
Suppose execution pauses here:
breakpoint()
The debugger displays:
(Pdb)
You may execute:
(Pdb) p message_input
Output:
<selenium.webdriver.remote.webelement.WebElement>
Print the current webpage URL:
(Pdb) p driver.current_url
Output:
https://www.testmuai.com/
selenium-playground/simple-form-demo
Print the page title:
(Pdb) p driver.title
Output:
Selenium Playground
Step Through the Code
Execute:
(Pdb) n
which means:
Next Line
│
▼
Execute Current Statement
│
▼
Pause Again
To step into a function:
(Pdb) s
To continue execution:
(Pdb) c
which resumes normal program execution.
Execution Flow
Launch Browser
│
▼
Open Website
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Debugger Starts
│
▼
Execute Commands
│
▼
Inspect Variables
│
▼
Continue Execution
│
▼
Assertions Passed
│
▼
Close Browser
Automation Testing Example
Suppose an assertion fails unexpectedly.
Instead of writing:
assert actual == expected
we can pause execution immediately before the assertion and inspect:
(Pdb) p actual
↓
Failure
(Pdb) p expected
↓
Success
Breakpoints make identifying failures significantly easier.
Real-World Example
Large automation frameworks frequently use breakpoints to investigate:
Failed assertions.
Missing elements.
Incorrect locators.
Synchronization problems.
Browser-related issues.
Framework failures.
Dynamic webpage behavior.
For example:
Automation Failure
│
▼
Breakpoint Reached
│
▼
Inspect Variables
│
▼
Execute Debug Commands
│
▼
Identify Failure
│
▼
Fix the Problem
│
▼
Run the Test Again
Breakpoints greatly simplify troubleshooting efforts.
Common Mistakes Beginners Make
Forgetting to Remove Breakpoints
Leaving:
breakpoint()
inside production automation code may unnecessarily pause execution during future test runs.
Always remove or comment out temporary breakpoints after debugging is complete.
Ignoring Useful Debugger Commands
Many beginners only use:
n
Learning commands such as:
s
c
p expression
l
q
greatly improves debugging efficiency.
Using Excessive Print Statements
Instead of writing:
print(driver.title)
print(driver.current_url)
print(element.text)
we can simply execute:
(Pdb) p driver.title
(Pdb) p driver.current_url
(Pdb) p element.text
without modifying the source code.
Best Practices
Use breakpoints strategically.
Learn commonly used
pdbcommands.Remove temporary breakpoints after debugging.
Inspect variables before modifying the automation script.
Prefer breakpoints over excessive print statements during troubleshooting.
Use breakpoints whenever assertions or synchronization problems occur.
Maintain meaningful assertions to simplify debugging.
Conclusion
Python’s built-in breakpoint() function provides a simple yet powerful mechanism for debugging Selenium automation scripts. By pausing execution and allowing developers to inspect variables and execute debugger commands interactively, breakpoints significantly improve troubleshooting efficiency.
Understanding how to use Python’s debugger effectively simplifies Selenium automation development and greatly improves framework maintainability in real-world projects.
Mastering breakpoints and debugger commands is an important Selenium automation and interview skill.
Frequently Asked Questions (FAQs)
What is breakpoint() in Python?
breakpoint() is Python’s built-in function that starts the debugger (pdb) and pauses program execution at the specified location.
Which debugger commands are commonly used?
The most frequently used commands include:
n→ Next line.s→ Step into.c→ Continue execution.p expression→ Print values.q→ Quit the debugger.l→ Display source code.
Can I use breakpoints with PyTest?
Yes.
Python’s built-in debugger works seamlessly with PyTest when tests are executed using the -s option.
Should I remove breakpoints after debugging?
Yes.
Temporary breakpoints should always be removed or commented out before committing production automation code.
Are breakpoints better than print statements?
Both are useful. However, breakpoints provide significantly more information because they allow developers to inspect variables interactively without modifying the source code.
Key Takeaways
Python’s
breakpoint()function provides powerful debugging capabilities for Selenium automation testing.Breakpoints allow developers to pause execution and inspect Selenium’s behavior interactively.
Common debugger commands such as
n,s,c, andp expressiongreatly simplify troubleshooting.Variable inspection significantly improves failure analysis.
Breakpoints should be used strategically during automation development.
Temporary breakpoints should always be removed or commented out after debugging is complete.
Proper debugging practices substantially improve framework maintainability.
Using Breakpoints is an important Selenium automation and interview topic.
