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:
$ pip install masonite-selenium
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:
$ python craft test HomePage
This will generate a new test that looks like this:
from masonite.testing import TestCaseclassTestHomePage(TestCase):"""All tests by default will run inside of a database transaction.""" transactions =TruedefsetUp(self):"""Anytime you override the setUp method you must call the setUp method on the parent class like below. """super().setUp()defsetUpFactories(self):"""This runs when the test class first starts up. This does not run before every test case. Use this method to set your database up. """pass
Building The TestCase
In order to build the selenium test we need to import the class and add it to our TestCase.
from masonite.testing import TestCasefrom masonite.testing.selenium import SeleniumTestCaseclassTestHomePage(TestCase,SeleniumTestCase):"""All tests by default will run inside of a database transaction.""" transactions =TruedefsetUp(self):"""Anytime you override the setUp method you must call the setUp method on the parent class like below. """super().setUp()
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.
from masonite.testing import TestCasefrom masonite.testing.selenium import SeleniumTestCaseclassTestHomePage(TestCase,SeleniumTestCase):"""All tests by default will run inside of a database transaction.""" transactions =TruedefsetUp(self):"""Anytime you override the setUp method you must call the setUp method on the parent class like below. """super().setUp() self.useBrowser('chrome', version='74')# self.useBrowser('firefox')
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.
You can chain all methods together to build up and mock user actions. An example test might look like this:
deftest_can_login_to_pypi(self): (self.visit('https://pypi.org/').clickLink('Log in').assertSee('Log in to PyPI').text('#username', 'josephmancuso').text('#password', 'secret').submit().assertCantSee('Your projects'))
Selectors
When finding a selector you can use a few symbols to help navigate the page
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:
from masonite.testing import TestCasefrom masonite.testing.selenium import SeleniumTestCaseclassTestHomePage(TestCase,SeleniumTestCase):"""All tests by default will run inside of a database transaction.""" transactions =True unique_attribute ='selenium'defsetUp(self):"""Anytime you override the setUp method you must call the setUp method on the parent class like below. """super().setUp() self.useBrowser('chrome', version='74')# self.useBrowser('firefox')
and finally you can select by that attribute using the @ symbol:
Below are the available methods you can use to build your tests.
visit
This method will navigate to a URL
self.visit('/')
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