×

Introduction

Selenium is a powerful longstanding framework for automating web browsers and testing web applications. Set up your first Selenium test suite by a clear step-by-step guide. This article will walk you through the process of setting up Selenium WebDriver with a popular programming language, Python, and running your first test. Prerequisites

Before you begin, install the following essentials:

  • Python (version 3.6 or higher)
  • pip (Python package installer)
  • PyCharm (code editor)
  • Google Chrome or Firefox web browser

Step 1: Install Selenium

First, start by installing the Selenium package. Open your terminal or command prompt and run the following command:


pip install selenium

This command will download and install the latest Selenium WebDriver.

Step 2: Download WebDriver

Selenium requires a driver to interface with the chosen browser. For Chrome, you’ll need ChromeDriver, and for Firefox, GeckoDriver.

  • ChromeDriver: Download from ChromeDriver download page.
  • GeckoDriver: Download from GeckoDriver releases.

After downloading, place the driver in a directory included in your system’s PATH.

Step 3: Set Up Your Project

Create a new directory for your Selenium project. Inside this directory, create a new Python file, for example, test_selenium.py.


mkdir selenium_test_suite 
cd selenium_test_suite touch test_selenium.py

Step 4: Write Your First Test

Open test_selenium.py in your code editor and start by importing the necessary modules and setting up the WebDriver.

from selenium import webdriver from selenium.webdriver.common.keys import Keys

  • # Initialize the Chrome WebDriver driver = webdriver.Chrome(executable_path;/path/to/chromedriver;))
  • # Update path as necessary
  • # Open a website driver.get(“http://www.google.com”) # Verify the title assert “Google” in driver.title
  • # Find the search box
  • search_box = driver.find_element_by_name
  • # Type in the search box search_box.send_keys(“Selenium WebDriver”) search_box.send_keys(Keys.RETURN) # Close the browser driver.quit()

In this script,

  • Import the necessary Selenium WebDriver modules.
  • Initialize the WebDriver for Chrome.
  • Open the Google homepage.
  • Verify that the page title contains “Google”.
  • Find the search input box using its name attribute.
  • Simulate typing “Selenium WebDriver” into the search box and pressing Enter.
  • Close the browser after completing the actions.

Step 5: Run Your Test

To run your test, navigate to your project directory in the terminal and execute the Python script:


python test_selenium.py

Step 6: Organize Your Tests

For a more organized test suite, consider using a testing framework like unittest, pytest, or nose. Below is an example using unittest:
Install unittest if it’s not already available:


pip install unittest

Refactor your test_selenium.py to use unittest:


import unittest from selenium import webdriver from selenium.webdriver.
common.keys import Keys class GoogleSearchTest(unittest.TestCase): def setUp(self): 
self.driver = webdriver.Chrome(executable_path='/path/to/chromedriver') 
def test_search_in_google(self): 
driver = self.driver driver.get("http://www.google.com")
self.assertIn("Google", driver.title)
search_box = driver.find_element_by_name("q")
search_box.send_keys("Selenium WebDriver")
search_box.send_keys(Keys.RETURN)
self.assertTrue("Selenium" in driver.page_source) 
def tearDown(self): 
self.driver.quit() if __name__ == "__main__": unittest.main()

In this script:

  • You define a test class that inherits from unittest.TestCase.
  • Use setUp and tearDown methods for initializing and closing the WebDriver.
  • Define your test case as a method within the test class.

Step 7: Execute the Test Suite

Run the test suite by executing the script:

 
python test_selenium.py

Conclusion

Finally, Setting up your first Selenium test suite involves installing the necessary tools, configuring the WebDriver, writing your test scripts, and optionally organizing them using a testing framework. To expertise in Selenium, Credo Systemz provides the best Selenium Training in Chennai. Learn Selenium to automate web application testing that leads to a successful career path.

Call Now Button