-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
commands.ts
129 lines (117 loc) · 3.14 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// ***********************************************
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
/// <reference types="Cypress" />
type Chainable<Subject = any> = Cypress.Chainable<Subject>;
type User = {
id: number;
username: string;
name: string;
is_admin: boolean;
is_verified: boolean;
};
function getCy(cyName: string): Chainable<JQuery<HTMLElement>> {
return cy.get(`[data-cy=${cyName}]`);
}
/**
* Deletes all users with username starting 'test'.
*/
function serverCommand(
command: "clearTestUsers"
): Chainable<{
success: true;
}>;
/**
* Creates a verified or unverified user, bypassing all safety checks.
* Redirects to `next`.
*
* Default values:
*
* - username: `testuser`
* - email: `${username}@example.com`
* - verified: false
* - name: `${username}`
* - password: `TestUserPassword`
* - next: `/`
*/
function serverCommand(
command: "createUser",
payload: {
username?: string;
email?: string;
verified?: boolean;
name?: string;
password?: string;
next?: string;
}
): Chainable<{
user: User;
userEmailId: number;
verificationToken: string | null;
}>;
/**
* Gets the secrets for the specified email, allowing Cypress to perform email
* validation. If unspecified, email defaults to `testuser@example.com`.
*/
function serverCommand(
command: "getEmailSecrets",
payload?: { email?: string }
): Chainable<{
user_email_id: number;
verification_token: string | null;
}>;
// The actual implementation of the 'serverCommand' function.
function serverCommand(command: string, payload?: any): any {
const url = `${Cypress.env(
"ROOT_URL"
)}/cypressServerCommand?command=${encodeURIComponent(command)}${
payload ? `&payload=${encodeURIComponent(JSON.stringify(payload))}` : ""
}`;
// GET the url, and return the response body (JSON is parsed automatically)
return cy.request(url).its("body");
}
function login(payload?: {
next?: string;
username?: string;
name?: string;
verified?: boolean;
password?: string;
}): Chainable<Window> {
return cy.visit(
Cypress.env("ROOT_URL") +
`/cypressServerCommand?command=login&payload=${encodeURIComponent(
JSON.stringify(payload)
)}`
);
}
Cypress.Commands.add("getCy", getCy);
Cypress.Commands.add("serverCommand", serverCommand);
Cypress.Commands.add("login", login);
export {}; // Make this a module so we can `declare global`
declare global {
namespace Cypress {
interface Chainable {
getCy: typeof getCy;
serverCommand: typeof serverCommand;
login: typeof login;
}
}
}