iOS emulator location for UITesting - ios

I am trying to run my UITests on Continuous Integration server (I am using Bitrise). So in one of my UITests I have the following:
let myLocationPin = app.otherElements["My Location"]
self.expectation(for: exists, evaluatedWith: myLocationPin, handler: nil)
self.waitForExpectations(timeout: 20, handler: nil)
expect(myLocationPin.exists).to(beTrue())
expect(myLocationPin.isHittable).to(beTrue())
myLocationPin.tap()
It works well on my local machine but as soon as the tests is being ran on the CI server it fails. I have found out that the reason is for the failure is that the emulator that runs the test doesn't have any selected location. I have tried to add a GPX file, but that didn't work either. Any suggestions ?

Did you select location on your local machine from the dropdown list before run?
If it is the case then you won't be able to use the graphical UI to do it on bitrise, however locations can be selected per scheme.
You can follow this post to set location on your schemes:
Automating Xcode' Debug > Simulate Location command
Update:
If the method above didn't solved the issue, another solution would be:
To change location in the simulator itself, you will need to add a script step before your UITest including the following script:
# download set-simulator-location
brew install lyft/formulae/set-simulator-location
# run simulator
export IOS_SIMULATOR_UDID=`instruments -s devices | grep "iPhone 6 (10.3) \[" | awk -F '[ ]' '{print $4}' | awk -F '[\[]' '{print $2}' | sed 's/.$//'`
echo $IOS_SIMULATOR_UDID
open -a "simulator" --args -CurrentDeviceUDID $IOS_SIMULATOR_UDID
# wait simulator to start fully
sleep 15
# set location
set-simulator-location -q London
Don't forget to change iPhone 6 (10.3) to the simulator you need and London to your own location, with -c flag you can set coordinates as well.
For more info about set-simulator-location visit: set-simulator-location

If you want custom location in your simulator then Select simulator and follor these two steps.
and change location

Related

How to get file path of most recently used simulator & most recently updated app on an iOS simulator in Bash?

It can be useful (or necessary) to know the full path on a mac of a given iOS simulator app. E.g., I have an iOS app with sqlite and want an easy way to build a connection string for debugging with DBeaver. Most of the time the criteria is "where on the mac is the path to the most recently run simulator & most recently built app on it?". How do I get this in bash?
# id of most recent device
device=$(ls -lt ~/Library/Developer/CoreSimulator/Devices/ | head -2 | tail -1 | awk 'NF{print $NF; exit}')
# id of most recent app
app=$(ls -lt ~/Library/Developer/CoreSimulator/Devices/"$device"/data/Containers/Data/Application | head -2 | tail -1 | awk 'NF{print $NF; exit}')
# the full path
fullpath=$(ls ~/Library/Developer/CoreSimulator/Devices/"$device"/data/Containers/Data/Application/"$app"/)
echo $fullpath

Qt-creator examples fail to build for iphonesimulator

