Organizing and Executing Selenium Code

Scaling Selenium execution with an IDE and a Test Runner library

If you want to run more than a handful of one-off scripts, you need to be able to organize and work with your code. This page should give you ideas for how to actually do productive things with your Selenium code.

Common Uses

Most people use Selenium to execute automated tests for web applications, but Selenium supports any use case of browser automation.

Repetitive Tasks

Perhaps you need to log into a website and download something, or submit a form. You can create a Selenium script to run with a service at preset times.

Web Scraping

Are you looking to collect data from a site that doesn’t have an API? Selenium will let you do this, but please make sure you are familiar with the website’s terms of service as some websites do not permit it and others will even block Selenium.


Running Selenium for testing requires making assertions on actions taken by Selenium. So a good assertion library is required. Additional features to provide structure for tests require use of Test Runner.


Regardless of how you use Selenium code, you won’t be very effective writing or executing it without a good Integrated Developer Environment. Here are some common options…

Test Runner

Even if you aren’t using Selenium for testing, if you have advanced use cases, it might make sense to use a test runner to better organize your code. Being able to use before/after hooks and run things in groups or in parallel can be very useful.


There are many different test runners available.

All the code examples in this documentation can be found in (or is being moved to) our example directories that use test runners and get executed every release to ensure all the code is correct and updated. Here is a list of test runners with links. The first item is the one that is used by this repository and the one that will be used for all examples on this page.

  • JUnit - A widely-used testing framework for Java-based Selenium tests.
  • TestNG - Offers extra features like parallel test execution and parameterized tests.
  • pytest - A preferred choice for many, thanks to its simplicity and powerful plugins.
  • unittest - Python’s standard library testing framework.
  • NUnit - A popular unit-testing framework for .NET.
  • MS Test - Microsoft’s own unit testing framework.
  • RSpec - The most widely used testing library for running Selenium tests in Ruby.
  • Minitest - A lightweight testing framework that comes with Ruby standard library.
  • Jest - Primarily known as a testing framework for React, it can also be used for Selenium tests.
  • Mocha - The most common JS library for running Selenium tests.


This is very similar to what was required in Install a Selenium Library. This code is only showing examples for what is being used in our Documentation Examples project.



To use it in a project, add it to the requirements.txt file:

in the project’s csproj file, specify the dependency as a PackageReference in ItemGroup:

Add to project’s gemfile

In your project’s package.json, add requirement to dependencies:


		String title = driver.getTitle();
		assertEquals("Web form", title);
    expect(value).to eq('Received!')

Setting Up and Tearing Down

Set Up

	public void setup() {
		driver = new ChromeDriver();

Tear Down

	public void teardown() {

Set Up

  before do
    @driver = Selenium::WebDriver.for :chrome

Tear Down

  config.after { @driver&.quit }



mvn clean test


gradle clean test
mocha runningTests.spec.js


In First script, we saw each of the components of a Selenium script. Here’s an example of that code using a test runner:

package dev.selenium.getting_started;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class UsingSeleniumTest {

	WebDriver driver;

	public void setup() {
		driver = new ChromeDriver();

	public void eightComponents() {


		String title = driver.getTitle();
		assertEquals("Web form", title);

		WebElement textBox = driver.findElement("my-text"));
		WebElement submitButton = driver.findElement(By.cssSelector("button"));


		WebElement message = driver.findElement("message"));
		String value = message.getText();
		assertEquals("Received!", value);


	public void teardown() {

from selenium import webdriver
from import By

def test_eight_components():
    driver = webdriver.Chrome()


    title = driver.title
    assert title == "Web form"


    text_box = driver.find_element(by=By.NAME, value="my-text")
    submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")


    message = driver.find_element(by=By.ID, value="message")
    value = message.text
    assert value == "Received!"

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumDocs.GettingStarted
    public class UsingSeleniumTest

        public void EightComponents()
            IWebDriver driver = new ChromeDriver();


            var title = driver.Title;
            Assert.AreEqual("Web form", title);

            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);

            var textBox = driver.FindElement(By.Name("my-text"));
            var submitButton = driver.FindElement(By.TagName("button"));
            var message = driver.FindElement(By.Id("message"));
            var value = message.Text;
            Assert.AreEqual("Received!", value);
# frozen_string_literal: true
require 'spec_helper'
require 'selenium-webdriver'

RSpec.describe 'Using Selenium' do
  before do
    @driver = Selenium::WebDriver.for :chrome

  it 'uses eight components' do

    title = @driver.title
    expect(title).to eq('Web form')

    @driver.manage.timeouts.implicit_wait = 500

    text_box = @driver.find_element(name: 'my-text')
    submit_button = @driver.find_element(tag_name: 'button')


    message = @driver.find_element(id: 'message')
    value = message.text
    expect(value).to eq('Received!')
const {By, Builder} = require('selenium-webdriver');
const assert = require("assert");

  describe('First script', function () {
    let driver;
    before(async function () {
      driver = await new Builder().forBrowser('chrome').build();
    it('First Selenium script with mocha', async function () {
      await driver.get('');
      let title = await driver.getTitle();
      assert.equal("Web form", title);
      await driver.manage().setTimeouts({implicit: 500});
      let textBox = await driver.findElement('my-text'));
      let submitButton = await driver.findElement(By.css('button'));
      await textBox.sendKeys('Selenium');
      let message = await driver.findElement('message'));
      let value = await message.getText();
      assert.equal("Received!", value);
    after(async () => await driver.quit());

Next Steps

Take what you’ve learned and build out your Selenium code!

As you find more functionality that you need, read up on the rest of our WebDriver documentation.