About Article:
This article describes how to check if a variable contains either of two specific values.
Description :
Find if the input text/variable contains any of the specified keywords.
Solution:
Create an extension that takes a variable/text and keywords and checks whether any of the keywords are present in the variable/text.
Here's the code to be used to create the extension:
function checkString(inputString, expected1, expected2) {
// Input validation
if (typeof inputString !== 'string' || typeof expected1 !== 'string' || typeof expected2 !== 'string') {
return 'Invalid input types';
}
// Check if either of the expected values is present in the input string
const containsExpected1 = inputString.includes(expected1);
const containsExpected2 = inputString.includes(expected2);
// Message based on the result
if (containsExpected1 && containsExpected2) {
return `String contains both expected values: "${expected1}" and "${expected2}"`;
}
else if (containsExpected1
{
return `String contains the expected value: "${expected1}"`;
}
else if (containsExpected2)
{
return `String contains the expected value: "${expected2}"`;
}
else
{
return 'The string does not contain any of the expected values';
}
}
const resultMessage = checkString(inputString, expected1, expected2);
return resultMessage;
Explanation:
Function, “checkString”, takes three “string” arguments: “inputString”,”expected1”, and “expected2”, It checks if either “expected1” or “expected2” is present in the “inputString”.
Depending on the result, it returns a message:
- If both substrings are present, it returns: 'String contains both expected values: "expected1" and "expected2"'.
- If only expected1 is present, it returns: 'String contains the expected value: "expected1"'.
- If only expected2 is present, it returns: 'String contains the expected value: "expected2"'.
- If neither substring is present, it returns: 'The string does not contain any of the expected values'.
Steps to Use the Extension:
1. Go to the Extensions section.
2. Create a New Extension.
3. Choose the appropriate access level: Project or Organization.
a.Project - Permits access at the project level.
b.Organization - Allows access across the entire organization.
4. Provide the Extension name and click "Create".
5. Copy and paste the following code into the editor:
6. Click the “Add” icon to create inputs for the arguments (“inputString”, “expected1”,
“expected2”).
7. Click the “Save” button
How to use the Extension:
1. Open the Journey where the step needs to be created.
2. Add a new step to call the extension using its name
Example: assertKeyWords() returning $feedbackMessage
3. Save the test step.
4. In the right-side panel under "Step Editor", scroll to the “Inputs” section.
Note: Inputs can be a Variable or Static Data.
- Pass the inputs:
-
- Use `$` for variables.
- Use `A` for static text.
5. Pass all the inputs under the Input sections.
The following steps will provide you with a better understanding of how an extension
works.
6. Click on the extension’s test step.
7. The "Side effects" tab in the right-side panel for results.
Comments
0 comments
Please sign in to leave a comment.