title | linkTitle | weight | description |
---|---|---|---|
Organizing and Executing Selenium Code |
Using Selenium |
10 |
Scaling Selenium execution with an IDE and a Test Runner library
|
{{< alert-content >}} This page is very incomplete and has placeholders for things that need to be added or expounded on. {{< /alert-content >}}
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.
Most people use Selenium to execute automated tests for web applications, but Selenium supports any use case of browser automation.
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.
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...
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.
{{< tabpane text=true >}} {{% tab header="Java" %}}
- JUnit - A widely-used testing framework for Java-based Selenium tests.
- TestNG - Offers extra features like parallel test execution and parameterized tests. {{% /tab %}}
{{% tab header="Python" %}}
- pytest - A preferred choice for many, thanks to its simplicity and powerful plugins.
- unittest - Python's standard library testing framework. {{% /tab %}}
{{% tab header="CSharp" %}}
- NUnit - A popular unit-testing framework for .NET.
- MS Test - Microsoft's own unit testing framework. {{% /tab %}}
{{% tab header="Ruby" %}}
- RSpec - The most widely used testing library for running Selenium tests in Ruby.
- Minitest - A lightweight testing framework that comes with Ruby standard library. {{% /tab %}}
{{% tab header="JavaScript" %}}
- 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. {{% /tab %}}
{{% tab header="Kotlin" %}}
{{% /tab %}} {{< /tabpane >}}
This is very similar to what was required in [Install a Selenium Library]({{< ref "install_library.md" >}}). This code is only showing examples for what is being used in our Documentation Examples project.
{{< tabpane text=true >}} {{% tab header="Java" %}}
Maven
Gradle
{{% /tab %}} {{% tab header="Python" %}}
To use it in a project, add it to the requirements.txt
file:
{{% /tab %}}
{{% tab header="CSharp" %}}
in the project's csproj
file, specify the dependency as a PackageReference
in ItemGroup
:
{{% /tab %}} {{% tab header="Ruby" %}}
Add to project's gemfile
{{% /tab %}}
{{% tab header="JavaScript" %}}
In your project's package.json
, add requirement to dependencies
:
{{% /tab %}} {{< tab header="Kotlin" >}} {{< /tab >}} {{< /tabpane >}}
{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L30-L31" >}} {{< /tab >}} {{% tab header="Python" %}} {{< gh-codeblock path="examples/python/tests/getting_started/using_selenium_tests.py#L8-L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< gh-codeblock path="examples/dotnet/SeleniumDocs/GettingStarted/UsingSeleniumTest.cs#L19-L20" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="examples/ruby/spec/getting_started/using_selenium_spec.rb#L14-L15" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="examples/javascript/test/getting_started/runningTests.spec.js#L14-L15" >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< gh-codeblock path="examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt#L20-21" >}} {{< /tab >}} {{< /tabpane >}}
{{< tabpane text=true >}} {{% tab header="Java" %}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L19-L22" >}}
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L45-L48" >}}
{{% /tab %}} {{% tab header="Python" %}}
{{< gh-codeblock path="examples/python/tests/getting_started/using_selenium_tests.py#L25-L28" >}}
{{< gh-codeblock path="examples/python/tests/getting_started/using_selenium_tests.py#L30-31" >}}
{{% /tab %}} {{< tab header="CSharp" >}} {{< badge-code >}} {{< /tab >}} {{% tab header="Ruby" %}}
{{< gh-codeblock path="examples/ruby/spec/getting_started/using_selenium_spec.rb#L7-L9" >}}
{{< gh-codeblock path="examples/ruby/spec/spec_helper.rb#L28" >}} {{% /tab %}} {{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/getting_started/runningTests.spec.js#L7-L9" >}}
{{< gh-codeblock path="examples/javascript/test/getting_started/runningTests.spec.js#L30" >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}
{{< tabpane text=true >}} {{% tab header="Java" %}}
mvn clean test
gradle clean test
{{% /tab %}} {{% tab header="Python" %}} {{< gh-codeblock path="examples/python/README.md#L35" >}} {{% /tab %}} {{< tab header="CSharp" >}} {{< badge-code >}} {{< /tab >}} {{% tab header="Ruby" %}} {{< gh-codeblock path="examples/ruby/README.md#L26" >}} {{% /tab %}} {{% tab header="JavaScript" %}}
mocha runningTests.spec.js
npx mocha runningTests.spec.js
{{% /tab %}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}
In [First script]({{< ref "first_script.md" >}}), we saw each of the components of a Selenium script. Here's an example of that code using a test runner:
{{< tabpane text=true >}} {{< tab header="Java" >}} {{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="examples/python/tests/getting_started/using_selenium_tests.py" >}} {{< /tab >}} {{< tab header="CSharp" >}} {{< gh-codeblock path="examples/dotnet/SeleniumDocs/GettingStarted/UsingSeleniumTest.cs" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< gh-codeblock path="examples/ruby/spec/getting_started/using_selenium_spec.rb" >}} {{< /tab >}} {{< tab header="JavaScript" >}} {{< gh-codeblock path="examples/javascript/test/getting_started/runningTests.spec.js" >}} {{< /tab >}} {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} {{< /tabpane >}}
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]({{< ref "/documentation/webdriver/" >}}).