Writing Your First Selenium Test Script: A Step-by-Step Guide

Selenium is a powerful tool for automating web browsers, making it a crucial skill for those interested in selenium automation testing. For beginners, understanding how to write a simple test script is the first step to mastering Selenium. This article will walk you through the process of setting up and executing your first Selenium test script in Python. With practice, you can advance to more complex tests, and enrolling in a selenium course or a selenium training in Chennai can accelerate your learning.

Step 1: Install Python and Selenium


Before you can start coding, ensure you have Python installed on your computer, as it’s one of the most widely used languages in Selenium testing. To check if Python is installed, open a terminal or command prompt and type:

python --version

If it’s not installed, download and install Python from python.org.

Next, install Selenium by running:

pip install selenium

Step 2: Set Up the WebDriver


Each browser (e.g., Chrome, Firefox) needs a specific WebDriver to communicate with Selenium. Here, we’ll set up ChromeDriver, but the process is similar for Firefox (using GeckoDriver).

  1. Download ChromeDriver from the official website. Make sure to choose the version that matches your Chrome browser.

  2. After downloading, save the ChromeDriver executable in a known location on your computer.


Step 3: Writing Your First Script


Now, let’s dive into writing a basic test script. This script will open a browser, navigate to a website, search for a term, and verify the search result.

Open a text editor like Visual Studio Code, and create a new file named first_test.py. Copy and paste the following code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Set the path to the ChromeDriver
chrome_driver_path = "path/to/chromedriver"

# Initialize WebDriver
driver = webdriver.Chrome(executable_path=chrome_driver_path)

# Step 1: Open a website
driver.get("https://www.google.com")

# Step 2: Locate the search bar, type a query, and hit Enter
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium automation testing")
search_box.send_keys(Keys.RETURN)

# Step 3: Wait and verify results
time.sleep(3) # Wait for results to load
assert "Selenium" in driver.title

# Step 4: Close the browser
driver.quit()

Step 4: Run the Test Script


To execute your script, navigate to the folder where first_test.py is saved in your terminal or command prompt and type:

python first_test.py

This script will open Chrome, navigate to Google, search for “Selenium automation testing,” and verify that the results contain the word “Selenium” in the page title.

Step 5: Understanding Key Selenium Concepts


The script you wrote uses several core Selenium commands:

  • get() opens a URL.

  • find_element(By.NAME, "q") locates the search bar using the element’s name.

  • send_keys() types text and sends the Enter command.

  • assert verifies that “Selenium” appears in the page title, a basic validation step.


Next Steps: Enroll in a Course


Writing your first script is just the beginning! To gain a more comprehensive understanding, consider a selenium course. Many programs cover advanced topics like handling dynamic content, working with iframes, and automating complex workflows. For hands-on guidance, enrolling in selenium training in Chennai can be highly beneficial, providing insights into best practices and troubleshooting.

With these fundamentals and continuous practice, you’ll be well on your way to mastering selenium automation testing and developing robust test scripts.

 

Leave a Reply

Your email address will not be published. Required fields are marked *