Organizing and Executing Selenium Code (original) (raw)
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.
Testing
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.
IDEs
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.
Choosing
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.
Kotest - A flexible and comprehensive testing framework specifically designed for Kotlin.
JUnit5 - The standard Java testing framework, fully compatible with Kotlin.
Installing
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:
Asserting
String title = driver.getTitle();
assertEquals("Web form", title);/examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class UsingSeleniumTest {
WebDriver driver;
@BeforeEach
public void setup() {
driver = new ChromeDriver();
}
@Test
public void eightComponents() {
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
String title = driver.getTitle();
assertEquals("Web form", title);
WebElement textBox = driver.findElement(By.name("my-text"));
WebElement submitButton = driver.findElement(By.cssSelector("button"));
textBox.sendKeys("Selenium");
submitButton.click();
WebElement message = driver.findElement(By.id("message"));
String value = message.getText();
assertEquals("Received!", value);
}
@AfterEach
public void teardown() {
driver.quit();
}
}
title = driver.title
assert title == "Web form"/examples/python/tests/getting_started/using_selenium_tests.py
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_eight_components():
driver = setup()
title = driver.title
assert title == "Web form"
driver.implicitly_wait(0.5)
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
teardown(driver)
def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver
def teardown(driver):
driver.quit()
var title = driver.Title;
Assert.AreEqual("Web form", title);/examples/dotnet/SeleniumDocs/GettingStarted/UsingSeleniumTest.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.GettingStarted
{
[TestClass]
public class UsingSeleniumTest
{
[TestMethod]
public void EightComponents()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
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"));
textBox.SendKeys("Selenium");
submitButton.Click();
var message = driver.FindElement(By.Id("message"));
var value = message.Text;
Assert.AreEqual("Received!", value);
driver.Quit();
}
}
} title = @driver.title
expect(title).to eq('Web form')/examples/ruby/spec/getting_started/using_selenium_spec.rb
# frozen_string_literal: true
require 'spec_helper'
require 'selenium-webdriver'
RSpec.describe 'Using Selenium' do
before do
@driver = Selenium::WebDriver.for :chrome
end
it 'uses eight components' do
@driver.get('https://www.selenium.dev/selenium/web/web-form.html')
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')
text_box.send_keys('Selenium')
submit_button.click
message = @driver.find_element(id: 'message')
value = message.text
expect(value).to eq('Received!')
end
end
let title = await driver.getTitle();
assert.equal("Web form", title);/examples/javascript/test/getting_started/runningTests.spec.js
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('https://www.selenium.dev/selenium/web/web-form.html');
let title = await driver.getTitle();
assert.equal("Web form", title);
await driver.manage().setTimeouts({implicit: 500});
let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));
await textBox.sendKeys('Selenium');
await submitButton.click();
let message = await driver.findElement(By.id('message'));
let value = await message.getText();
assert.equal("Received!", value);
});
after(async () => await driver.quit());
});Setting Up and Tearing Down
Set Up
@BeforeEach
public void setup() {
driver = new ChromeDriver();
}/examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class UsingSeleniumTest {
WebDriver driver;
@BeforeEach
public void setup() {
driver = new ChromeDriver();
}
@Test
public void eightComponents() {
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
String title = driver.getTitle();
assertEquals("Web form", title);
WebElement textBox = driver.findElement(By.name("my-text"));
WebElement submitButton = driver.findElement(By.cssSelector("button"));
textBox.sendKeys("Selenium");
submitButton.click();
WebElement message = driver.findElement(By.id("message"));
String value = message.getText();
assertEquals("Received!", value);
}
@AfterEach
public void teardown() {
driver.quit();
}
}
Tear Down
@AfterEach
public void teardown() {
driver.quit();
}/examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class UsingSeleniumTest {
WebDriver driver;
@BeforeEach
public void setup() {
driver = new ChromeDriver();
}
@Test
public void eightComponents() {
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
String title = driver.getTitle();
assertEquals("Web form", title);
WebElement textBox = driver.findElement(By.name("my-text"));
WebElement submitButton = driver.findElement(By.cssSelector("button"));
textBox.sendKeys("Selenium");
submitButton.click();
WebElement message = driver.findElement(By.id("message"));
String value = message.getText();
assertEquals("Received!", value);
}
@AfterEach
public void teardown() {
driver.quit();
}
}
Set Up
def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver/examples/python/tests/getting_started/using_selenium_tests.py
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_eight_components():
driver = setup()
title = driver.title
assert title == "Web form"
driver.implicitly_wait(0.5)
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
teardown(driver)
def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver
def teardown(driver):
driver.quit()
Tear Down
def teardown(driver):
driver.quit()/examples/python/tests/getting_started/using_selenium_tests.py
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_eight_components():
driver = setup()
title = driver.title
assert title == "Web form"
driver.implicitly_wait(0.5)
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
teardown(driver)
def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver
def teardown(driver):
driver.quit()
Set Up
before do
@driver = Selenium::WebDriver.for :chrome
end/examples/ruby/spec/getting_started/using_selenium_spec.rb
# frozen_string_literal: true
require 'spec_helper'
require 'selenium-webdriver'
RSpec.describe 'Using Selenium' do
before do
@driver = Selenium::WebDriver.for :chrome
end
it 'uses eight components' do
@driver.get('https://www.selenium.dev/selenium/web/web-form.html')
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')
text_box.send_keys('Selenium')
submit_button.click
message = @driver.find_element(id: 'message')
value = message.text
expect(value).to eq('Received!')
end
end
Tear Down
config.after { @driver&.quit }/examples/ruby/spec/spec_helper.rb
# frozen_string_literal: true
require 'selenium-webdriver'
require 'selenium/webdriver/support/guards'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
Dir.mktmpdir('tmp')
config.example_status_persistence_file_path = 'tmp/examples.txt'
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.before do |example|
bug_tracker = 'https://github.com/SeleniumHQ/seleniumhq.github.io/issues'
guards = Selenium::WebDriver::Support::Guards.new(example,
bug_tracker: bug_tracker)
guards.add_condition(:platform, Selenium::WebDriver::Platform.os)
guards.add_condition(:ci, Selenium::WebDriver::Platform.ci)
results = guards.disposition
send(*results) if results
end
config.after { @driver&.quit }
def start_session
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('disable-search-engine-choice-screen')
options.add_argument('--no-sandbox')
@driver = Selenium::WebDriver.for(:chrome, options: options)
end
def start_bidi_session
options = Selenium::WebDriver::Chrome::Options.new(web_socket_url: true)
@driver = Selenium::WebDriver.for :chrome, options: options
end
def start_firefox
options = Selenium::WebDriver::Options.firefox(timeouts: {implicit: 1500})
@driver = Selenium::WebDriver.for :firefox, options: options
end
end
### Set Up
before(async function () {
driver = await new Builder().forBrowser('chrome').build();
});/examples/javascript/test/getting_started/runningTests.spec.js
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('https://www.selenium.dev/selenium/web/web-form.html');
let title = await driver.getTitle();
assert.equal("Web form", title);
await driver.manage().setTimeouts({implicit: 500});
let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));
await textBox.sendKeys('Selenium');
await submitButton.click();
let message = await driver.findElement(By.id('message'));
let value = await message.getText();
assert.equal("Received!", value);
});
after(async () => await driver.quit());
});### Tear Down
after(async () => await driver.quit());/examples/javascript/test/getting_started/runningTests.spec.js
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('https://www.selenium.dev/selenium/web/web-form.html');
let title = await driver.getTitle();
assert.equal("Web form", title);
await driver.manage().setTimeouts({implicit: 500});
let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));
await textBox.sendKeys('Selenium');
await submitButton.click();
let message = await driver.findElement(By.id('message'));
let value = await message.getText();
assert.equal("Received!", value);
});
after(async () => await driver.quit());
});Executing
Maven
Gradle
/examples/python/README.md
# Running tests from Selenium Python examples
#### 1. Clone this repository
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
#### 2. Navigate to `python` directory
cd seleniumhq.github.io/examples/python
#### 3. Create a virtual environment
- On Windows:
py -m venv venv venv\Scripts\activate
- On Linux/Mac:
python3 -m venv venv source venv/bin/activate
#### 4. Install dependencies:
pip install -r requirements.txt
> for help, see: https://packaging.python.org/en/latest/tutorials/installing-packages
#### 5. Run tests
- Run all tests with the default Python interpreter:
pytest
- Run all tests with every installed/supported Python interpreter:
tox
> Please have some patience - If you are doing it for the first time, it will take a little while to download the browser drivers
- Run a specific example:
pytest path/to/test_script.py
> Make sure to replace `path/to/test_script.py` with the path and name of the example you want to run
/examples/ruby/README.md
# Running all tests from Selenium ruby example
Follow these steps to run all test example from selenium ruby
1. Clone this repository
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
2. Navigate to `ruby` directory
cd seleniumhq.github.io/examples/ruby
3. Install dependencies using bundler
bundler install
4. Run all tests
bundle exec rspec
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
# Execute a ruby script
Use this command to run a ruby script and follow the first script example
ruby example_script.rb
Mocha
mocha runningTests.spec.js
npx
npx mocha runningTests.spec.js
Examples
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class UsingSeleniumTest {
WebDriver driver;
@BeforeEach
public void setup() {
driver = new ChromeDriver();
}
@Test
public void eightComponents() {
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
String title = driver.getTitle();
assertEquals("Web form", title);
WebElement textBox = driver.findElement(By.name("my-text"));
WebElement submitButton = driver.findElement(By.cssSelector("button"));
textBox.sendKeys("Selenium");
submitButton.click();
WebElement message = driver.findElement(By.id("message"));
String value = message.getText();
assertEquals("Received!", value);
}
@AfterEach
public void teardown() {
driver.quit();
}
}
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_eight_components():
driver = setup()
title = driver.title
assert title == "Web form"
driver.implicitly_wait(0.5)
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"
teardown(driver)
def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver
def teardown(driver):
driver.quit()
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.GettingStarted
{
[TestClass]
public class UsingSeleniumTest
{
[TestMethod]
public void EightComponents()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
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"));
textBox.SendKeys("Selenium");
submitButton.Click();
var message = driver.FindElement(By.Id("message"));
var value = message.Text;
Assert.AreEqual("Received!", value);
driver.Quit();
}
}
}# frozen_string_literal: true
require 'spec_helper'
require 'selenium-webdriver'
RSpec.describe 'Using Selenium' do
before do
@driver = Selenium::WebDriver.for :chrome
end
it 'uses eight components' do
@driver.get('https://www.selenium.dev/selenium/web/web-form.html')
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')
text_box.send_keys('Selenium')
submit_button.click
message = @driver.find_element(id: 'message')
value = message.text
expect(value).to eq('Received!')
end
end
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('https://www.selenium.dev/selenium/web/web-form.html');
let title = await driver.getTitle();
assert.equal("Web form", title);
await driver.manage().setTimeouts({implicit: 500});
let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));
await textBox.sendKeys('Selenium');
await submitButton.click();
let message = await driver.findElement(By.id('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 ourWebDriver documentation.
Support the Selenium Project
Learn more or view the full list of sponsors.