selenium
  1. selenium-tool-suite

Tool Suite - Selenium

Selenium is a suite of tools that are used to automate web browsers. Selenium includes three main tools:

  • Selenium IDE
  • Selenium WebDriver
  • Selenium Grid

Selenium IDE

Selenium IDE is a browser extension that enables you to record, edit, and debug Selenium tests. Selenium IDE is available for Chrome and Firefox browsers.

Syntax

To record a test case using Selenium IDE:

  1. Open the Selenium IDE extension in your browser
  2. Navigate to the website that you want to record a test case for
  3. Click on the "Record" button in the Selenium IDE interface
  4. Perform the actions that you want to include in your test case
  5. Click on the "Stop Recording" button when you are finished

Example

The following is an example of a test case that was recorded using Selenium IDE:

| Command         | Target                     | Value |
| --------------- | --------------------------| ----- |
| open            | /                          |       |
| clickAndWait    | link=Login                 |       |
| type            | id=username               | admin |
| type            | id=password               | admin |
| clickAndWait    | css=input[type="submit"]   |       |
| assertText      | css=h2                     | Home  |
| clickAndWait    | link=Logout                |       |
| assertText      | css=h2                     | Login |

Output

The Selenium IDE test case can be exported in several different programming languages, including Java, C#, Python, and Ruby.

Selenium WebDriver

Selenium WebDriver is a web automation framework that enables you to write code using a variety of programming languages.

Syntax

To write a test case using Selenium WebDriver:

  1. Set up a WebDriver object for the browser you want to automate
  2. Write the code to navigate to the website and perform the desired actions
  3. Use WebDriver commands to interact with the website elements
  4. Use assertions to verify that the website is behaving as expected

Example

The following is an example of a test case written in Python using Selenium WebDriver:

from selenium import webdriver

# Set up WebDriver object
driver = webdriver.Chrome()

# Navigate to website and perform actions
driver.get("http://www.example.com")
driver.find_element_by_link_text("Contact Us").click()
driver.find_element_by_id("name").send_keys("John Doe")
driver.find_element_by_id("email").send_keys("johndoe@example.com")
driver.find_element_by_id("message").send_keys("This is a test message")
driver.find_element_by_id("submit").click()

# Verify website behavior
assert "Thank you" in driver.page_source

# Close browser
driver.close()

Output

When the test case is run, the browser will open and navigate to the specified website. The test will perform the actions specified in the code and verify that the website is behaving as expected.

Selenium Grid

Selenium Grid is a tool that enables you to run multiple tests in parallel on multiple browsers and operating systems.

Syntax

To use Selenium Grid:

  1. Set up a Hub on one machine
  2. Set up Nodes on multiple machines
  3. Define the desired capabilities for the test
  4. Run the test using RemoteWebDriver

Example

The following is an example of how to set up Selenium Grid using Docker and run a test on multiple browsers:

  1. Set up the Hub and two Nodes using Docker

    $ docker run -d -p 4444:4444 --name selenium-hub selenium/hub:3.141.59-zinc
    $ docker run -d -P --link selenium-hub:hub selenium/node-chrome-debug:3.141.59-zinc
    $ docker run -d -P --link selenium-hub:hub selenium/node-firefox-debug:3.141.59-zinc
    
  2. Define the desired capabilities for the test in Python

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    chrome = DesiredCapabilities.CHROME.copy()
    chrome['platform'] = 'LINUX'
    
    firefox = DesiredCapabilities.FIREFOX.copy()
    firefox['platform'] = 'LINUX'
    
    browsers = {
        "chrome": webdriver.Remote(
            command_executor="http://localhost:4444/wd/hub",
            desired_capabilities=chrome
        ),
        "firefox": webdriver.Remote(
            command_executor="http://localhost:4444/wd/hub",
            desired_capabilities=firefox
        )
    }
    
  3. Run the test on multiple browsers

    for browser, driver in browsers.items():
        driver.get("http://www.example.com")
        # Perform test steps
        driver.quit()
    

Output

The test will run on both Chrome and Firefox browsers, allowing you to test the same functionality on different environments.

Published on: