Last updated
Last updated
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.
First we will need to install the package:
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:
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.
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.
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.
Here is a basic example on building a test for the installed homepage:
we can then run the test by running:
You can chain all methods together to build up and mock user actions. An example test might look like this:
When finding a selector you can use a few symbols to help navigate the page
Take this form for example:
You can select an ID by using the #
symbol:
You can select by the name by simply passing in the name value. This will default to the name 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:
Below are the available methods you can use to build your tests.
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.
This method will assert that the title is a specific value
Opposite of assertTitleIs
.
This method will assert that the current URL is a specific value
Asserts that something is available on the page that the user can see
Just an alias for assertSee
.
Opposite of assertCanSee
. Used to assert that text is not on the page.
Types text into a text box.
You can choose an option in a select box by its value:
This will check a checkbox
Resizes the window based on a width and heigh parameter
Maximizes the window
Minimizes the window
Refreshes the window
Navigates backwards
Navigates forwards
Clicks a link on a page
Alias for link
.
Submits the current form the last entered element is in
You can also submit another form by entering a selector
Clicks an element
Closes the browser