diff --git a/packages/cli/templates/support.js b/packages/cli/templates/support.js index bf0e95f9..cf2d47cd 100644 --- a/packages/cli/templates/support.js +++ b/packages/cli/templates/support.js @@ -1,3 +1,6 @@ import '@dhis2/cypress-commands' +// You can use helpers of `@dhis2/cypress-commands` directly: +// import { enableAutoLogin } from '@dhis2/cypress-commands' +// enableAutoLogin() // Add additional support functions here diff --git a/packages/cypress-commands/src/commands/find.js b/packages/cypress-commands/src/commands/find.js index 8d17ba6e..673bde12 100644 --- a/packages/cypress-commands/src/commands/find.js +++ b/packages/cypress-commands/src/commands/find.js @@ -14,3 +14,5 @@ export const find = (originalFn, subject, selectors, options = {}) => { const selector = parseSelectorWithDataTest(selectors, prefix) return originalFn(subject, selector, restOptions) } + +Cypress.Commands.overwrite('find', find) diff --git a/packages/cypress-commands/src/commands/get.js b/packages/cypress-commands/src/commands/get.js index 43141dc7..e67885c1 100644 --- a/packages/cypress-commands/src/commands/get.js +++ b/packages/cypress-commands/src/commands/get.js @@ -14,3 +14,5 @@ export const get = (originalFn, selectors, options = {}) => { const selector = parseSelectorWithDataTest(selectors, prefix) return originalFn(selector, restOptions) } + +Cypress.Commands.overwrite('get', get) diff --git a/packages/cypress-commands/src/commands/login.js b/packages/cypress-commands/src/commands/login.js index 91850b29..0f17daef 100644 --- a/packages/cypress-commands/src/commands/login.js +++ b/packages/cypress-commands/src/commands/login.js @@ -1,12 +1,17 @@ -const LOGIN_END_POINT = 'dhis-web-commons-security/login.action' +export const LOGIN_ENDPOINT = 'dhis-web-commons-security/login.action' -const loginBasicAuth = () => { +/** + * This is done through cy.request(...) + * because Cypress doesn't allow multiple domains per test: + * https://docs.cypress.io/guides/guides/web-security.html#One-Superdomain-per-Test + */ +export const login = () => { const username = Cypress.env('dhis2_username') const password = Cypress.env('dhis2_password') const loginUrl = Cypress.env('dhis2_base_url') - return cy.request({ - url: `${loginUrl}/${LOGIN_END_POINT}`, + cy.request({ + url: `${loginUrl}/${LOGIN_ENDPOINT}`, method: 'POST', form: true, followRedirect: true, @@ -18,40 +23,4 @@ const loginBasicAuth = () => { }) } -const loginDev = () => { - cy.visit('/') - cy.get('body') - .then($body => { - if (!$body.find('input#server').length) return cy.wrap(false) - - const loginUrl = Cypress.env('dhis2_base_url') - const username = Cypress.env('dhis2_username') - const password = Cypress.env('dhis2_password') - - cy.get('#server').type(loginUrl) - cy.get('#j_username').type(username) - cy.get('#j_password').type(password) - cy.get('{loginsubmit}', { prefix: 'dhis2-adapter' }).click() - - return cy.wrap(true) - }) - .then(found => { - if (found) return cy.wrap(true) - loginBasicAuth() - .its('body') - .should('not.include', 'class="loginPage"') - return cy.wrap(true) - }) - .then(() => { - return cy - }) -} - -/** - * This is done through cy.request(...) - * because Cypress doesn't allow multiple domains per test: - * https://docs.cypress.io/guides/guides/web-security.html#One-Superdomain-per-Test - */ -export const login = () => { - return loginDev() -} +Cypress.Commands.add('login', login) diff --git a/packages/cypress-commands/src/commands/registerCommands.js b/packages/cypress-commands/src/commands/registerCommands.js deleted file mode 100644 index bf4f8b59..00000000 --- a/packages/cypress-commands/src/commands/registerCommands.js +++ /dev/null @@ -1,13 +0,0 @@ -import { find } from './find' -import { get } from './get' -import { login } from './login' -import { stubWithFixture } from './stubWithFixture' -import { visitWhenStubbed } from './visitWhenStubbed' - -export const registerCommands = () => { - Cypress.Commands.overwrite('find', find) - Cypress.Commands.overwrite('get', get) - Cypress.Commands.add('login', login) - Cypress.Commands.add('stubWithFixture', stubWithFixture) - Cypress.Commands.add('visitWhenStubbed', visitWhenStubbed) -} diff --git a/packages/cypress-commands/src/commands/stubWithFixture.js b/packages/cypress-commands/src/commands/stubWithFixture.js index a0cfbf2d..c541db93 100644 --- a/packages/cypress-commands/src/commands/stubWithFixture.js +++ b/packages/cypress-commands/src/commands/stubWithFixture.js @@ -12,3 +12,5 @@ export const stubWithFixture = ({ method = 'GET', url, fixture }) => { response: `fixture:${fixture}`, }) } + +Cypress.Commands.add('stubWithFixture', stubWithFixture) diff --git a/packages/cypress-commands/src/commands/visitWhenStubbed.js b/packages/cypress-commands/src/commands/visitWhenStubbed.js index 62213b91..a3147efa 100644 --- a/packages/cypress-commands/src/commands/visitWhenStubbed.js +++ b/packages/cypress-commands/src/commands/visitWhenStubbed.js @@ -12,3 +12,5 @@ export const visitWhenStubbed = (url, options = {}) => { }, }) } + +Cypress.Commands.add('visitWhenStubbed', visitWhenStubbed) diff --git a/packages/cypress-commands/src/index.js b/packages/cypress-commands/src/index.js index bd26c9b2..4d33623e 100644 --- a/packages/cypress-commands/src/index.js +++ b/packages/cypress-commands/src/index.js @@ -1,6 +1,20 @@ +// will automatically create the commands +import './commands/find' +import './commands/get' +import './commands/login' +import './commands/stubWithFixture' +import './commands/visitWhenStubbed' + // helpers export { dataTestNameToSelector } from './helper/dataTestNameToSelector' export { parseSelectorWithDataTest } from './helper/parseSelectorWithDataTest' -// commands -export { registerCommands } from './commands/registerCommands' +// setup helpers +export { enableAutoLogin } from './setups/enableAutoLogin' + +// backward compatibility +export const registerCommands = () => { + cy.log( + 'The usage of `registerCommands` has been deprecated. It is now a no-op. Commands are registered automatically.' + ) +} diff --git a/packages/cypress-commands/src/setups/enableAutoLogin.js b/packages/cypress-commands/src/setups/enableAutoLogin.js new file mode 100644 index 00000000..f75e6b51 --- /dev/null +++ b/packages/cypress-commands/src/setups/enableAutoLogin.js @@ -0,0 +1,16 @@ +/* globals before */ +export const enableAutoLogin = () => { + before(() => { + // Persist this across tests so we don't have to login before each test + Cypress.Cookies.defaults({ whitelist: 'JSESSIONID' }) + cy.login() + }) + + beforeEach(() => { + // This ensures the app platform knows which URL to use even if + // REACT_APP_DHIS2_BASE_URL is undefined It also ensures that the value + // from the cypress env is used instead of REACT_APP_DHIS2_BASE_URL + const loginUrl = Cypress.env('dhis2_base_url') + localStorage.setItem('DHIS2_BASE_URL', loginUrl) + }) +}