I've successfully installed Qt 5.7.0 and Qt-creator 4.1.0 on El Capitan with Xcode 8.
I fixed the xcode sdk-version errors from qt, and now I'm trying to build one of the examples for iphonesimulator. None of them work. All of them fail with error message of type:
The following build commands failed:
CopyPNGFile Debug-iphonesimulator/2dpainting.app/Default-568h#2x.png 2dpainting.xcodeproj/Default-568h#2x.png
I can confirm that directory Debug-iphonesimulator/2dpainting.app does not have the png-file, it's actually located somewhere within the qt installation directories. Copying the png to the source folder does not help as the folder gets overwritten upon running 'make'.
Any advice would be appreciated.
Edit:
The build kit warns about the following issue:
"Device type is not supported by the Qt version". Device type is 'iOS simulator'.
I had the same problem after I had updated my Xcode to version 8.0.
My first error was "Project ERROR: Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild."
To solve this problem, I created a symbolic link:
cd /Applications/Xcode.app/Contents/Developer/usr/bin/
sudo ln -s xcodebuild xcrun
Then I got error "Project ERROR: Current iphonesimulator SDK version (10.0) is too old. Please upgrade Xcode."
I commented two strings out in file QT_DIR/5.7/ios/mkspecs/macx-ios-clang/features/sdk.prf
lessThan(QMAKE_MAC_SDK_VERSION, "8.0"): \
error("Current $$QMAKE_MAC_SDK SDK version ($$QMAKE_MAC_SDK_VERSION) is too old. Please upgrade Xcode.")
Then I got error about emulator. Qt could not find it. I replaced line of code in file QT_DIR/5.7/ios/mkspecs/macx-ios-clang/xcodebuild.mk from:
IPHONESIMULATOR_GENERIC_DESTINATION := "id=$(shell xcrun simctl list devices | grep -E 'iPhone|iPad' | grep -v unavailable | perl -lne 'print $$1 if /((.*?))/' | tail -n 1)"
to:
IPHONESIMULATOR_GENERIC_DESTINATION := "id=$(shell xcrun simctl list devices | grep -E 'iPhone|iPad' | grep -v unavailable | awk 'match ($$0, /\(([A-F0-9\-]*\))/ ) { print substr ($$0, RSTART+1, RLENGTH-2) }' | tail -n 1)"
And finally afer all of it I got error "The following build commands failed:
CopyPNGFile Debug-iphonesimulator/Test01.app/Default-568h#2x.png Test01.xcodeproj/Default-568h#2x.png"
This error occurred due to the fact that the system has two file xcrun. And script /Applications/Xcode.app/Contents/Developer/usr/bin/copypng starts one, which is a symbolic link. Then I changed path in this script
from:
my $PNGCRUSH = `xcrun -f pngcrush`;
to:
my $PNGCRUSH = `/usr/bin/xcrun -f pngcrush`;
And then I finally had built the project without errors and opened it in Xcode.
I had this stupid error and spent two days to nail down. I was about to downgrade Xcode and thought lets try one more time. Finally nailed down.
Symptom:
The simplest project wont build from Qt Creator. The error I would get: CopyPNG failed or something along the line and a hint '-f' unknown parameter.
Reason: xcrun takes both -f and -find from the terminal I can see, but it does not like -f from the script copypng. (copypng is a perl script by the way.)
The solution: Open the file /Applications/Xcode.app/Contents/Developer/usr/bin/copypng and find
my $PNGCRUSH = `xcrun -f pngcrush`;
changed to
my $PNGCRUSH = `xcrun -find pngcrush`;
PS: I dint have path problem with xcrun, if you have it'd be good to put the whole path as advised above.

After "cordova run ios" terminal does not echo keyboard input

Using Cordova 4.1.2. After successfully running the project on an iPad using:
cordova run ios
and exiting LLDB using kill or safequit commands the terminal window will no longer echo keyboard input.
Is there a shell command I can enter to restore normal function of the terminal window? Currently I have to exit the terminal and start a new one after testing my cordova application.
It appears that one of the scripts being run by the cordova run ios is turning off terminal echo. Terminal settings before:
~>stty
speed 9600 baud;
lflags: echoe echoke echoctl pendin
iflags: iutf8
oflags: -oxtabs
cflags: cs8 -parenb
And after:
speed 9600 baud;
lflags: -icanon -iexten -echo echoe echoke echoctl
iflags: inlcr iutf8
oflags: -oxtabs
cflags: cs8 -parenb
dsusp eof lnext reprint status werase
<undef> <undef> <undef> <undef> <undef> <undef>
I have been able to work around this issue by carefully typing the following after LLDB exits to restore keyboard echo:
stty echo

Running iOS UIAutomation as a post-action build script is return as a posix spawn error

