selenium

selenium

跨浏览器网页自动化测试框架

Selenium是一个开源的浏览器自动化测试框架,支持多种编程语言和主流浏览器。它遵循W3C WebDriver标准,让开发者能便捷地编写测试脚本来模拟用户操作网页。Selenium的跨平台兼容性、多语言支持和丰富API使其成为Web应用测试的理想选择,广泛应用于功能测试、回归测试和持续集成等场景。

Selenium自动化测试浏览器自动化WebDriver开源项目Github

Selenium

CI

<a href="https://selenium.dev"><img src="common/images/selenium_logo_mark_green.svg" width="180" alt="Selenium Logo"/></a>

Selenium is an umbrella project encapsulating a variety of tools and libraries enabling web browser automation. Selenium specifically provides an infrastructure for the W3C WebDriver specification — a platform and language-neutral coding interface compatible with all major web browsers.

The project is made possible by volunteer contributors who've generously donated thousands of hours in code development and upkeep.

Selenium's source code is made available under the Apache 2.0 license.

This README is for developers interested in contributing to the project. For people looking to get started using Selenium, please check out our User Manual for detailed examples and descriptions, and if you get stuck, there are several ways to Get Help.

Contributing

Please read CONTRIBUTING.md before submitting your pull requests.

Installing

These are the requirements to create your own local dev environment to contribute to Selenium.

All Platforms

  • Bazelisk, a Bazel wrapper that automatically downloads the version of Bazel specified in .bazelversion file and transparently passes through all command-line arguments to the real Bazel binary.
  • Java JDK version 17 or greater (e.g., Java 17 Temurin)
    • Set JAVA_HOME environment variable to location of Java executable (the JDK not the JRE)
    • To test this, try running the command javac. This command won't exist if you only have the JRE installed. If you're met with a list of command-line options, you're referencing the JDK properly.

MacOS

  • Xcode including the command-line tools. Install the latest version using: xcode-select --install
  • Rosetta for Apple Silicon Macs. Add build --host_platform=//:rosetta to the .bazelrc.local file. We are working to make sure this isn't required in the long run.

Windows

Several years ago Jim Evans published a great article on Setting Up a Windows Development Environment for the Selenium .NET Language Bindings; This article is out of date, but it includes more detailed descriptions and screenshots that some people might find useful.

<details> <summary>Click to see Current Windows Setup Requirements</summary>

Option 1: Automatic Installation from Scratch

