Introduction
After learning how to launch a browser, open a website, and close the browser, it’s time to run your first Selenium automation test.
An automation test is a script that performs user actions automatically and verifies whether the application behaves as expected. Instead of manually clicking buttons or selecting checkboxes, Selenium performs these actions programmatically and validates the results.
A typical Selenium automation test consists of the following steps:
Launch the browser.
Open the target website.
Locate one or more web elements.
Perform user actions.
Verify the expected outcome.
Close the browser.
Running your first automation test helps you understand the complete Selenium testing workflow and forms the foundation for building more advanced automation scripts.
In this tutorial, you will learn how to run your first Selenium automation test, understand each line of code, explore real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is an Automation Test?
An automation test is a program that automatically executes predefined test steps and verifies whether the application behaves correctly.
Unlike manual testing, automation testing executes the same test repeatedly without human intervention.
For example, an automation test can:
Open a browser.
Navigate to a webpage.
Click a checkbox.
Verify that the checkbox is selected.
Close the browser.
Why Do We Run Automation Tests?
Automation tests help to:
Eliminate repetitive manual testing.
Improve testing speed.
Increase test accuracy.
Detect application defects early.
Support Continuous Integration and Continuous Deployment (CI/CD).
Save time during regression testing.
Basic Workflow of an Automation Test
A Selenium automation test generally follows this workflow:
Launch Browser
│
▼
Open Website
│
▼
Locate Web Element
│
▼
Perform User Action
│
▼
Verify Expected Result
│
▼
Close Browser
Practical Example
The following example launches Chrome, opens the Selenium practice page, selects the first checkbox if it is not already selected, verifies that the checkbox is selected, and closes the browser.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 4. Your First Selenium Script - Running Your First Automation Test
# Practice site: https://the-internet.herokuapp.com/checkboxes
#
# A complete automation test opens a page, interacts with an element, and
# asserts the expected outcome.
def test_checkbox_automation():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/checkboxes")
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
first_checkbox = checkboxes[0]
if not first_checkbox.is_selected():
first_checkbox.click()
assert first_checkbox.is_selected()
finally:
driver.quit()
Output
Chrome browser launched successfully.
Checkboxes page opened successfully.
First checkbox located.
Checkbox selected successfully.
Assertion Passed.
Automation Test Executed Successfully.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports Selenium WebDriver and the By class used to locate web elements.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Website
driver.get("https://the-internet.herokuapp.com/checkboxes")
Opens the Selenium practice page containing checkboxes.
Locate the Checkboxes
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
Locates all checkbox elements available on the webpage.
Select the First Checkbox
first_checkbox = checkboxes[0]
if not first_checkbox.is_selected():
first_checkbox.click()
Gets the first checkbox and selects it only if it is not already selected.
Verify the Checkbox Selection
assert first_checkbox.is_selected()
Confirms that the checkbox has been successfully selected.
If the assertion passes, the automation test is considered successful.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Execution Flow
Import Selenium Modules
│
▼
Launch Chrome Browser
│
▼
Open Checkboxes Page
│
▼
Locate Checkbox Elements
│
▼
Select First Checkbox
│
▼
Verify Checkbox is Selected
│
▼
Close Browser
Automation Testing Example
Suppose your application contains a “Remember Me” checkbox on the login page.
An automation test can verify that the checkbox is selected correctly.
remember_me.click()
assert remember_me.is_selected()
This ensures the application’s checkbox functionality works as expected.
Real-World Example
Consider an online registration form that requires users to accept the Terms and Conditions before submitting the form.
Instead of manually checking the checkbox during every test, Selenium can automate the process.
terms_checkbox.click()
assert terms_checkbox.is_selected()
This saves time and ensures consistent testing.
Common Mistakes Beginners Make
Using find_element() Instead of find_elements()
Incorrect
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
first_checkbox = checkboxes[0]
This raises an error because find_element() returns only one element, not a list.
Correct
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
Clicking an Already Selected Checkbox
Incorrect
first_checkbox.click()
If the checkbox is already selected, clicking it again will deselect it.
Correct
if not first_checkbox.is_selected():
first_checkbox.click()
Forgetting Assertions
Without assertions, Selenium performs actions but never verifies whether the test actually passed.
Always validate the expected result.
Best Practices
Always verify the result using assertions.
Check an element’s current state before interacting with it.
Use meaningful variable names.
Close the browser using
driver.quit().Keep automation tests small, readable, and focused on one functionality.
Conclusion
Running your first Selenium automation test introduces the complete automation workflow—from launching the browser to validating application behavior.
In this example, Selenium automatically opened a webpage, located a checkbox, selected it when necessary, verified the expected result, and closed the browser. This same workflow is used in real-world automation projects to test login forms, shopping carts, registration pages, and many other web application features.
Mastering this workflow is an important milestone before learning advanced Selenium topics such as waits, locators, page objects, and automation frameworks.
Frequently Asked Questions (FAQs)
What is an automation test?
An automation test is a script that automatically performs user actions and verifies expected application behavior.
Why do we use assertions?
Assertions verify that the application behaves as expected.
What does find_elements() return?
It returns a list of all matching web elements.
Why do we use is_selected()?
is_selected() checks whether a checkbox or radio button is currently selected.
Why should we use driver.quit()?
It closes all browser windows and properly ends the WebDriver session.
Key Takeaways
An automation test performs actions and verifies expected results automatically.
A typical Selenium test launches the browser, opens a webpage, interacts with elements, validates the outcome, and closes the browser.
find_elements()returns multiple matching elements.click()performs user interactions with web elements.is_selected()checks the selection state of checkboxes and radio buttons.Assertions confirm whether the automation test has passed.
Always close the browser using
driver.quit().Understanding your first automation test is the foundation for building reliable Selenium automation frameworks.
