Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1645ee8

Browse files
committed
Add convenience find commands
1 parent 3bd8704 commit 1645ee8

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

cypress/e2e/create-room/create-room.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { HomeserverInstance } from "../../plugins/utils/homeserver";
2020
import Chainable = Cypress.Chainable;
2121

2222
function openCreateRoomDialog(): Chainable<JQuery<HTMLElement>> {
23-
cy.findByRole("button", { name: "Add room" }).click();
24-
cy.findByRole("menuitem", { name: "New room" }).click();
23+
cy.findButton("Add room").click();
24+
cy.findMenuitem("New room").click();
2525
return cy.get(".mx_CreateRoomDialog");
2626
}
2727

@@ -46,15 +46,15 @@ describe("Create Room", () => {
4646

4747
openCreateRoomDialog().within(() => {
4848
// Fill name & topic
49-
cy.findByRole("textbox", { name: "Name" }).type(name);
50-
cy.findByRole("textbox", { name: "Topic (optional)" }).type(topic);
49+
cy.findTextbox("Name").type(name);
50+
cy.findTextbox("Topic (optional)").type(topic);
5151
// Change room to public
52-
cy.findByRole("button", { name: "Room visibility" }).click();
53-
cy.findByRole("option", { name: "Public room" }).click();
52+
cy.findButton("Room visibility").click();
53+
cy.findOption("Public room").click();
5454
// Fill room address
55-
cy.findByRole("textbox", { name: "Room address" }).type("test-room-1");
55+
cy.findTextbox("Room address").type("test-room-1");
5656
// Submit
57-
cy.findByRole("button", { name: "Create room" }).click();
57+
cy.findButton("Create room").click();
5858
});
5959

6060
cy.url().should("contain", "/#/room/#test-room-1:localhost");

cypress/support/e2e.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ import "./network";
3838
import "./composer";
3939
import "./proxy";
4040
import "./axe";
41+
import "./find";

cypress/support/find.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/// <reference types="cypress" />
18+
19+
import Chainable = Cypress.Chainable;
20+
21+
declare global {
22+
// eslint-disable-next-line @typescript-eslint/no-namespace
23+
namespace Cypress {
24+
interface Chainable {
25+
/**
26+
* Finds an element with the role "button".
27+
*
28+
* @param name - accessible name of the element to find
29+
*/
30+
findButton(name: string): Chainable<JQuery>;
31+
/**
32+
* Finds an element with the role "textbox".
33+
*
34+
* @param name - accessible name of the element to find
35+
*/
36+
findTextbox(name: string): Chainable<JQuery>;
37+
/**
38+
* Finds an element with the role "option".
39+
*
40+
* @param name - accessible name of the element to find
41+
*/
42+
findOption(name: string): Chainable<JQuery>;
43+
/**
44+
* Finds an element with the role "menuitem".
45+
*
46+
* @param name - accessible name of the element to find
47+
*/
48+
findMenuitem(name: string): Chainable<JQuery>;
49+
}
50+
}
51+
}
52+
53+
Cypress.Commands.add("findButton", (name: string): Chainable<JQuery> => {
54+
return cy.findByRole("button", { name });
55+
});
56+
57+
Cypress.Commands.add("findTextbox", (name: string): Chainable<JQuery> => {
58+
return cy.findByRole("textbox", { name });
59+
});
60+
61+
Cypress.Commands.add("findOption", (name: string): Chainable<JQuery> => {
62+
return cy.findByRole("option", { name });
63+
});
64+
65+
Cypress.Commands.add("findMenuitem", (name: string): Chainable<JQuery> => {
66+
return cy.findByRole("menuitem", { name });
67+
});
68+
69+
// Needed to make this file a module
70+
export {};

docs/cypress.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ creation that can be called to set up tests.
157157
### Try to write tests from the users's perspective
158158

159159
Like for instance a user will not look for a button by querying a CSS selector. Instead you should work
160-
with roles / labels etc.. You can make use of `findBy…` queries provided by
161-
[Cypress Testing Library](https://github.com/testing-library/cypress-testing-library).
160+
with roles / labels etc.. You can make use of `cy.findBy…` queries provided by
161+
[Cypress Testing Library](https://github.com/testing-library/cypress-testing-library) and some convencience
162+
commands, such as `findButton(name)` or `findTextbox(name)`.
163+
See [`/cypress/support/find.ts`](../cypress/support/find.ts) for a complete list.
162164

163165
### Using matrix-js-sdk
164166

0 commit comments

Comments
 (0)