]>
Commit | Line | Data |
---|---|---|
e306af50 TL |
1 | declare global { |
2 | namespace Cypress { | |
3 | interface Chainable<Subject> { | |
1e59de90 | 4 | login(username?: string, password?: string): void; |
2a845540 | 5 | logToConsole(message: string, optional?: any): void; |
e306af50 | 6 | text(): Chainable<string>; |
1e59de90 | 7 | ceph2Login(username?: string, password?: string): Chainable<any>; |
39ae355f | 8 | checkAccessibility(subject: any, axeOptions?: any, skip?: boolean): void; |
e306af50 TL |
9 | } |
10 | } | |
11 | } | |
2a845540 TL |
12 | // Disabling tslint rule since cypress-cucumber has |
13 | // issues with absolute import paths. | |
14 | // This can be removed when | |
15 | // https://github.com/cypress-io/cypress-browserify-preprocessor/issues/53 | |
16 | // is fixed. | |
17 | /* tslint:disable*/ | |
18 | import { CdHelperClass } from '../../src/app/shared/classes/cd-helper.class'; | |
19 | import { Permissions } from '../../src/app/shared/models/permissions'; | |
39ae355f | 20 | import { table } from 'table'; |
2a845540 | 21 | /* tslint:enable*/ |
e306af50 TL |
22 | let auth: any; |
23 | ||
24 | const fillAuth = () => { | |
25 | window.localStorage.setItem('dashboard_username', auth.username); | |
e306af50 TL |
26 | window.localStorage.setItem('dashboard_permissions', auth.permissions); |
27 | window.localStorage.setItem('user_pwd_expiration_date', auth.pwdExpirationDate); | |
28 | window.localStorage.setItem('user_pwd_update_required', auth.pwdUpdateRequired); | |
29 | window.localStorage.setItem('sso', auth.sso); | |
30 | }; | |
31 | ||
1e59de90 TL |
32 | Cypress.Commands.add('login', (username, password) => { |
33 | cy.session([username, password], () => { | |
34 | requestAuth(username, password).then((resp) => { | |
e306af50 TL |
35 | auth = resp.body; |
36 | auth.permissions = JSON.stringify(new Permissions(auth.permissions)); | |
37 | auth.pwdExpirationDate = String(auth.pwdExpirationDate); | |
38 | auth.pwdUpdateRequired = String(auth.pwdUpdateRequired); | |
39 | auth.sso = String(auth.sso); | |
40 | fillAuth(); | |
41 | }); | |
1e59de90 TL |
42 | }); |
43 | }); | |
44 | ||
45 | Cypress.Commands.add('ceph2Login', (username, password) => { | |
46 | const url: string = Cypress.env('CEPH2_URL'); | |
47 | cy.session([username, password, url], () => { | |
48 | requestAuth(username, password, url).then((resp) => { | |
49 | auth = resp.body; | |
50 | auth.permissions = JSON.stringify(new Permissions(auth.permissions)); | |
51 | auth.pwdExpirationDate = String(auth.pwdExpirationDate); | |
52 | auth.pwdUpdateRequired = String(auth.pwdUpdateRequired); | |
53 | auth.sso = String(auth.sso); | |
54 | const args = { | |
55 | username: auth.username, | |
56 | permissions: auth.permissions, | |
57 | pwdExpirationDate: auth.pwdExpirationDate, | |
58 | pwdUpdateRequired: auth.pwdUpdateRequired, | |
59 | sso: auth.sso | |
60 | }; | |
61 | // @ts-ignore | |
62 | cy.origin( | |
63 | url, | |
64 | { args }, | |
65 | ({ uname, permissions, pwdExpirationDate, pwdUpdateRequired, sso }: any) => { | |
66 | window.localStorage.setItem('dashboard_username', uname); | |
67 | window.localStorage.setItem('dashboard_permissions', permissions); | |
68 | window.localStorage.setItem('user_pwd_expiration_date', pwdExpirationDate); | |
69 | window.localStorage.setItem('user_pwd_update_required', pwdUpdateRequired); | |
70 | window.localStorage.setItem('sso', sso); | |
71 | } | |
72 | ); | |
73 | }); | |
74 | }); | |
e306af50 TL |
75 | }); |
76 | ||
1e59de90 TL |
77 | function requestAuth(username: string, password: string, url = '') { |
78 | username = username ? username : Cypress.env('LOGIN_USER'); | |
79 | password = password ? password : Cypress.env('LOGIN_PWD'); | |
80 | return cy.request({ | |
81 | method: 'POST', | |
82 | url: !url ? 'api/auth' : `${url}api/auth`, | |
83 | headers: { Accept: CdHelperClass.cdVersionHeader('1', '0') }, | |
84 | body: { username: username, password: password } | |
85 | }); | |
86 | } | |
87 | ||
20effc67 | 88 | // @ts-ignore |
aee94f69 TL |
89 | Cypress.Commands.add('text', { prevSubject: true }, ($element: JQuery<HTMLElement>) => { |
90 | cy.wrap($element).scrollIntoView(); | |
91 | return cy | |
92 | .wrap($element) | |
93 | .invoke('text') | |
94 | .then((text: string) => { | |
95 | return text.toString(); | |
96 | }); | |
e306af50 | 97 | }); |
2a845540 TL |
98 | |
99 | Cypress.Commands.add('logToConsole', (message: string, optional?: any) => { | |
100 | cy.task('log', { message: `(${new Date().toISOString()}) ${message}`, optional }); | |
101 | }); | |
39ae355f TL |
102 | |
103 | // Print cypress-axe violations to the terminal | |
104 | function a11yErrorLogger(violations: any) { | |
105 | const violationData = violations.flatMap(({ id, impact, description, nodes }: any) => { | |
106 | return nodes.flatMap(({ html }: any) => { | |
107 | return [ | |
108 | ['Test', Cypress.currentTest.title], | |
109 | ['Error', id], | |
110 | ['Impact', impact], | |
111 | ['Description', description], | |
112 | ['Element', html], | |
113 | ['', ''] | |
114 | ]; | |
115 | }); | |
116 | }); | |
117 | ||
118 | cy.task('log', { | |
119 | message: table(violationData, { | |
120 | header: { | |
121 | alignment: 'left', | |
122 | content: Cypress.spec.relative | |
123 | } | |
124 | }) | |
125 | }); | |
126 | } | |
127 | ||
128 | Cypress.Commands.add('checkAccessibility', (subject: any, axeOptions?: any, skip?: boolean) => { | |
129 | cy.checkA11y(subject, axeOptions, a11yErrorLogger, skip); | |
130 | }); |