If your mobile testing goals cannot be accomplished using low-code options in the Trainer, mabl gives you the flexibility to customize your tests using snippets. Snippets are custom scripts that run as test steps. In mobile tests, you can write custom scripts using Appium or JavaScript. This article identifies use cases for Appium and JavaScript snippets.
Appium snippets
Write custom code snippets using Appium to interact with your Android or iOS applications under test. Appium snippets are useful when an app-specific edge case can’t be easily covered with the native low-code functionality provided in the Trainer.
You can save Appium snippets as reusable snippets to reuse in other mobile tests.
See our GitHub repo for examples of Appium snippets.
To interact with a mobile application in an Appium snippet, the snippet must first get the driver object using const driver = await mabl.mobile.getDriver()
. For example, the following snippet makes an asynchronous call to doubleTapTargetByXPath()
, accesses the driver object, locates the element using the .findElement()
method, and double taps on it.
function mablJavaScriptStep(mablInputs, callback, element_text = 'New account form') { doubleTapTargetByXpath(`//*[@text="${element_text}" or @content-desc="${element_text}" or @label="${element_text}"]`) .then((result) = callback(`Tapped element with text: "${element_text}"`)) .catch((error) = callback(error)); } async function doubleTapTargetByXpath(element_xpath) { const driver = await mabl.mobile.getDriver(); try { const target = await driver.findElement("xpath", element_xpath); await target.click(); await target.click(); } catch (error) { return error; } }
Helpful resources
When creating Appium snippets, you may find the following resources helpful:
- For a list of methods that are available through the Appium driver object , refer to Appium documentation.
- If you want to target elements in your Appium snippet with XPath, the debug view in the mabl Trainer can help you identify attributes to use for your XPath expression.
- Leverage generative AI. The AI Snippet Generator can create snippets that access the Appium driver and perform actions in the app.
JavaScript snippets
JavaScript snippets do not interact with mobile app elements. They are useful for accomplishing other advanced use cases, such as variable generation, manipulation, and querying external APIs.
When you save a JavaScript snippet, you can reuse it again in other mobile and browser tests.
The following JavaScript snippet uses the predefined "mablJavaScriptStep" function to convert the value of the text parameter to lowercase.
function mablJavaScriptStep(mablInputs, callback,text = "{{@text}}") { let lowerCase = text.toLowerCase() callback(lowerCase) }
Learn more
For more information on snippets, check out the following pages:
- Working with snippets: how to create, import, and use snippets in your tests
- The Snippet Editor: overview of how the different parts of the Snippet Editor work
- JavaScript snippets: introduction to JavaScript snippets
- Mabl snippet repo: GitHub repo with snippets created by mabl and the mabl community
- JavaScript snippet examples
- Working with dates in mabl: how to format dates using the GetDateComponents.js snippet