- Pre-Requisites
- Getting Cypress Running
- Configuring Cypress Credentials and Environments
- Contributing
- Getting started with writing tests
- Download
- a recent/current version of Node.js
- Optional Download
- A code editor that supports code completion
- Recommendation: VS Code
- Git clone the repo using
git clone https://github.com/ChromaticFlint/cypress-pom-starter
- Navigate into the repo root directory.
- Install the dependencies with
npm install
- In the project root, make a copy of
cypress.env.json
and name itcypress.env-example.json
. - Start Cypress for the environment of test that you would like to interact with the following scripts based on the
package.json
scripts:- Running Local with the UI:
npm run cypress:local
- Running Local headlessly:
npm run cypress:headless
- Running Staging with the UI:
npm run cypress:staging
- Running Staging headlessly:
npm run cypress:staging:headless
- Running Prod with the UI:
npm run cypress:prod
- Running Prod with headlessly:
npm run cypress:prod:headless
- Running Local with the UI:
- Launch your tests from the Cypress test runner if you used the UI, or observe the test running headlessly within the command line interface.
- By default if you run
npm run cypress:prod
thehttps://the-internet.herokuapp.com/
test should run successfully if everything is configured appropriately.
- By default if you run
When working with user credentials you should always avoid committing them to Git/GitHub. Instead, you can distribute an cypress.env-example.json
file with the sensitive data removed. You can then share the necessary info via a password manager or another form. (Not over public slack channels...)
- Rename the
cypress.env-example.json
file tocypress.env.json
- Profit
- Within
cypress.env.json
within credentials, create any set of credentials required. - access these credentials within your tests through
Cypress.env('credentials').<NAMEOFUSER>.email
andCypress.env('credentials').<NAMEOFUSER>.password
respectively
When working with multiple environments of test it is helpful to quickly be able to test different environments locally, or switch environments of test within a CI/CD pipeline. Without odd various of SSO this can be achieved through the package.json
scripts, or with the One Login SSO example included in this repo through both the package.json
script and the cypress.env.json
file.
- Within
package.json
locate the scripts section. - Adjust the
--config baseUrl=<URL>
to the application of test. - Adjust the script accordingly, all scripts are run with
npm run <SCRIPTNAME>
see examples from the getting cypress running section
SSO - Specifically Onelogin with a separate step between username and password entry (I don't recommend this)
- Manually identify in you application if you require the following:
- SSO Authentication, where the SSO service doesn't have an API.
- Use
cy.request()
to authenticate the the SSO API
- Use
- SSO Authetnication, where the SSO service has multiple steps in the log in process
- Use a combination of postman to identify the payload needed to login, and then use
cy.request()
to submit the request.
- Use a combination of postman to identify the payload needed to login, and then use
- SSO Authentication, where the authentication URL is outside of the base domain of test.
- Again... Use
cy.request()
- Also you are going to want to copy that URL, make sure to get each one if they are different per environment.
- Again... Use
- SSO Authentication, where the SSO service doesn't have an API.
- If
cy.request()
still didn't fit your need, I'm sorry lets use puppeteer.
- Move the
/cypress/examples/onelogin-auth-example.spec.ts
file into the integration folder. - Uncomment the
beforeEach()
statement in/cypress/support/index.js
- If an authentication URL is required outside of the application URL.
- Open
cypress.env.json
for each environment add a new key to the environment for the authentication domain.- See example below, Application auth was added to
local
, you will need this for all environments of test. Name itauthUrl
if you are planning on using theonelogin-auth-example
before()
code.
- See example below, Application auth was added to
"domains": {
"local": {
"defaultApplication": "https://localhost:4200/this_is_an_example",
"authUrl": "https://applicationauth.why/"
},
"staging": {
"defaultApplication": "https://staging.the-internet.herokuapp.com/this_is_an_example"
},
"prod": {
"defaultApplication": "https://the-internet.herokuapp.com/"
}
- Open
/cypress/support/getOneLoginAuth.js
and manually perform the SSO Login. Adjust the selectors withingetOneLoginAuth
to the necessary selectors for puppeteer to login. - Finally, adjust the
--config baseUrl=<URL>
to the application of test. - Adjust the script accordingly, all scripts are run with
npm run <SCRIPTNAME>
see examples from the getting cypress running section - There could be a number of things that go wrong here:
- First could be imports from moving files around.
- Second would probably be how the
getOneLogiinAutth.js
is set up, this can go poorly if the site is slow, you might need to set some waits unfortunately. You can troubleshoot by settingheadless
to false on line 6. - Third, funny enough would be if
tsconfig.json
hasallowJs
set totrue
, this will break the puppeteerasync
function. - Fourth could just be the jankey code this is using to perform the auth. 🤷 as I said, I don't recommend this.
- The Cypress automation code organization is leveraging a class inheritance page object model structure.
- The Base page object is located within
<root>/cypress/page-objects/base.ts
- This page object will contain all of the global methods that are required by any page object. The goal of these methods is to extent Cypress' functionality to be easier and debateably faster to write.
- All page-object will inherit from the base page object and be located within the page-objects folder located at
<root>/cypress/page-objects
- These page objects will contain the methods to interact with the application corresponding with each page within the application.
- In each page object will manage and contain all of the selectors and ids for the page elements that would exist on a given page.
- Example page-objects in default suite.
/cypress/page-objects/landing-page.ts
,/cypress/page-objects/checkboxes.ts
- The integration folder will contain all of our tests.
- the integration folder is located at
<root>/cypress/integration
- the integration folder is formatted almost identically to the page-objects model folder so, there is a corresponding director for the compass and atlas application.
- Example test files in default suite.
/cypress/integration/landing-page.spec.ts
,/cypress/integration/checkboxes.spec.ts
- Each individual page will have a
spec.ts
file within the appropriate integration folder if the application is large, it might be helpful to start organizating the spec files per section of the application. This spec file should contain all of tests that occur for that specific page.
- the integration folder is located at
- The Base page object is located within
Contributions to this repository is encouraged and appreciated. If you would like to please create a fork of the repo and submit a PR, or submit an issue. Thank you.