I'm entirely new to using bash and Xcode build scripts and so my code is probably a jungle full of errors.
The idea here is to trigger the script below which will scrape the directory that it is saved in for any .js automation scripts. It will then send these scripts to instruments to be run one at a time. I found some nifty code that created time stamped files and so I used that to create a more meaningful storage system.
#!/bin/bash
# This script should run all (currently only one) tests, independently from
# where it is called from (terminal, or Xcode Run Script).
# REQUIREMENTS: This script has to be located in the same folder as all the
# UIAutomation tests. Additionally, a *.tracetemplate file has to be present
# in the same folder. This can be created with Instruments (Save as template...)
# The following variables have to be configured:
#EXECUTABLE="Plans.app"
# Find the test folder (this script has to be located in the same folder).
ROOT="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Prepare all the required args for instruments.
TEMPLATE=`find $ROOT -name '*.tracetemplate'`
#EXECUTABLE=`find ~/Library/Application\ Support/iPhone\ Simulator | grep "${EXECUTABLE}$"`
echo "$BUILT_PRODUCTS_DIR"
echo "$PRODUCT_NAME"
EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/"
SCRIPTS=`find $ROOT -name '*.js'`
# Prepare traces folder
TRACES="${ROOT}/Traces/`date +%Y-%m-%d_%H-%M-%S`"
mkdir -p "$TRACES"
printf "\n" >> "$ROOT/results.log"
echo `date +%Y-%m-%d_%H-%M-%S` >> "$ROOT/results.log"
# Get the name of the user we should use to run Instruments.
# Currently this is done, by getting the owner of the folder containing this script.
USERNAME=`ls -l "${ROOT}/.." | grep \`basename "$ROOT"\` | awk '{print $3}'`
# Bring simulator window to front. Depending on the localization, the name is different.
osascript -e 'try
tell application "iPhone Simulator" to activate
on error
tell application "iOS Simulator" to activate
end try'
# Prepare an Apple Script that promts for the password.
PASS_SCRIPT="tell application \"System Events\"
activate
display dialog \"Password for user $USER:\" default answer \"\" with hidden answer
text returned of the result
end tell"
# Run all the tests.
for SCRIPT in $SCRIPTS; do
echo -e "\nRunning test script $SCRIPT"
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
#echo "$COMMAND"
echo "Executing command $TESTC" >> "$ROOT/results.log"
echo "here $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
echo $OUTPUT >> "$ROOT/results.log"
echo "Finished logging" >> "$ROOT/results.log"
SCRIPTNAME=`basename "$SCRIPT"`
TRACENAME=`echo "$SCRIPTNAME" | sed 's_\.js$_.trace_g'`
for i in $(ls -A1t $PWD | grep -m 1 '.trace')
do
TRACEFILE="$PWD/$i"
done
if [ -e $TRACEFILE ]; then
mv "$TRACEFILE" "${TRACES}/${TRACENAME}"
fi
if [ `grep " Fail: " results.log | wc -l` -gt 0 ]; then
echo "Test ${SCRIPTNAME} failed. See trace for details."
open "${TRACES}/${TRACENAME}"
exit 1
break
fi
done
rm results.log
A good portion of this was taken from another Stack Overflow answer but because of the repository setup that I'm working with I needed to keep the paths abstract and separate from the root folder of the script. Everything seems to work (although probably not incredibly efficiently) except for the actual xcrun command to launch instruments.
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
echo "Executing command $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
This is turned into the following by whatever black magic Bash runs on:
sudo -u Braains xcrun instruments -l -c -t
/Users/Braains/Documents/Automation/AppName/TestCases/UIAutomationTemplate.tracetemplate
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/ -e UIARESULTSPATH
/Users/Braains/Documents/Automation/AppName/TestCases/Traces/2014-07-17_16-31-49/ -e
UIASCRIPT /Users/Braains/Documents/Automation/AppName/TestCases/Test-Case_1js
(^ Has inserted line breaks for clarity of the question ^)
The resulting error that I am seeing is:
posix spawn failure; aborting launch (binary ==
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/AppName).
I have looked all over for a solution to this but I can't find anything because Appium has a similar issue. Unfortunately I don't understand the systems well enough to know how to translate the fixes to Appium to my own code but I imagine it's a similar issue.
I do know that the posix spawn failure is related to threading, but I don't know enough about xcrun to say what's causing the threading issue.
Related info:
- I'm building for the simulator but it'd be great to work on real devices too
- I'm using xCode 5.1.1 and iOS Simulator 7.1
- This script is meant to be run as a build post action script in xCode
- I did get it briefly working once before I broke it and couldn't get it back to the working state. So I think that means all of my permissions are set correctly.
UPDATE: So I've gotten to the root of this problem although I have not found a fix yet. First of all I have no idea what xcrun is for and so I dropped it. Then after playing around I found that my Xcode environment variables are returning the wrong path, probably because of some project setting somewhere. If you copy the Bash command from above but replace Debug-iphoneos with Debug-iphonesimulator the script can be run from the command line and will work as expected.
So for anyone who happens across this the only solution I could find was to hardcode the script for the simulator.
I changed EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/" to be EXECUTABLE="${SYMROOT}/Debug-iphonesimulator/${EXECUTABLE_PATH}". This is obviously not a great solution but it works for now.

