@cuppet/core 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -249,6 +249,35 @@ class MqttManager {
|
|
|
249
249
|
});
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
/** Wait for specific message on a topic with timeout. IMPORTANT: Message is position,case and whitespace sensitive.
|
|
253
|
+
* @param {string} topic - Topic to wait for message on
|
|
254
|
+
* @param {string} message - Message to wait for
|
|
255
|
+
* @param {number} timeoutSeconds - Timeout in seconds
|
|
256
|
+
* @returns {Promise<Object>} - Resolves with message when received
|
|
257
|
+
*/
|
|
258
|
+
async waitForSpecificMessage(topic, message, timeoutSeconds = 10) {
|
|
259
|
+
const timeoutMs = timeoutSeconds * 1000;
|
|
260
|
+
const startTime = Date.now();
|
|
261
|
+
|
|
262
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
263
|
+
const latestMessage = this.getLatestMessage(topic);
|
|
264
|
+
if (latestMessage) {
|
|
265
|
+
const latestMessageString = JSON.stringify(latestMessage.message);
|
|
266
|
+
const expectedMessageString = JSON.stringify(message);
|
|
267
|
+
if (latestMessageString === expectedMessageString) {
|
|
268
|
+
return latestMessage;
|
|
269
|
+
} else {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`Message: ${latestMessageString} on topic ${topic} does not match expected: ${expectedMessageString}`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
throw new Error(`Timeout waiting for message on topic: ${topic} after ${timeoutSeconds} seconds`);
|
|
279
|
+
}
|
|
280
|
+
|
|
252
281
|
/**
|
|
253
282
|
* Check if client is connected
|
|
254
283
|
* @returns {boolean}
|
|
@@ -77,3 +77,8 @@ Given('I switch back to original window', async function () {
|
|
|
77
77
|
Given('I switch to {string} tab', async function (tabNumber) {
|
|
78
78
|
this.page = await helper.switchToTab(this.browser, tabNumber);
|
|
79
79
|
});
|
|
80
|
+
|
|
81
|
+
Then('I generate UUID and store it in {string} variable', async function (variable) {
|
|
82
|
+
const uuid = helper.generateRandomUuid();
|
|
83
|
+
await dataStorage.iStoreVariableWithValueToTheJsonFile(uuid, variable);
|
|
84
|
+
});
|
|
@@ -69,6 +69,20 @@ Then('I should receive a message on MQTT topic {string}', async function (topic)
|
|
|
69
69
|
await mqttFunctions.validateMessageReceived(this.mqttManager, topic, 10);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Wait for a specific message on a topic with timeout
|
|
74
|
+
* @example Then I should receive the message "Hello World" on MQTT topic "test/response" within 5 seconds
|
|
75
|
+
*/
|
|
76
|
+
Then(
|
|
77
|
+
'I should receive the message {string} on MQTT topic {string} within {int} seconds',
|
|
78
|
+
async function (message, topic, timeout) {
|
|
79
|
+
await mqttFunctions.waitForSpecificMessage(this.mqttManager, topic, message, timeout);
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
Then('I should receive the message {string} on MQTT topic {string}', async function (message, topic) {
|
|
84
|
+
await mqttFunctions.waitForSpecificMessage(this.mqttManager, topic, message, 10);
|
|
85
|
+
});
|
|
72
86
|
/**
|
|
73
87
|
* Validate the content of the latest message on a topic
|
|
74
88
|
* @example Then the MQTT message on topic "test/echo" should equal "Hello World"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cuppet/core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Core testing framework components for Cuppet - BDD framework based on Cucumber and Puppeteer",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"pa11y": "^9.0.0",
|
|
45
45
|
"pa11y-reporter-html": "^2.0.0",
|
|
46
46
|
"puppeteer": "^24.0.1",
|
|
47
|
+
"uuid": "^13.0.0",
|
|
47
48
|
"webdriverio": "9.12.7",
|
|
48
49
|
"xml2js": "^0.6.2"
|
|
49
50
|
},
|
package/src/helperFunctions.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @typedef {import('puppeteer').Browser} Browser
|
|
5
5
|
*/
|
|
6
6
|
const config = require('config');
|
|
7
|
+
const { v4: uuidv4 } = require('uuid');
|
|
8
|
+
|
|
7
9
|
module.exports = {
|
|
8
10
|
/**
|
|
9
11
|
* Waits for a keypress event to continue the test execution.
|
|
@@ -32,6 +34,15 @@ module.exports = {
|
|
|
32
34
|
.substring(2, length + 2);
|
|
33
35
|
},
|
|
34
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Generate random v4 UUID
|
|
39
|
+
* @example 123e4567-e89b-12d3-a456-426614174000
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
generateRandomUuid: function () {
|
|
43
|
+
return uuidv4();
|
|
44
|
+
},
|
|
45
|
+
|
|
35
46
|
/**
|
|
36
47
|
* Iterate through json object and return the value of specific property
|
|
37
48
|
* @param object - the json object to iterate through
|
package/src/mqttFunctions.js
CHANGED
|
@@ -98,6 +98,20 @@ module.exports = {
|
|
|
98
98
|
return await mqttManager.waitForMessage(resolvedTopic, timeoutSeconds);
|
|
99
99
|
},
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Wait for a specific message on a topic with timeout
|
|
103
|
+
* @param {object} mqttManager - MQTT manager instance
|
|
104
|
+
* @param {string} topic - Topic to wait for message on
|
|
105
|
+
* @param {string} message - Message to wait for
|
|
106
|
+
* @param {number} timeoutSeconds - Timeout in seconds
|
|
107
|
+
* @returns {Promise<Object>} - Message object
|
|
108
|
+
*/
|
|
109
|
+
waitForSpecificMessage: async function (mqttManager, topic, message, timeoutSeconds = 10) {
|
|
110
|
+
const resolvedTopic = await this.prepareTopic(topic);
|
|
111
|
+
const resolvedMessage = await this.prepareMessage(message);
|
|
112
|
+
return await mqttManager.waitForSpecificMessage(resolvedTopic, resolvedMessage, timeoutSeconds);
|
|
113
|
+
},
|
|
114
|
+
|
|
101
115
|
/**
|
|
102
116
|
* Get the latest message from a topic
|
|
103
117
|
* @param {object} mqttManager - MQTT manager instance
|