UNPKG

qe-fe-automation

Version:
125 lines (109 loc) 3.69 kB
import { Interception } from 'cypress/types/net-stubbing'; import { utilitySelector } from '@utility-lib/index'; import { HttpRequest, HttpStatusCodes } from './base'; import compareSnapshotCommand from 'cypress-visual-regression/dist/command'; import 'cypress-iframe'; import { AdminClaim } from '@growth-lib/admin-claim'; export let AUTH_TOKEN: string; compareSnapshotCommand(); export interface LoginParams { email: string; password: string; isSA?: boolean; localStorage?: boolean; } Cypress.Commands.add('logTaRequestUuid', (interception: Interception) => { cy.allure().logStep( `Request URL: [${interception.request.url}] --> ta-request-uuid: [${interception.response?.headers['ta-request-uuid']}]` ); }); Cypress.Commands.add('loginWithAuthToken', (loginParams: LoginParams) => { const { email, isSA = false, localStorage = true } = loginParams; cy.log(`logging in with ${email}`); cy.clearLocalStorage(); Cypress.session.clearAllSavedSessions(); cy.session([email, isSA, localStorage], () => { loginRequest(loginParams); }); }); Cypress.Commands.add('setAuthToken', (loginParams: LoginParams) => { loginRequest(loginParams); }); Cypress.Commands.add('verifyListContainsText', (selector, list) => { cy.get(selector).each((item, index) => { cy.wrap(item).should('contain.text', list[index]); }); }); Cypress.Commands.add('verifyListContainsAttributeQaid', (selector, attr, list) => { cy.get(selector).each((item, index) => { cy.wrap(item).should('attr', attr, list[index]); }); }); Cypress.Commands.add('verifySelectorsContainsAttributeQaid', (attribute, titles) => { titles.forEach((title) => { cy.get(`[${attribute}="${title}"]`).each((item) => { cy.wrap(item).should('be.visible'); }); }); }); Cypress.Commands.add('clickIfExist', (selector, timeout?: Cypress.Timeoutable) => { cy.get(utilitySelector.bodySelector).then((body) => { if (body.find(selector).length) { cy.get(selector, timeout).should('be.visible').click(); } }); }); Cypress.Commands.add('checkIfExist', (selector) => { cy.get(utilitySelector.bodySelector).then((body) => { return !!body.find(selector).length; }); }); export function loginRequest(loginParams: LoginParams) { const { email, password, isSA = false, localStorage = true } = loginParams; cy.allure().logStep(`login with ${email}`); cy.request({ method: 'POST', url: `/api/uaa/token?isSA=${isSA}`, auth: { user: email, pass: password }, retryOnStatusCodeFailure: true }).then(({ body, status }) => { expect(status, 'Successfully login').to.equal(200); AUTH_TOKEN = 'TripActions ' + body.token; cy.allure().logStep(`Successfully login with ${email}`); if (localStorage) window.localStorage.setItem('tripactions.TripActionsToken', body.token); }); } Cypress.Commands.add('clearInputAndType', (selector, input) => { cy.get(selector).then((button) => { cy.wrap(button).clear(); cy.wrap(button).type(input); }); }); Cypress.Commands.add('updateSplits', () => { cy.allure().logStep('Update splits'); return cy .request({ method: HttpRequest.Get, url: `api/user/split/getsplits`, headers: { Authorization: AdminClaim.interceptedToken ? 'TripActions ' + AdminClaim.interceptedToken : AUTH_TOKEN }, failOnStatusCode: true }) .then(({ body, status }) => { expect(status, 'Updated splits').to.equal(HttpStatusCodes.Ok); return cy.task('putDataInCache', { key: 'splitList', data: body }); }); }); export function getUserBaseUrlPrefix(): string { return 'app/user2'; }