This project was bootstrapped with Create React App.
This application uses sessionStorage
to store authentication (login) information. During the
login process, a token is stored in sessionStorage. The presence of this token indicate a user
has successfully logged into the application. The absence of this token indicates the login
screen should be presented to the user.
This simple application has two pages:
/welcome
- does not require authentication/secondPage
- requires authentication (a token value insessionStorage
)
Navigating to /secondPage
without a prior login will redirect back to the /welcome
page.
We would like to run a Lighthouse audit on both the welcome page ad the second page of this
application. These tests can be found in cypress/integration/test.spec.js
.
To do this, we use the cypress-audit plugin.
After running the tests, the Lighthouse reports for each of the pages can be found in
cypress/test-results
.
Adding a call to cy.lighthouse()
directly within our tests will not run Lighthouse on the
pages behind our sessionStorage
authentication (see cypress-audit issue #121)
since the values in session storage are not transferred over to the new Cypress tab when
Lighthouse is run.
As a result, while running Lighthouse on the second page (the page requiring authentication), the audit is actually run against the welcome page. This can be seen in the lighthouse report's screenshots.
Lighthouse supports adding custom audits. These custom audits are added to the generated Lighthouse reports and can be based on custom metrics gathered from the page via a gatherer. These custom gatherers have some lifecycle hooks that can be used to read or modify the session/tab where the Lighthouse audit is running.
The one of particular use to us is the beforePass
hook, which will run after the new
Cypress window is created, but before the URL is visited. It is at this point where we can
modify the sessionStorage
values before the app is loaded!
See cypress/support/lighthouse-session-gatherer.js
for the custom gatherer code where we add
the session storage values. See cypress/support/lighthouse-session-audit.js
for the custom
audit that depends on this gatherer.
Important Implementation Note: The custom gatherer will not be run unless the following conditions are met:
- The gatherer is listed as a
requiredArtifact
in the custom audit file - The gatherer is listed in the lighthouse config
- The audit is listed in the lighthouse config
- The audit is listed in the lighthouse config categories
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
To launch the Cypress test viewer.
You can learn more in the Create React App documentation.
To learn React, check out the React documentation.