While JavaScript snippets are a great tool for handling more complex testing scenarios, such as working with date formats, sometimes writing code that accomplishes exactly what you want is a challenge. With GenAI Script Generation, you can describe what you want the snippet to do and let generative AI handle the code.
This article identifies best practices for working with the AI snippet generator:
GenAI Script Generation for API tests
mabl also supports GenAI Script Generation for API tests. For more information, see the article on custom scripts in API tests.
Write a clear prompt
Start with a clear prompt that defines what you want the snippet to accomplish and provide examples when appropriate. To ensure your prompt yields the desired output, include samples of valid and invalid results. Consider the following sample prompt for returning a random date within the last 20 years:
"Return a random date within the last 20 years in the format MM/DD/YYYY. June 1, 2023 would be returned as 06/01/2023. A response of 06/01/2035 is invalid because it is in the future. A response of 14/35/2020 is invalid because the month and day fields are out of range."
For more examples, consider the following prompts:
- "Return the ID from the current URL that starts with mbl. For example, if the current URL is https://www.example.com/users/12345/?id=mbl309jd3, the result should be mbl309jd3"
- "Return a string starting with PO, then 2 random numbers, then 3 random letters"
- "Return a random date of birth in the format MM/DD/YYYY for someone between the ages of 18 and 65"
- "Count all checked checkboxes on the page. Return the number of checked checkboxes."
- "Scroll down the bottom of the page, then scroll back to the top of the page."
Writing prompts that interact with the page
The AI Snippet Generator can create snippets that interact with the page through the DOM (web) or the Appium driver object (mobile). However, the AI Snippet Generator does not have access to the HTML or page source. When describing what you want the snippet to accomplish, use general phrasing instead of business terms that are specific to your application.
For example, if the prompt is “Check the box for the most recent insurance claim on the page”, the AI Snippet Generator will not know what represents an “insurance claim” on the page. If you rephrase the prompt as “Check the box associated with the item in the list with the most recent date,” the AI Snippet Generator can generate a generic snippet that performs this action via the DOM or the Appium driver object.
Add parameters before generating the snippet
Parameters are a great way to make the snippet easier to modify and reuse across tests. To generate parameterized snippets with AI, add the parameters before writing the prompt, and make sure the prompt references the parameters.
The following table shows examples of how to reference parameters in a prompt:
Parameter | Prompt |
id | Click on the checkbox that matches the value of "id" and return "Checked!" |
token | Make an API call using the token provided |
valueToUpperCase | Return valueToUpperCase in all caps |
Iterate as needed
The AI snippet generator can iterate on an existing snippet in the code editor. So if the initial output from the AI snippet generator isn't what you want, continue to provide direction and guidance until the response is satisfactory.
For example, if you wrote the prompt "Return today's date", and the AI snippet generator returned the date in MM/DD/YYYY format, you could submit a followup prompt to modify the existing snippet: "Return the date in DD/MM/YYYY format instead."
Want to go back to an earlier snippet? If you didn't save your work, click into the code editor and use the CTRL + Z/CMD + Z keyboard shortcut to undo changes.