Cypress: add a way to specify the prefix for userIDs (#9787)

This helps a lot with debugging tests where there are multiple users.
pull/28217/head
Richard van der Hoff 2022-12-19 13:12:01 +00:00 committed by GitHub
parent 3ec75fdd3c
commit ddfa627ce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -158,8 +158,8 @@ describe("Cryptography", function () {
cy.startSynapse("default") cy.startSynapse("default")
.as("synapse") .as("synapse")
.then((synapse: SynapseInstance) => { .then((synapse: SynapseInstance) => {
cy.initTestUser(synapse, "Alice"); cy.initTestUser(synapse, "Alice", undefined, "alice_");
cy.getBot(synapse, { displayName: "Bob", autoAcceptInvites: false }).as("bob"); cy.getBot(synapse, { displayName: "Bob", autoAcceptInvites: false, userIdPrefix: "bob_" }).as("bob");
}); });
}); });

View File

@ -22,6 +22,10 @@ import { Credentials } from "./synapse";
import Chainable = Cypress.Chainable; import Chainable = Cypress.Chainable;
interface CreateBotOpts { interface CreateBotOpts {
/**
* A prefix to use for the userid. If unspecified, "bot_" will be used.
*/
userIdPrefix?: string;
/** /**
* Whether the bot should automatically accept all invites. * Whether the bot should automatically accept all invites.
*/ */
@ -41,6 +45,7 @@ interface CreateBotOpts {
} }
const defaultCreateBotOptions = { const defaultCreateBotOptions = {
userIdPrefix: "bot_",
autoAcceptInvites: true, autoAcceptInvites: true,
startClient: true, startClient: true,
bootstrapCrossSigning: true, bootstrapCrossSigning: true,
@ -153,7 +158,7 @@ function setupBotClient(
Cypress.Commands.add("getBot", (synapse: SynapseInstance, opts: CreateBotOpts): Chainable<MatrixClient> => { Cypress.Commands.add("getBot", (synapse: SynapseInstance, opts: CreateBotOpts): Chainable<MatrixClient> => {
opts = Object.assign({}, defaultCreateBotOptions, opts); opts = Object.assign({}, defaultCreateBotOptions, opts);
const username = Cypress._.uniqueId("userId_"); const username = Cypress._.uniqueId(opts.userIdPrefix);
const password = Cypress._.uniqueId("password_"); const password = Cypress._.uniqueId("password_");
return cy.registerUser(synapse, username, password, opts.displayName).then((credentials) => { return cy.registerUser(synapse, username, password, opts.displayName).then((credentials) => {
cy.log(`Registered bot user ${username} with displayname ${opts.displayName}`); cy.log(`Registered bot user ${username} with displayname ${opts.displayName}`);

View File

@ -37,11 +37,14 @@ declare global {
* @param synapse the synapse returned by startSynapse * @param synapse the synapse returned by startSynapse
* @param displayName the displayName to give the test user * @param displayName the displayName to give the test user
* @param prelaunchFn optional function to run before the app is visited * @param prelaunchFn optional function to run before the app is visited
* @param userIdPrefix optional prefix to use for the generated user id. If unspecified, `user_` will be
* useed.
*/ */
initTestUser( initTestUser(
synapse: SynapseInstance, synapse: SynapseInstance,
displayName: string, displayName: string,
prelaunchFn?: () => void, prelaunchFn?: () => void,
userIdPrefix?: string,
): Chainable<UserCredentials>; ): Chainable<UserCredentials>;
/** /**
* Logs into synapse with the given username/password * Logs into synapse with the given username/password
@ -91,7 +94,12 @@ Cypress.Commands.add(
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
Cypress.Commands.add( Cypress.Commands.add(
"initTestUser", "initTestUser",
(synapse: SynapseInstance, displayName: string, prelaunchFn?: () => void): Chainable<UserCredentials> => { (
synapse: SynapseInstance,
displayName: string,
prelaunchFn?: () => void,
userIdPrefix = "user_",
): Chainable<UserCredentials> => {
// XXX: work around Cypress not clearing IDB between tests // XXX: work around Cypress not clearing IDB between tests
cy.window({ log: false }).then((win) => { cy.window({ log: false }).then((win) => {
win.indexedDB.databases()?.then((databases) => { win.indexedDB.databases()?.then((databases) => {
@ -101,7 +109,7 @@ Cypress.Commands.add(
}); });
}); });
const username = Cypress._.uniqueId("userId_"); const username = Cypress._.uniqueId(userIdPrefix);
const password = Cypress._.uniqueId("password_"); const password = Cypress._.uniqueId("password_");
return cy return cy
.registerUser(synapse, username, password, displayName) .registerUser(synapse, username, password, displayName)