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

Commit 9aade5a

Browse files
authored
Improve cypress logging output (#10845)
... by grouping together the output from some custom commands.
1 parent 2d58489 commit 9aade5a

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

cypress/support/bot.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { ISendEventResponse, MatrixClient, Room } from "matrix-js-sdk/src/m
2020
import { HomeserverInstance } from "../plugins/utils/homeserver";
2121
import { Credentials } from "./homeserver";
2222
import Chainable = Cypress.Chainable;
23+
import { collapseLastLogGroup } from "./log";
2324

2425
interface CreateBotOpts {
2526
/**
@@ -65,6 +66,7 @@ declare global {
6566
* @param opts create bot options
6667
*/
6768
getBot(homeserver: HomeserverInstance, opts: CreateBotOpts): Chainable<CypressBot>;
69+
6870
/**
6971
* Returns a new Bot instance logged in as an existing user
7072
* @param homeserver the instance on which to register the bot user
@@ -78,18 +80,21 @@ declare global {
7880
password: string,
7981
opts: CreateBotOpts,
8082
): Chainable<MatrixClient>;
83+
8184
/**
8285
* Let a bot join a room
8386
* @param cli The bot's MatrixClient
8487
* @param roomId ID of the room to join
8588
*/
8689
botJoinRoom(cli: MatrixClient, roomId: string): Chainable<Room>;
90+
8791
/**
8892
* Let a bot join a room by name
8993
* @param cli The bot's MatrixClient
9094
* @param roomName Name of the room to join
9195
*/
9296
botJoinRoomByName(cli: MatrixClient, roomName: string): Chainable<Room>;
97+
9398
/**
9499
* Send a message as a bot into a room
95100
* @param cli The bot's MatrixClient
@@ -173,15 +178,21 @@ Cypress.Commands.add("getBot", (homeserver: HomeserverInstance, opts: CreateBotO
173178
opts = Object.assign({}, defaultCreateBotOptions, opts);
174179
const username = Cypress._.uniqueId(opts.userIdPrefix);
175180
const password = Cypress._.uniqueId("password_");
181+
Cypress.log({
182+
name: "getBot",
183+
message: `Create bot user ${username} with opts ${JSON.stringify(opts)}`,
184+
groupStart: true,
185+
});
176186
return cy
177187
.registerUser(homeserver, username, password, opts.displayName)
178188
.then((credentials) => {
179-
cy.log(`Registered bot user ${username} with displayname ${opts.displayName}`);
180189
return setupBotClient(homeserver, credentials, opts);
181190
})
182191
.then((client): Chainable<CypressBot> => {
183192
Object.assign(client, { __cypress_password: password });
184-
return cy.wrap(client as CypressBot);
193+
Cypress.log({ groupEnd: true, emitOnly: true });
194+
collapseLastLogGroup();
195+
return cy.wrap(client as CypressBot, { log: false });
185196
});
186197
});
187198

@@ -194,9 +205,21 @@ Cypress.Commands.add(
194205
opts: CreateBotOpts,
195206
): Chainable<MatrixClient> => {
196207
opts = Object.assign({}, defaultCreateBotOptions, { bootstrapCrossSigning: false }, opts);
197-
return cy.loginUser(homeserver, username, password).then((credentials) => {
198-
return setupBotClient(homeserver, credentials, opts);
208+
Cypress.log({
209+
name: "loginBot",
210+
message: `log in as ${username} with opts ${JSON.stringify(opts)}`,
211+
groupStart: true,
199212
});
213+
return cy
214+
.loginUser(homeserver, username, password)
215+
.then((credentials) => {
216+
return setupBotClient(homeserver, credentials, opts);
217+
})
218+
.then((res) => {
219+
Cypress.log({ groupEnd: true, emitOnly: true });
220+
collapseLastLogGroup();
221+
cy.wrap(res, { log: false });
222+
});
200223
},
201224
);
202225

cypress/support/log.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
declare global {
20+
// eslint-disable-next-line @typescript-eslint/no-namespace
21+
namespace Cypress {
22+
// secret undocumented options to Cypress.log
23+
interface LogConfig {
24+
/** begin a new log group; remember to match with `groupEnd` */
25+
groupStart: boolean;
26+
27+
/** end a log group that was previously started with `groupStart` */
28+
groupEnd: boolean;
29+
30+
/** suppress regular output: useful for closing a log group without writing another log line */
31+
emitOnly: boolean;
32+
}
33+
}
34+
}
35+
36+
/** collapse the last open log group in the Cypress UI
37+
*
38+
* Credit to https://j1000.github.io/blog/2022/10/27/enhanced_cypress_logging.html
39+
*/
40+
export function collapseLastLogGroup() {
41+
const openExpanders = window.top.document.getElementsByClassName("command-expander-is-open");
42+
const numExpanders = openExpanders.length;
43+
const el = openExpanders[numExpanders - 1];
44+
if (el) el.parentElement.click();
45+
}

cypress/support/login.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
import Chainable = Cypress.Chainable;
2020
import { HomeserverInstance } from "../plugins/utils/homeserver";
21+
import { collapseLastLogGroup } from "./log";
2122

2223
export interface UserCredentials {
2324
accessToken: string;
@@ -100,6 +101,7 @@ Cypress.Commands.add(
100101
prelaunchFn?: () => void,
101102
userIdPrefix = "user_",
102103
): Chainable<UserCredentials> => {
104+
Cypress.log({ name: "initTestUser", groupStart: true });
103105
// XXX: work around Cypress not clearing IDB between tests
104106
cy.window({ log: false }).then((win) => {
105107
win.indexedDB.databases()?.then((databases) => {
@@ -140,6 +142,13 @@ Cypress.Commands.add(
140142
// wait for the app to load
141143
return cy.get(".mx_MatrixChat", { timeout: 30000 });
142144
})
145+
.then(() => {
146+
Cypress.log({
147+
groupEnd: true,
148+
emitOnly: true,
149+
});
150+
collapseLastLogGroup();
151+
})
143152
.then(() => ({
144153
password,
145154
username,

0 commit comments

Comments
 (0)