Selenium
This WebDriver testing example will use Selenium and a popular Node.js testing suite. You are expected to already have
Node.js installed, along with npm
or yarn
although the finished example project uses yarn
.
Create a Directory for the Tests
Let’s create a space to write these tests in our project. We will be using a nested directory for
this example project as we will later also go over other frameworks, but typically you will only need to use one. Create
the directory we will use with mkdir -p webdriver/selenium
. The rest of this guide will assume you are inside the
webdriver/selenium
directory.
Initializing a Selenium Project
We will be using a pre-existing package.json
to bootstrap this test suite because we have already chosen specific
dependencies to use and want to showcase a simple working solution. The bottom of this section has a collapsed
guide on how to set it up from scratch.
package.json
:
We have a script that runs Mocha as a test framework exposed as the test
command. We also have various dependencies
that we will be using to run the tests. Mocha as the testing framework, Chai as the assertion library, and
selenium-webdriver
which is the Node.js Selenium package.
Click me if you want to see how to set a project up from scratch
If you want to install the dependencies from scratch, just run the following command.
I suggest also adding a "test": "mocha"
item in the package.json
"scripts"
key so that running Mocha can be called
simply with
Testing
Unlike the WebdriverIO Test Suite, Selenium does not come out of the box with a Test Suite and
leaves it up to the developer to build those out. We chose Mocha, which is pretty neutral and not related to WebDrivers, so our script will need to do a bit of work to set up everything for us in the correct order. Mocha expects a
testing file at test/test.js
by default, so let’s create that file now.
test/test.js
:
If you are familiar with JS testing frameworks, describe
, it
, and expect
should look familiar. We also have
semi-complex before()
and after()
callbacks to set up and teardown mocha. Lines that are not the tests themselves
have comments explaining the setup and teardown code. If you were familiar with the Spec file from the
WebdriverIO example, you notice a lot more code that isn’t tests, as we have to set up a few
more WebDriver related items.
Running the Test Suite
Now that we are all set up with our dependencies and our test script, let’s run it!
We should see output the following output:
We can see that our Hello Tauri
test suite we created with describe
had all 3 items we created with it
pass their
tests!
With Selenium and some hooking up to a test suite, we just enabled e2e testing without modifying our Tauri application at all!
© 2024 Tauri Contributors. CC-BY / MIT