make receiveMessage more robust by checking first if the message is not already in the timeline

This commit is contained in:
Bruno Windels 2018-09-13 12:04:18 +02:00
parent 239e6a4bce
commit 9f4cf776c5

View file

@ -46,23 +46,38 @@ module.exports.receiveMessage = async function(session, expectedMessage) {
session.log.step(`receives message "${expectedMessage.body}" from ${expectedMessage.sender}`); session.log.step(`receives message "${expectedMessage.body}" from ${expectedMessage.sender}`);
// wait for a response to come in that contains the message // wait for a response to come in that contains the message
// crude, but effective // crude, but effective
await session.page.waitForResponse(async (response) => {
if (response.request().url().indexOf("/sync") === -1) { async function assertLastMessage() {
return false; const lastTile = await getLastEventTile(session);
} const lastMessage = await getMessageFromEventTile(lastTile);
const body = await response.text(); await assertMessage(lastMessage, expectedMessage);
if (expectedMessage.encrypted) { }
return body.indexOf(expectedMessage.sender) !== -1 &&
body.indexOf("m.room.encrypted") !== -1; // first try to see if the message is already the last message in the timeline
} else { let isExpectedMessage = false;
return body.indexOf(expectedMessage.body) !== -1; try {
} assertLastMessage();
}); isExpectedMessage = true;
// wait a bit for the incoming event to be rendered } catch(ex) {}
await session.delay(1000);
const lastTile = await getLastEventTile(session); if (!isExpectedMessage) {
const foundMessage = await getMessageFromEventTile(lastTile); await session.page.waitForResponse(async (response) => {
assertMessage(foundMessage, expectedMessage); if (response.request().url().indexOf("/sync") === -1) {
return false;
}
const body = await response.text();
if (expectedMessage.encrypted) {
return body.indexOf(expectedMessage.sender) !== -1 &&
body.indexOf("m.room.encrypted") !== -1;
} else {
return body.indexOf(expectedMessage.body) !== -1;
}
});
// wait a bit for the incoming event to be rendered
await session.delay(1000);
await assertLastMessage();
}
session.log.done(); session.log.done();
} }