How to Call the TestFairy SDK with Appium
Sauce AI for Test Authoring: Move from intent to performance in minutes.|xBack to ResourcesBlogPosted<
Sauce AI for Test Authoring: Move from intent to performance in minutes.
|
x
Blog
How to Call the TestFairy SDK with Appium
Our nomadic beta testing resolution can help mobile developer and SDETs meliorate their mobile trial mechanisation. In this clause, we extend how to make Appium scripts talking to the TestFairy SDK.
We ’ ve be busy making our wagerer not only for nomadic developers but also for SDETs who are looking for innovative means to get their awesome. The challenge is how to make Appium handwriting talk to the TestFairy SDK. All of the example code in this post is useable asexposed source, so you can copy and adapt it to your need.
Environment
We ’ ll be utiliseWebdriverIOwith JavaScript but all examples can be port to other guest and languages with little to no change. The only requirement in our drivers is the ability to arouse mobile: startService viaexecute()which is already available on the latest UIAutomator2 driver. Ourexam bed appis too included in the repo.
How it Works
Our solution is based on the principle that Appium can start Android service by constructing purport over adb. These aim can throw additional tilt for services to interpret. We ’ ve created a mere service to parse incoming intent data to decide which TestFairy SDK method to invoke.
Every time Appium starts our service, the service read the intent, invokes the TestFairy SDK and discontinue itself once the work is done. The service we launch will not interrupt any of the running activities, giving us the power to use it without change across various types of apps including video games, social networking, and camera apps.
Here are the steps you need to take:
Copy the “ Appium call TestFairy ” fileinto your Android app project.
Add this line to your app manifest:
& lt; service android: exported= & quot; true & quot; android: name= & quot; .TestFairyService & quot; / & gt;
SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
Add these helpers to your Appium book. As an model, we ply begin (), stop () and addEvent () but you can easily simulate one of these to add an invocation for other methods as you need. Make certain you update TestFairyService to parse your newly added invocation. Update these functions with your package gens by replacing: com.example.appiumcallstestfairy/.TestFairyService with com.your.package.name/.TestFairyService
1/**2* Starts recording a TestFaiy session.3*4* @ param client wdio client5* @ param appToken TestFairy app token can be base at https: //app.testfairy.com/settings6*/7async role begin (node, appToken) {8client.startActivity ()9const args = Buffer.from (JSON.stringify ([& # x27; start & # x27;, appToken])) .toString (& # x27; base64 & # x27;);10await client.execute (& # x27; mobile: startService & # x27;, {11intent: & # x27; -- es & quot; args & quot; & quot; & # x27; + args + & # x27; & quot; com.example.appiumcallstestfairy/.TestFairyService & # x27;,12});13}14/**15* Sends a string case to currently recorded session. It will show up in the session timeline.16* Multiple session that has the same case can be searched at https: //app.testfairy.com/sessions17*18* @ param guest wdio guest19* @ param case Some string value to represent a substantial case hap during tests20*/21async function addEvent (client, event) {22const args = Buffer.from (JSON.stringify ([& # x27; addEvent & # x27;, event])) .toString (& # x27; base64 & # x27;);23await client.execute (& # x27; mobile: startService & # x27;, {24intention: & # x27; -- es & quot; args & quot; & quot; & # x27; + args + & # x27; & quot; com.example.appiumcallstestfairy/.TestFairyService & # x27;,25});26}27/**28* Stops recording a TestFairy session.29*30* @ param client wdio client31*/32async office halt (client) {33const args = Buffer.from (JSON.stringify ([& # x27; stop & # x27;])) .toString (& # x27; base64 & # x27;);34await client.execute (& # x27; mobile: startService & # x27;, {35intent: & # x27; -- es & quot; args & quot; & quot; & # x27; + args + & # x27; & quot; com.example.appiumcallstestfairy/.TestFairyService & # x27;,36});37}38
Then, invoke TestFairy method wherever you need them.
1// Test suite2describe (& # x27; Create TestFairy session & # x27;, function () {3let client;4before (async part () {5client = await webdriverio.remote (androidOptions);6});7it (& # x27; should create and destroy a session & # x27;, async function () {8const res = await client.status ();9assert.isObject (res.build);10const current_package = await client.getCurrentPackage ();11assert.equal (current_package, & # x27; com.example.appiumcallstestfairy & # x27;);12// Start a session13console.log (& quot; Starting a TestFairy session & quot;);14await begin (client, & quot; SDK-XXXXXXX & quot;); // TestFairy app token can be found at https: //app.testfairy.com/settings15// Make your averment16// Mark significant points in time during the session to be able to use them later in your TestFairy splashboard for search or preview17await addEvent (client, & quot; Initial assertions surpass, this will show up in session timeline & quot;);18// Make your affirmation19// Stop session20await stoppage (client);21console.log (& quot; Ending TestFairy session & quot;);22const delete_session = await client.deleteSession ();23assert.isNull (delete_session);24});25});26
What ’ s Inside the TestFairyService?
It is a small service which parses the bundled Intent extra. For simplicity, we pass all arguments as a single twine value, json encode in base64.
1@Override2public int onStartCommand (Intent intent, int masthead, int startId) {3Bundle extras = intent.getExtras ();4if (extras! = null & amp; & amp; extras.containsKey (& quot; args & quot;)) {5try {6final String args = extras.getString (& quot; args & quot;);7final JSONArray argsArray = new JSONArray (new String (Base64.decode (args, Base64.DEFAULT), StandardCharsets.UTF_8));8switch (argsArray.getString (0)) {9example & quot; start & quot;:10TestFairy.begin (getApplicationContext (), argsArray.getString (1));11break;12case & quot; addEvent & quot;:13TestFairy.addEvent (argsArray.getString (1));14break;15case & quot; stop & quot;:16TestFairy.stop ();17break;18default:19break;20}21} catch (JSONException t) {22Log.w (& quot; TestFairyService & quot;, & quot; Can & # x27; t invoke TestFairy & quot;, t);23}24}25stopSelf ();26homecoming super.onStartCommand (purport, flags, startId);27}28
Once the outcry is done, we stop the service so that it does not consume more imagination than necessary.
Win
Here you can chance thecomplete example,include thetest app. Let us know if we can improve it farther. Until so, abide safe and enjoy hacking!
This article was originally issue in August 2021 and has been updated in March 2023.
Sr. Software Engineer
Share this post
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free