Selenium Testing
Introduction
Selenium tests are browser tests. With normal unit testing you are usually testing sections of your code like JSON endpoints or seeing a header on a page.
With Selenium testing, you are able to test more advanced features that may require JS handling like what happens when a user clicks a button or submits or a form.
With selenium testing, you will build a bunch of actions for your tests and a browser will open and actually go step by step according to your instructions.
This package comes shipped with all the required selenium drivers so there is no need for any external selenium server installations.
Getting Started
Installation
First we will need to install the package:
Creating The TestCase
This package extends the current testing framework so you will add the TestCase to your current unit tests.
Let's build a new TestCase
and test the homepage that shows when Masonite is first installed:
This will generate a new test that looks like this:
Building The TestCase
In order to build the selenium test we need to import the class and add it to our TestCase
.
We now have all methods available to us to start building our selenium tests.
Using a Browser
First before we build our TestCase, we need to specify which browser we want. The current 2 options are: chrome
and firefox
.
You can specify which browser to use using the useBrowser()
method inside the setUp()
method.
If you are using the chrome driver you can optionally specify which version to run.
Headless
You can optionally specify if you want to run your browsers in a headless state. This means that the browser will not actually open to run tests but will run in the background. This will not effect your tests but is just a preference and usually your tests will run faster.
Building The Test
Here is a basic example on building a test for the installed homepage:
we can then run the test by running:
Method Chaining
You can chain all methods together to build up and mock user actions. An example test might look like this:
Selectors
When finding a selector you can use a few symbols to help navigate the page
Take this form for example:
Selecting by ID
You can select an ID by using the #
symbol:
Selecting by Name
You can select by the name by simply passing in the name value. This will default to the name attribute:
Selecting by Class
Selecting by a Unique Attribute
The issue with selecting by a normal selector like an ID or a name is that these could change. This is why you are able to select with a unique attribute name.
You may change your form a bit to do something like this instead:
You can then tell Masonite what the name of your unique attribute is:
and finally you can select by that attribute using the @
symbol:
Available Methods
Below are the available methods you can use to build your tests.
visit
This method will navigate to a URL
If the URL does not start with http then Masonite will prepend the APP_URL
environment variable to the front. If this is running inside your Masonite application, you can change this value in your .env
file.
assertTitleIs
This method will assert that the title is a specific value
assertTitleIsNot
Opposite of assertTitleIs
.
assertUrlIs
This method will assert that the current URL is a specific value
assertSee
Asserts that something is available on the page that the user can see
assertCanSee
Just an alias for assertSee
.
assertCantSee
Opposite of assertCanSee
. Used to assert that text is not on the page.
text
Types text into a text box.
selectBox
You can choose an option in a select box by its value:
check
This will check a checkbox
resize
Resizes the window based on a width and heigh parameter
mazimize
Maximizes the window
minimize
Minimizes the window
refresh
Refreshes the window
back
Navigates backwards
forward
Navigates forwards
link
Clicks a link on a page
clickLink
Alias for link
.
submit
Submits the current form the last entered element is in
You can also submit another form by entering a selector
click
Clicks an element
close
Closes the browser
Last updated