This script will ensure a complete ready to execute developer environment. (nothing is installed or set that is already present unless otherwise prompted)

  1. Open Powershell as an Administrator
  2. Execute: Set-ExecutionPolicy Bypass -Scope Process -Force to allow running the script in the process
  3. Navigate to the directory you want to clone Selenium in, or the parent directory of an already cloned Selenium repo
  4. Download and execute this script in the powershell terminal: [scripts/dev-environment-setup.ps1]`

Option 2: Manual Installation

  1. Allow running scripts in Selenium in general:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    
  2. Enable Developer Mode:
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"
    
  3. Install MSYS2, which is an alternative shell environment that provides Unix-like commands
    • Add bin directory to PATH environment variable (e.g., "C:\tools\msys64\usr\bin")
    • Add bash.exe location as the BAZEL_SH environment variable (e.g., "C:\tools\msys64\usr\bin\bash.exe")
  4. Install the latest version of Visual Studio Community
    • Use the visual studio installer to modify and add the "Desktop development with C++" Workload
    • Add Visual C++ build tools installation directory location to BAZEL_VC environment variable (e.g. "C:\Program Files\Microsoft Visual Studio\2022\Community\VC")
    • Add Visual C++ Build tools version to BAZEL_VC_FULL_VERSION environment variable (this can be discovered from the directory name in "$BAZEL_VC\Tools\MSVC\<BAZEL_VC_FULL_VERSION>")
  5. Add support for long file names (bazel has a lot of nested directories that can exceed default limits in Windows)
    • Enable Long Paths support with these 2 registry commands:
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor" /t REG_DWORD /f /v "DisableUNCCheck" /d "1" reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem" /t REG_DWORD /f /v "LongPathsEnabled" /d "1"
    • Allow Bazel to create short name versions of long file paths: fsutil 8dot3name set 0
    • Set bazel output to C:/tmp instead of nested inside project directory:
      • Create a file selenium/.bazelrc.windows.local
      • Add "startup --output_user_root=C:/tmp" to the file
</details>

Alternative Dev Environments

If you want to contribute to the project, but do not want to set up your own local dev environment, there are two alternatives available.

Using GitPod

Rather than creating your own local dev environment, GitPod provides a ready to use environment for you.

Open in Gitpod

Using Dev Container

As an alternative you can build a Dev Container - basically a docker container - suitable for building and testing Selenium using the devcontainer.json in the .devcontainer directory. Supporting IDEs like VS Code or IntelliJ IDEA should point you to how such a container can be created.

Using Docker Image

You can also build a Docker image suitable for building and testing Selenium using the Dockerfile in the dev image directory.

Building

Selenium is built using a common build tool called Bazel, to allow us to easily manage dependency downloads, generate required binaries, build and release packages, and execute tests; all in a fast, efficient manner. For a more detailed discussion, read Simon Stewart's article on Building Selenium

Often we wrap Bazel commands with our custom Rake wrapper. These are run with the ./go command.

The common Bazel commands are:

  • bazel build — evaluates dependencies, compiles source files and generates output files for the specified target. It's used to create executable binaries, libraries, or other artifacts.
  • bazel run — builds the target and then executes it. It's typically used for targets that produce executable binaries.
  • bazel test — builds and runs the target in a context with additional testing functionality
  • bazel query — identifies available targets for the provided path.

Each module that can be built is defined in a BUILD.bazel file. To execute the module you refer to it starting with a //, then include the relative path to the file that defines it, then :, then the name of the target. For example, the target to build the Grid is named executable-grid and it is defined in the 'selenium/java/src/org/openqa/selenium/grid/BAZEL.build' file. So to build the grid you would run: bazel build //java/src/org/openqa/selenium/grid:executable-grid.

The Bazel documentation has a handy guide for various shortcuts and all the ways to build multiple targets, which Selenium makes frequent use of.

To build everything for a given language:

bazel build //<language>/...

To build just the grid there is an alias name to use (the log will show where the output jar is located):

bazel build grid

To make things more simple, building each of the bindings is available with this ./go command

./go <language>:build

Developing

Java

IntelliJ

Most of the team uses Intellij for their day-to-day editing. If you're working in IntelliJ, then we highly recommend installing the Bazel IJ plugin which is documented on its own site.

To use Selenium with the IntelliJ Bazel plugin, import the repository as a Bazel project, and select the project view file from the scripts directory. ij.bazelproject for Mac/Linux and ij-win.bazelproject for Windows.

Linting

We also use Google Java Format for linting, so using the Google Java Formatter Plugin is useful; there are a few steps to get it working, so read their configuration documentation. There is also an auto-formatting script that can be run: ./scripts/format.sh

Local Installation

While Selenium is not built with Maven, you can build and install the Selenium pieces for Maven to use locally by deploying to your local maven repository (~/.m2/repository), using:

./go java:install

Updating Dependencies

Dependencies are defined in the file maven_deps.bzl. To automatically update and pin new dependencies, run:

./go java:update

Python

You can run Python code locally by updating generated files in the python directory using:

./go py:update

To install Selenium locally based on a specific commit, you can use:

./go py:install

Ruby

Instead of using irb, you can create an interactive REPL with all gems loaded using: bazel run //rb:console

If you want to debug code, you can do it via debug gem:

  1. Add binding.break to the code where you want the debugger to start.
  2. Run tests with ruby_debug configuration: bazel test --config ruby_debug <test>.
  3. When debugger starts, run the following in a separate terminal to connect to debugger:
bazel-selenium/external/bundle/bin/rdbg -A

If you want to use RubyMine for development, you can configure it use Bazel artifacts:

  1. Open rb/ as a main project directory.
  2. Run bundle exec rake update as necessary to create up-to-date artifacts. If this does not work, run ./go rb:update from the selenium (parent) directory.
  3. In <kbd>Settings / Languages & Frameworks / Ruby SDK and Gems</kbd> add new <kbd>Interpreter</kbd> pointing to ../bazel-selenium/external/rules_ruby_dist/dist/bin/ruby.
  4. You should now be able to run and debug any spec. It uses Chrome by default, but you can alter it using environment variables secified in Ruby Testing section below.

Rust

To keep Carbo.Bazel.lock synchronized with Cargo.lock, run:

CARGO_BAZEL_REPIN=true bazel sync --only=crates

Testing

There are a number of bazel configurations specific for testing.

Common Options Examples

Here are examples of arguments we make use of in testing the Selenium code:

  • --pin_browsers - run specific browser versions defined in the build (versions are updated regularly)
  • --headless - run browsers in headless mode (supported be Chrome, Edge and Firefox)
  • --flaky_test_attempts 3 - re-run failed tests up to 3 times
  • --local_test_jobs 1 - control parallelism of tests
  • --cache_test_results=no, -t- - disable caching of test results and re-runs all of them
  • --test_output all - print all output from the tests, not just errors
  • --test_output streamed - run all tests one by one and print its output immediately
  • --test_env FOO=bar - pass extra environment variable to test process
  • --run_under="xvfb-run -a" - prefix to insert before the execution

Filtering

Selenium tests can be filtered by size:

  • small — typically unit tests where no browser is opened
  • large — typically tests that actually drive a browser
  • medium — tests that are more involved than simple unit tests, but not fully driving a browser

These can be filtered using the test_size_filters argument like this:

bazel test //<language>/... --test_size_filters=small

Tests can also be filtered by tag like:

bazel test //<language>/... --test_tag_filters=this,-not-this

Java

<details> <summary>Click to see Java Test Commands</summary>

To run unit tests:

bazel test //java/... --test_size_filters=small

To run integration tests:

bazel test //java/... --test_size_filters=medium

To run browser tests:

bazel test //java/... --test_size_filters=large --test_tag_filters=<browser>

To run a specific test:

bazel test //java/test/org/openqa/selenium/chrome:ChromeDriverFunctionalTest
</details>

JavaScript

<details> <summary>Click to see JavaScript Test Commands</summary>

To run the tests run:

bazel test //javascript/node/selenium-webdriver:tests

You can use --test_env to pass in the browser name as SELENIUM_BROWSER.

bazel test //javascript/node/selenium-webdriver:tests --test_env=SELENIUM_BROWSER=firefox
</details>

Python

<details> <summary>Click to see Python Test Commands</summary>

Run unit tests with:

bazel test //py:unit

To run tests with a specific browser:

bazel test //py:test-<browsername>

To run all Python tests:

bazel test //py:all
</details>

Ruby

<details> <summary>Click to see Ruby Test Commands</summary>

Test targets:

CommandDescription
bazel test //rb/...Run unit, all integration tests and lint
bazel test //rb:lintRun RuboCop linter
bazel test //rb/spec/...Run unit and integration tests for all browsers
bazel test //rb/spec/... --test_size_filters smallRun unit tests
bazel test //rb/spec/unit/...Run unit tests
bazel test //rb/spec/... --test_size_filters largeRun integration tests for all browsers
bazel test //rb/spec/integration/...Run integration tests for all browsers
bazel test //rb/spec/integration/... --test_tag_filters firefoxRun integration tests for local Firefox only
bazel test //rb/spec/integration/... --test_tag_filters firefox-remoteRun integration tests for remote Firefox only
bazel test //rb/spec/integration/... --test_tag_filters firefox,firefox-remoteRun integration tests for local and remote Firefox

Ruby test targets have the same name as the spec file with _spec.rb removed, so you can run them individually. Integration tests targets also have a browser and remote suffix to control which browser to pick and whether to use Grid.

Test fileTest target
rb/spec/unit/selenium/webdriver/proxy_spec.rb//rb/spec/unit/selenium/webdriver:proxy
rb/spec/integration/selenium/webdriver/driver_spec.rb//rb/spec/integration/selenium/webdriver:driver-chrome
rb/spec/integration/selenium/webdriver/driver_spec.rb//rb/spec/integration/selenium/webdriver:driver-chrome-remote
rb/spec/integration/selenium/webdriver/driver_spec.rb//rb/spec/integration/selenium/webdriver:driver-firefox
rb/spec/integration/selenium/webdriver/driver_spec.rb//rb/spec/integration/selenium/webdriver:driver-firefox-remote

Supported browsers:

  • chrome
  • edge
  • firefox
  • firefox-beta
  • ie
  • safari
  • safari-preview

In addition to the Common Options Examples, here are some additional Ruby specific ones:

  • --test_arg "-eTimeouts" - test only specs which name include "Timeouts"
  • --test_arg "<any other RSpec argument>" - pass any extra RSpec arguments (see `bazel run @bundle//bin:rspec --

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多