Simulate iPad using Cordova/PhoneGap emulator command

I would like to use the included ./emulator command with Cordova/PhoneGap to run my app in the iPad simulator from the command line.
The basic instructions are here:
http://docs.phonegap.com/en/2.2.0/guide_command-line_index.md.html
I've installed the iOS simulator from here:
https://github.com/phonegap/ios-sim
The documentation says it supports simulating an iPad from the command line. However, it opens by default to iPhone and changing the device to "iPad" closes the app (and it is not installed on the home screen). I've searched but can't find documentation to launch to simulate an iPad.
How do I run the Cordova ./emulator command to open to iPad?
It may be that you were using an old version of phonegap/cordova, but in version 3.4 the following works for me:
cordova emulate ios --target="iPad"
The Cordova emulate script is just a wrapper for the ios-sim command which you can use directly from the command line. Assuming your current working directory is the one with the emulate script in it, you can launch your build in the emulator in iPad mode with the following:
ios-sim launch ../build/myApp.app --family ipad --stderr console.log --stdout console.log &
The following is undoubtedly naive (I don't know shell-scripting), but I've hacked the emulate script to support a second command-line parameter which allows me to specify the device family. You might not be aware that the script already accepts a first parameter which allows you to specify the path to your project's .app file as it figures out the path if the parameter is not specified.
Update your version of the script with the following (GitHub fork here):
#! /bin/sh
#
# Licensing info removed for brevity
#
set -e
XCODE_VER=$(xcodebuild -version | head -n 1 | sed -e 's/Xcode //')
XCODE_MIN_VERSION="4.5"
if [[ "$XCODE_VER" < "$XCODE_MIN_VERSION" ]]; then
echo "Cordova can only run in Xcode version $XCODE_MIN_VERSION or greater."
exit 1
fi
CORDOVA_PATH=$( cd "$( dirname "$0" )" && pwd -P)
PROJECT_PATH="$(dirname "$CORDOVA_PATH")"
XCODEPROJ=$( ls "$PROJECT_PATH" | grep .xcodeproj )
PROJECT_NAME=$(basename "$XCODEPROJ" .xcodeproj)
APP_PATH=$1
DEVICE_FAMILY=$2
if [ $# -lt 1 ]; then
APP_PATH="$PROJECT_PATH/build/$PROJECT_NAME.app"
DEVICE_FAMILY=iphone
fi
if [ ! -d "$APP_PATH" ]; then
echo "Project '$APP_PATH' is not built. Building."
$CORDOVA_PATH/build || exit $?
fi
if [ ! -d "$APP_PATH" ]; then
echo "$APP_PATH not found to emulate."
exit 1
fi
# launch using ios-sim
if which ios-sim >/dev/null; then
ios-sim launch "$APP_PATH" --family "$DEVICE_FAMILY" --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" &
else
echo -e '\033[31mError: ios-sim was not found. Please download, build and install version 1.4 or greater from https://github.com/phonegap/ios-sim into your path. Or "brew install ios-sim" using homebrew: http://mxcl.github.com/homebrew/\033[m'; exit 1;
fi
You can then run the script like this (assumes your present working directory is the cordova directory containing the scripts):
./emulate ../build/myApp.app ipad
If you're always going to test on the iPad and you prefer not to have to specify the path to your app every time, you could just hardcode your preferred device family into the script like so and launch the emulator as you have been doing previously:
ios-sim launch "$APP_PATH" --family ipad --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" &
For me all the mentioned options here did not work, I had to call it with this command to show the iPad Retina:
``ios-sim launch [DIR_OF_APP]platforms/ios/build/emulator/My-App.app --devicetypeid "com.apple.CoreSimulator.SimDeviceType.iPad-Retina, 8.2"
To retrieve all the devicetypeid's type ios-sim showdevicetypes

Resources