Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/templates/support.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions packages/cypress-commands/src/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions packages/cypress-commands/src/commands/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const get = (originalFn, selectors, options = {}) => {
const selector = parseSelectorWithDataTest(selectors, prefix)
return originalFn(selector, restOptions)
}

Cypress.Commands.overwrite('get', get)
51 changes: 10 additions & 41 deletions packages/cypress-commands/src/commands/login.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
13 changes: 0 additions & 13 deletions packages/cypress-commands/src/commands/registerCommands.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/cypress-commands/src/commands/stubWithFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const stubWithFixture = ({ method = 'GET', url, fixture }) => {
response: `fixture:${fixture}`,
})
}

Cypress.Commands.add('stubWithFixture', stubWithFixture)
2 changes: 2 additions & 0 deletions packages/cypress-commands/src/commands/visitWhenStubbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const visitWhenStubbed = (url, options = {}) => {
},
})
}

Cypress.Commands.add('visitWhenStubbed', visitWhenStubbed)
18 changes: 16 additions & 2 deletions packages/cypress-commands/src/index.js
Original file line number Diff line number Diff line change
@@ -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.'
)
}
16 changes: 16 additions & 0 deletions packages/cypress-commands/src/setups/enableAutoLogin.js
Original file line number Diff line number Diff line change
@@ -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)
})
}