-
Notifications
You must be signed in to change notification settings - Fork 10
/
commands.js
62 lines (53 loc) · 1.91 KB
/
commands.js
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
// https://on.cypress.io/custom-commands
import BackendCaller from "./backend_caller";
Cypress.Commands.add("getBySelector", (selector, ...args) => {
return cy.get(`[data-cy=${selector}]`, ...args);
});
/**
* Expects the subject to be an anchor element `<a>` with a target attribute set
* to "_blank", such that the link opens in a new tab.
* Since we cannot access the new tab in Cypress tests, we remove the target
* attribute from the link and instead just follow the link in the same tab
* to continue testing.
*/
Cypress.Commands.add("clickExpectNewTab", { prevSubject: true }, ($subject, args) => {
const errMsg = "Expected subject to be an anchor element";
expect($subject.is("a"), errMsg).to.be.true;
cy.wrap($subject).should("have.attr", "target", "_blank");
return cy.wrap($subject).invoke("removeAttr", "target").click(args);
});
Cypress.Commands.add("cleanDatabase", () => {
return BackendCaller.callCypressRoute("database_cleaner", "cy.cleanDatabase()", {});
});
beforeEach(() => {
cy.cleanDatabase();
});
Cypress.Commands.add("createUser", (role) => {
if (!["admin", "editor", "teacher", "generic"].includes(role)) {
throw new Error(`Invalid role: ${role}`);
}
return BackendCaller.callCypressRoute("user_creator", "cy.createUser()", { role: role });
});
Cypress.Commands.add("login", (user) => {
return cy.request({
method: "POST",
url: "/users/sign_in",
form: true,
failOnStatusCode: true,
body: {
"user[email]": user.email,
"user[password]": user.password,
},
});
});
Cypress.Commands.add("createUserAndLogin", (role) => {
cy.createUser(role).then((user) => {
cy.login({ email: `${role}@mampf.cypress`, password: "cypress123" }).then((_) => {
cy.wrap(user);
});
});
});
Cypress.Commands.add("i18n", (key, substitutions) => {
return BackendCaller.callCypressRoute("i18n", "cy.i18n()",
{ i18n_key: key, substitutions: substitutions });
});