Created
October 12, 2018 07:30
-
-
Save Cloudo/4b3b80cb3d1c486f1c3a65523fcc0f02 to your computer and use it in GitHub Desktop.
cypress utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getElementById = q => Cypress.$(`#${q}`)[0] | |
export const getElementByName = q => Cypress.$(`[name="${q}"]`)[0] | |
export const getElement = q => getElementById(q) || getElementByName(q) | |
export const get = q => cy.get(`#${CSS.escape(q)}`) | |
export const uncheck = q => { | |
const checkbox = getElement(q) | |
if (checkbox && checkbox.checked) { | |
get(q).uncheck({ force: true }) | |
} | |
} | |
export const uploadFile = (q, options = {}) => { | |
const { | |
fileName = 'stub.pdf', | |
mime = 'application/pdf', | |
newName = 'stub.pdf', | |
} = options | |
return cy | |
.fixture(fileName) | |
.as('file') | |
.get(`${q}--wrapper input`) | |
.then(function dispatchEvent(el) { | |
const blob = new Blob([this.file]) | |
// eslint-disable-next-line | |
el[0].fakeFiles = [ | |
new File([blob], newName, { | |
lastModified: new Date(0), | |
type: mime, | |
}), | |
] | |
el[0].dispatchEvent(new Event('change', { bubbles: true })) | |
}) | |
} | |
const GISRD_API_URL = 'http://server.test.rd.altarix.org/rd2' | |
export const saveUserInfo = xsrf => { | |
localStorage.setItem('xsrfToken', xsrf) | |
localStorage.setItem('user', JSON.stringify({ loggedIn: true })) | |
} | |
export const loginAs = (name = '201010001', password = 'password') => | |
cy | |
.request({ | |
method: 'POST', | |
url: `${GISRD_API_URL}/auth/logon`, | |
body: { | |
login: name, | |
password, | |
}, | |
headers: { | |
Accept: 'text/html, application/json;', | |
'Content-Type': 'application/json', | |
}, | |
}) | |
.as('currentUser') | |
.then(response => saveUserInfo(response.body.body)) | |
export const route = (method, url, ...others) => | |
cy.route(method, `${GISRD_API_URL}${url}`, ...others) | |
export const stub = (url, alias, method = 'GET') => { | |
const mockAlias = `${alias}--mock` | |
cy.fixture(`${alias}.json`).as(mockAlias) | |
route(method, url, `@${mockAlias}`).as(alias) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment