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…
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.
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.
Maven
Gradle
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:
# frozen_string_literal: truerequire'selenium-webdriver'require'selenium/webdriver/support/guards'RSpec.configuredo|config|# Enable flags like --only-failures and --next-failureconfig.example_status_persistence_file_path='.rspec_status'# Disable RSpec exposing methods globally on `Module` and `main`config.disable_monkey_patching!config.expect_with:rspecdo|c|c.syntax=:expectendconfig.beforedo|example|bug_tracker='https://gigithub.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.dispositionsend(*results)ifresultsendconfig.after{@driver&.quit}defstart_session@driver=Selenium::WebDriver.for:chromeenddefstart_firefoxoptions=Selenium::WebDriver::Options.firefox(timeouts:{implicit:1500})@driver=Selenium::WebDriver.for:firefox,options:optionsendend
# Running all tests from Selenium python example
Follow these steps to run all test example from selenium python
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. Install dependencies using pip
```
pip install -r requirements.txt
```> if you are on a different python version, for example python3.x you may have to replace `pip` with `pip3`
4. Run all tests
```
pytest
```> 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 specific example
To run a specific Selenium Python example, use the following command:
```bash
python first_script.py
```Make sure to replace `first_script.py` with the path and name of the example you want to run.
# 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 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
```