iosxcodeshxctestxcode-cloud

Execute shell script before UI Test for iOS app starts


I have UI test (for an iOS app) that opens iOS photo gallery and taps on specific image. I want to have custom script that will copy this image to simulator's photo gallery at the beginning of the test of even right before it will start.

I have sh script that works when I manually run it after simulator is launched, but I want it to be executed during test run on Xcode Cloud.

This is script code

#!/bin/bash

# Get the UDID of the simulator (adjust for your target simulator)
SIMULATOR_UDID=$(xcrun simctl list devices | grep "Booted" | awk -F '[()]' '{print $2}')

# Path to the image
IMAGE_PATH="./myImage.jpg"

# Add the image to the simulator gallery
xcrun simctl addmedia $SIMULATOR_UDID $IMAGE_PATH

I already tried to add it to pre-test actions for Test Scheme and to Build Phases of UI test target - no luck, I even wasn't able to find logs related to this script execution.


Solution

  • Try this script code:

    #!/bin/bash
    
    # Ensure the script logs output for debugging
    set -x
    
    # Path to the image (adjust if necessary)
    IMAGE_PATH="$SRCROOT/myImage.jpg"
    
    # Get the UDID from environment variable
    SIMULATOR_UDID="$TARGET_DEVICE_IDENTIFIER"
    
    # Wait until the simulator is booted
    boot_status=$(xcrun simctl bootstatus "$SIMULATOR_UDID" -b)
    if [ $? -ne 0 ]; then
      echo "Simulator failed to boot"
      exit 1
    fi
    
    # Add the image to the simulator gallery
    xcrun simctl addmedia "$SIMULATOR_UDID" "$IMAGE_PATH"
    

    Steps to Implement

    Script

    Logging

    Image Path

    Simulator Boot

    When running on Xcode Cloud, make sure that your image file is included in the build artifacts or accessible at the specified path.