Xcode script automation for SauceLabs

Couple months ago, I’ve tried to set a mobile testing environment with Appium and one of the best tools to execute these tests was SauceLabs, a cloud platform dedicated for testing. SauceLabs is pretty easy to use but here is couple tricks to make even easier.

SauceLabs testing environment

To run your Appium tests on SauceLabs, you might need to upload your app as zip file to their Sauce Storage. This space is temporary: your app will be automatically removed from the cloud after 7 days. Everything is in command line, you can upload with one command and list your uploaded archives with another.

If you don’t have yet a continuous integration environment, by using a shell script in “Build Phases” in Xcode, you can actually upload to Sauce Storage automatically. It’s what’s happening if you’re using crash trackers like SplunkMint or NewRelic. Here is a temporary one to work with:

tmpPath=$1
username=$2
accessKey=$3
...

echo 'Zipping app'
zip -r "myApp.zip" "$tmpPath" > /dev/null

echo 'List files created'
ls -l

echo 'Display current md5 hash.'
md5 myApp.zip

echo 'Posting the zip archive to Saucelabs.'
curl -u "$username":"$accessKey" \
    -X POST \
    -H 'Content-Type: application/octet-stream' \
    --data-binary @myApp.zip \
    "https://saucelabs.com/rest/v1/storage/$username/myApp.zip?overwrite=true"

echo 'List zip files from Saucelabs.'
curl -u "$username":"$accessKey" \
    "https://saucelabs.com/rest/v1/storage/$username"

I bind 3 params in my shell script, the temporary path of the app, a username and the accessKey of SauceLabs. Then I archive the app and check its md5 hash: after uploading it SauceLabs return hash of your zip file too, that’s an easy way to check integrity.

The only thing missing is to add a build phase to my iOS project. In Xcode, selecting your target, under Build Phases, add a new run script phase and add this code in it.

SCRIPT=`/usr/bin/find "${SRCROOT}" -name upload-ios-app-to-saucelabs.sh | head -n 1`

if [ "${CONFIGURATION}" = "Debug" ]
then
echo "[info] Build for Debug ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app"
/bin/sh "${SCRIPT}" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app" 'MY_SAUCELAB_USERNAME' 'MY_ACCESS_KEY'

Inspired by other script build phases as crash reporters, this code execute your shell script written above and giving in parameter your freshly built app and your SauceLabs credentials. Then, I run this script only when in debug at the first install, I don’t want an overkilled solution that runs UI testing for every build.

Finally, in our script files, some improvements could have been made like creating a temporary folder to work with when zipping the app and removing it at the end. Also, checking the number of parameters given might be a great idea. You can find an improved one in my repo here.

© 2023 Benoit Pasquier. All Rights Reserved
Author's picture

Benoit Pasquier

Software Engineer 🇫🇷, writing about career development, mobile engineering and self-improvement

ShopBack 💰

Singapore 🇸🇬