Skip to content

Commit 710730c

Browse files
committed
Refactor other actions into files
1 parent 366c32b commit 710730c

File tree

3 files changed

+146
-52
lines changed

3 files changed

+146
-52
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2022 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+
export function idle(): void {
20+
cy.wait(5000);
21+
}
22+
23+
export function wait(data: any): string {
24+
const time = data["time"]? parseInt(data["time"], 10): 5000;
25+
cy.wait(time);
26+
return "wait_over";
27+
}
28+
29+
export function advanceClock(data: any): string {
30+
cy.clock().tick(data["milliseconds"]);
31+
return "advanced_clock";
32+
}
33+
34+
export function clearIDBStorage(): string {
35+
cy.window().then((window) => {
36+
return window.indexedDB.databases().then(async databases => {
37+
const promises: Promise<void>[] = [];
38+
const databaseNames: string[] = databases
39+
.map((db) => db.name)
40+
.filter((name) => name !== undefined) as string[];
41+
for (const name of databaseNames) {
42+
cy.log("Deleting indexedDb database", name);
43+
const request = window.indexedDB.deleteDatabase(name);
44+
const promise: Promise<void> = new Promise((resolve) => {
45+
request.onsuccess = () => { resolve(); };
46+
});
47+
promises.push(promise);
48+
}
49+
// await Promise.all(promises);
50+
});
51+
});
52+
cy.wait(5000);
53+
return "storage_cleared";
54+
}
55+
56+
export function exit(): void {
57+
cy.log('Client asked to exit, test complete or server teardown');
58+
}
59+
60+
export function reload(): string {
61+
cy.visit("/");
62+
return "reloaded";
63+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2022 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+
export function sendMessage(data: any): string {
20+
cy.get(".mx_SendMessageComposer div[contenteditable=true]")
21+
.click()
22+
.type(data["message"])
23+
.type("{enter}");
24+
//cy.contains(data['message']).closest('mx_EventTile').should('have.class', 'mx_EventTile_receiptSent');
25+
return "message_sent";
26+
}
27+
28+
export function verifyMessageInTimeline(data: any): string {
29+
cy.contains(data["message"]);
30+
return "verified";
31+
}
32+
33+
export function verifyLastMessageIsUTD(): string {
34+
// verifies that the last tile is an UTD
35+
cy.get(".mx_EventTile").then((elements) => {
36+
const lastEventTile = Array.isArray(elements) ? elements[elements.length - 1] : elements;
37+
cy.get(".mx_UnknownBody", { withinSubject: lastEventTile });
38+
});
39+
cy.get(".mx_UnknownBody");
40+
return "verified";
41+
}
42+
43+
export function verifyLastMessageIsTrusted(): string {
44+
cy.get(".mx_EventTile")
45+
.last()
46+
.find(".mx_EventTile_e2eIcon").should("not.exist");
47+
return "verified";
48+
}

cypress/e2e/trafficlight/trafficlight.spec.ts

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
/// <reference types='cypress' />
2020

2121
import { login, logout, register } from "./actions/auth";
22+
import { advanceClock, clearIDBStorage, exit, idle, reload, wait } from "./actions/browser";
2223
import {
2324
acceptCrossSigningRequest,
2425
enableDehydratedDevice,
@@ -35,6 +36,12 @@ import {
3536
inviteUser,
3637
openRoom,
3738
} from "./actions/room";
39+
import {
40+
sendMessage,
41+
verifyLastMessageIsTrusted,
42+
verifyLastMessageIsUTD,
43+
verifyMessageInTimeline,
44+
} from "./actions/timeline";
3845

3946
type JSONValue =
4047
| string
@@ -76,7 +83,13 @@ function recurse() {
7683
const data: JSONValue = resp.body.data;
7784
const action: string = resp.body.action;
7885
cy.log('running action', action, JSON.stringify(data));
79-
const result = runAction(action, data);
86+
let result;
87+
try {
88+
result = runAction(action, data);
89+
} catch (e) {
90+
// Don't keep running if we encounter an error!
91+
return;
92+
}
8093
if (result) {
8194
sendResponse(result);
8295
}
@@ -124,64 +137,34 @@ function runAction(action: string, data: JSONValue): string | undefined {
124137
case "invite_user":
125138
return inviteUser(data);
126139

127-
case 'idle':
128-
cy.wait(5000);
129-
break;
140+
// Timeline
130141
case 'send_message':
131-
cy.get('.mx_SendMessageComposer div[contenteditable=true]')
132-
.click()
133-
.type(data['message'])
134-
.type("{enter}");
135-
//cy.contains(data['message']).closest('mx_EventTile').should('have.class', 'mx_EventTile_receiptSent');
136-
return "message_sent";
142+
return sendMessage(data);
137143
case "verify_message_in_timeline":
138-
cy.contains(data["message"]);
139-
return "verified";
140-
case 'wait': {
141-
const time = data["time"]? parseInt(data["time"], 10): 5000;
142-
cy.wait(time);
143-
return "wait_over";
144-
}
145-
case "advance_clock": {
146-
cy.clock().tick(data["milliseconds"]);
147-
return "advanced_clock";
148-
}
144+
return verifyMessageInTimeline(data);
149145
case "verify_last_message_is_utd":
150-
// verifies that the last tile is an UTD
151-
cy.get(".mx_EventTile").then((elements) => {
152-
const lastEventTile = Array.isArray(elements) ? elements[elements.length - 1] : elements;
153-
cy.get(".mx_UnknownBody", { withinSubject: lastEventTile });
154-
});
155-
cy.get(".mx_UnknownBody");
156-
return "verified";
157-
case "verify_last_message_is_trusted": {
158-
cy.get(".mx_EventTile")
159-
.last()
160-
.find(".mx_EventTile_e2eIcon").should("not.exist");
161-
return "verified";
162-
}
146+
return verifyLastMessageIsUTD();
147+
case "verify_last_message_is_trusted":
148+
return verifyLastMessageIsTrusted();
149+
150+
// Browser
151+
case 'idle':
152+
idle();
153+
break;
154+
case 'wait':
155+
return wait(data);
156+
case "advance_clock":
157+
return advanceClock(data);
163158
case "clear_idb_storage":
164-
cy.window().then((window) => {
165-
return window.indexedDB.databases().then(databases => {
166-
const databaseNames: string[] = databases
167-
.map((db) => db.name)
168-
.filter((name) => name !== undefined) as string[];
169-
for (const name of databaseNames) {
170-
cy.log("Deleting indexedDb database", name);
171-
window.indexedDB.deleteDatabase(name);
172-
}
173-
});
174-
});
175-
return "storage_cleared";
159+
return clearIDBStorage();
176160
case "reload":
177-
cy.visit("/");
178-
// cy.reload();
179-
return "reloaded";
161+
return reload();
180162
case 'exit':
181-
cy.log('Client asked to exit, test complete or server teardown');
182-
return;
163+
exit();
164+
break;
165+
183166
default:
184167
cy.log('WARNING: unknown action ', action);
185-
return;
168+
break;
186169
}
187170
}

0 commit comments

Comments
 (0)