Xcode 7 UI Testing Failure: App accessibility isn't loaded - ios

While I'm running UITests on Simulator with Xcode 7, it's failed when invoking XCUIApplication().launch() with App accessibility isn't loaded

this is a known bug in Xcode bot, usually happens on large server code bases. It's already been filed by Apple Developers (myself as well), and they're 'fixing' it.
Hope this helps.

If you run tests inside CI system under your control, you could possibly reset simulators before running the UI tests. That way the bug is workarounded.
You could reset simulators from command line with following command:
xcrun simctl list | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl erase uuid
As pointed in this SO thread you could also use(however I didn't try it):
xcrun simctl erase all

Restarting the device got rid of this issue for me. (I was using a real device not a simulator)

I got this error following a crash in the test app in CI. In my case, I successfully reproduced the issue locally, then it was easy to see that it crashes (a crash alert appears) and to investigate and fix the error.
In my case the alert was also written to stderr, but on the CI it was not redirected to the logs. If I couldn't repro locally, my next step was to find a way to redirect stderr & stdout to logs.

Related

simctl | fbsimctl : Simulator becomes unresponsive if booted after shutdown command

I'm trying to achieve some automation for iOS simulators. I'm facing a strange issue that I'm not able to understand. Precisely following is my scenario:
I create a simulator using command: xcrun simctl create "iPhone 8 Plus"
I rename my newly created simulator: xcrun simctl rename <udid> my-new-iphone-8-plus
I boot my simulator: xcrun simctl boot <udid>
I open the simulator app: open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/. At this point the simulator app is up and running and I can fully interact with the simulator.
I shutdown the simulator: xcrun simctl shutdown <udid>. At this point the simulator gets shutdown but the simulator app is still running, which makes sense because I may be running more than one simulators simultaneously.
I boot the shutdown simulator again: xcrun simctl boot <udid>. At this point the simulator launches quickly. Neither the boot screen appears nor any loading indicator, as it appeared the first time I launched it.
At this moment the simulator is not responsive to my touch. It seems like it's stuck. The command returns successfully and doesn't return any errors.
I have tried the same thing using fbsimctl as well. I faced the same issue. Also tried with multiple simulators.
ONE IMPORTANT THING: When I close the simulator manually by selecting the desired simulator window and then do CMD + W or close it from the top left red close button and then boot it again from command line it surprisingly works fine.
OBJECTIVE: When the simulator is booted after shutdown using command line, the simulator should be responsive like it was before.
I tried other things as well, like killing process using pid. It didn't help.
Any help in this regard will be highly appreciated. Thanks!

How do I solve Xcode's "This app could not be installed at this time." error

Sometimes, Xcode will not install Apps in the simulator, giving nothing more than the message "This app could not be installed at this time.".
The simulator writes a log for the events that occur while installing an App.
To access it, list the available simulators by typing this in the terminal:
instruments -s devices
this will give you a list with entries like
iPad Pro (10.5-inch) (11.4) [75DE3247-4641-4D71-BAC1-3EC3B019FC87] (Simulator)
Copy the uuid between the brackets for the simulator you are using.
Then type
cd ~/Library/Logs/CoreSimulator/<THE-UUID-YOU-JUST-COPIED>/MobileInstallation
You are now in a directory, that contains the installation logs.
Open the newest log and look for errors of your installation (most likely at the end of the log).
First Reset the simulator by following option:
Hardware -> Erase All content and settings...
and then clean your project by following commands:
shift+cmd+K
shift+opt+cmd+K
If you still want to know what exactly causing the issue, you can check at log files from below location:
~/Library/Logs/CoreSimulator/

how to debug with real device using xcode tools in terminal

I've already have Xcode tools installed on my mac and now I can build my iOS game project like this:
xcodebuild -scheme "Game Scheme"
and the next thing I want is to install and run the generated app (using the command above) on a real device (connect to mac).
I'm wondering if there is a command such as (I read the document about xcrun but still can not figure out how to get this done):
xcrun "my game.app" -l
to install the game on my device, run it, and print all the logs in my terminal just like Xcode did in the console.
Any advice will be appreciated, thanks :)
Doing so is a complicated process that requires reverse-engineering the MobileDevice framework interface or the underlying protocols.
Fortunately, various parties have done the work for you.
https://github.com/phonegap/ios-deploy
http://www.libimobiledevice.org/

Can't run UIAutomationTest from command line

I have an iOS application, and I want to put it in continuous integration. The strategy I'm using to do that is trying to run the tests from the command line.
I'm trying to do it using the following script:
TRACETEMPLATE="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"
instruments -t $TRACETEMPLATE build/Release-iphonesimulator/MyApp.app -e UIACSCRIPT "UnitTests/SomeTest.js"
SomeTest.js is a simple javascript file that I can run in Instrumentation inside Xcode and it works. I build my application using:
xcodebuild -alltargets -sdk iphonesimulator5.1
and this generates the MyApp.app. I can run the application from Xcode in the simulator just fine, however, when I try to run by the instruments, I get a box saying that there is an unknown error and in the command line it prints:
2012-05-15 15:32:59.928 instruments[17548:1d03] Recording cancelled : At least one target failed to launch; aborting run
Instruments Trace Error : Failed to start trace.
Can anyone give me any help/suggestions about what might be happening and how can I fix this?
Also, it always open the iPad simulator? Can I make it that it opens any simulator I want?
I was getting this same error and nearly pulled my hair out trying to solve it. The error messages are completely unhelpful.
What ended up being the problem for me was the .app file that I was pointing to. Once I found the "right" .app file that was deeply buried in the 5.1 Simulator folder, I was able to finally run UIAutomation tests from the command line with Xcode 4.3.
The path to the .app that worked for me:
~/Library/Application\ Support/iPhone\ Simulator/5.1/Applications/{string-of-numbers-and-letters}/MyApp.app/
Here is the full command I'm using to run UIAutomation tests from the command line with Xcode 4.3:
instruments -t /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate ~/Library/Application\ Support/iPhone\ Simulator/5.1/Applications/{string-of-numbers-and-letters}/MyApp.app/ -e UIASCRIPT /Path/To/TestFile.js
Hope this helps someone else, as the documentation out there for this is extremely lacking.
Update
Note that as of Xcode 4.5.2, the automation template location has changed from that on my original answer:
/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate
The correct location of the Automation.tracetemplate, as of Xcode 4.5.2, is /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate
The paths for the command line invocation of UIAutomation has changed slightly over the past couple of years. I have written a few bash shell scripts that work together which can launch your UIAutomation test file in a way that works with Xcode 4.4.1.
See this gist: https://gist.github.com/3605692#comments
In a nutshell, the key command ends up being:
instruments -t $INSTRUMENTS_TEMPLATE $APP_PATH -e UIASCRIPT $SCRIPT_PATH -e UIARESULTSPATH $RESULTS_PATH
My scripts show the variable substitutions needed. The other benefit is that iOS simulator apps have often changing GUID as part of their path. My script will find them, so you don't have to hard code the temporary GUID in your builds / scripts / command line invocations.
sorry, my english is very bad.
As the easiest way to run this script from Teamtsity? Each assembly - the numbers and letters are different.
Not the easiest way:
clean the simulator prior to assembly
find the folder with the program, and it contains these numbers and letters, written in the variable.
This variable, such as writing the script.
Maybe there's a way easier to take from these numbers?
The Build Configuration should be debug than Release. That was the reason for the error in my case.
You can change Build Configuration in 'Edit Schemes' and by choosing the Profile tab. Verify that build tab also is in debug mode.
I did the ps command to see which processes are running. I found that instruments was still running. I then did killall instruments and that killed the instruments process. I then did my instruments command and it worked perfect after that.

How can I get the console logs from the iOS Simulator?

I want to see what happens in the iOS Simulator if I'm not testing the app in Xcode.
For example, if I open a link in the Safari simulator, see what happens in the console,
or if I install a web-app, see the links that I'm pressing in console.
How can I do this?
I want to see it in Xcode or Terminal, but it's not a problem if I need to use another bit of software.
iOS Simulator > Menu Bar > Debug > Open System Log
Old ways:
iOS Simulator prints its logs directly to stdout, so you can see the logs mixed up with system logs.
Open the Terminal and type: tail -f /var/log/system.log
Then run the simulator.
EDIT:
This stopped working on Mavericks/Xcode 5. Now you can access the simulator logs in its own folder: ~/Library/Logs/iOS Simulator/<sim-version>/system.log
You can either use the Console.app to see this, or just do a tail (iOS 7.0.3 64 bits for example):
tail -f ~/Library/Logs/iOS\ Simulator/7.0.3-64/system.log
EDIT 2:
They are now located in ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
tail -f ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
You can view the console for the iOS Simulator via desktop Safari. It's similar to the way you use desktop Safari to view the console for physical iOS devices.
Whenever the simulator is running and there's a webpage open, there'll be an option under the Develop menu in desktop safari that lets you see the iOS simulator console:
Develop -> iPhone Simulator -> site name
There's an option in the Simulator to open the console
Debug > Open System Log
or use the
keyboard shortcut: ⌘/
iOS 8 and iOS 9
Under iOS 8 and iOS 9 this location is now:
~/Library/Logs/CoreSimulator/<DEVICE_CODE>
So, the following will work:
tail -f ~/Library/Logs/CoreSimulator/<DEVICE_CODE>/system.log
The DEVICE_CODE value can be found via the following terminal command:
instruments -s devices
You should not rely on instruments -s. The officially supported tool for working with Simulators from the command line is xcrun simctl.
The log directory for a device can be found with xcrun simctl getenv booted SIMULATOR_LOG_ROOT. This will always be correct even if the location changes.
Now that things are moving to os_log it is easier to open Console.app on the host Mac. Booted simulators should show up as a log source on the left, just like physical devices. You can also run log commands in the booted simulator:
# os_log equivalent of tail -f
xcrun simctl spawn booted log stream --level=debug
# filter log output
xcrun simctl spawn booted log stream --predicate 'processImagePath endswith "myapp"'
xcrun simctl spawn booted log stream --predicate 'eventMessage contains "error" and messageType == info'
# a log dump that Console.app can open
xcrun simctl spawn booted log collect
# open location where log collect will write the dump
cd `xcrun simctl getenv booted SIMULATOR_SHARED_RESOURCES_DIRECTORY`
If you want to use Safari Developer tools (including the JS console) with a webpage in the Simulator: Start one of the simulators, open Safari, then go to Safari on your mac and you should see Simulator in the menu.
You can open a URL in the Simulator by dragging it from the Safari address bar and dropping on the Simulator window. You can also use xcrun simctl openurl booted <url>.
[iOS Logger]
You can use the Console application(select your device in Devices) on your Mac to see a log message that were sent using NSLog, os_log, Logger (you will not see logs from print function).
Also please check (Action -> Include <Info/Debug> Messages)
Please note that if you want to see a log from WebView(UIWebView or WKWebView) you should use Safary -> Develop -> device
[Find crash log]
If you are using Swift, remember that println will only print to the debug log (which appears in xCode's debug area). If you want to print to system.log, you have to use NSLog as in the old days.
Then you can view the simulator log via its menu, Debug > Open System Log... (cmd + /)
tailing /var/log/system.log didn't work for me. I found my logs by using Console.app. They were in
~/Library/Logs/iOS Simulator/{version}/system.log
XCode > 6.0 AND iOS > 8.0
The below script works if you have XCode version > 8.0
I use the below small Script to tail the simulator logs onto the system console.
#!/bin/sh
sim_dir=`xcrun instruments -s | grep "iPhone 6 (8.2 Simulator)" | awk {'print $NF'} | tr -d '[]'`
tail -f ~/Library/Logs/CoreSimulator/$sim_dir/system.log
You can pass in the simulator type used in the Grep as an argument. As mentioned in the above posts, there are simctl and instruments command to view the type of simulators available for use depending on the Xcode version.
To View the list of available devices/simulators.
xcrun instruments -s
OR
xcrun simctl list
Now you can pass in the Device code OR Simulator type as an argument to the script and replace the "iPhone 6 (8.2 Simulator)" inside grep to be $1
You can see the Simulator console window, including Safari Web Inspector and all the Web Development Tools by using the Safari Technology Preview app.
Open your page in Safari on the Simulator and then go to Safari Technology Preview > Develop > Simulator.
In Xcode:
View->Debug Area->Activate Console
I can open the log directly via the iOS simulator: Debug -> Open System Log... Not sure when this was introduced, so it might not be available for earlier versions.
No NSLog or print content will write to system.log, which can be open by Select Simulator -> Debug -> Open System log on Xcode 11.
I figure out a way, write logs into a file and open the xx.log with Terminal.app.Then the logs will present in Terminal.app lively.
I use CocoaLumberjack achieve this.
STEP 1:
Add DDFileLogger DDOSLogger and print logs path. config() should be called when App lunch.
static func config() {
#if DEBUG
DDLog.add(DDOSLogger.sharedInstance) // Uses os_log
let fileLogger: DDFileLogger = DDFileLogger() // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
fileLogger.logFileManager.maximumNumberOfLogFiles = 7
DDLog.add(fileLogger)
DDLogInfo("DEBUG LOG PATH: " + (fileLogger.currentLogFileInfo?.filePath ?? ""))
#endif
}
STEP 2:
Replace print or NSLog with DDLogXXX.
STEP 3:
$ tail -f {path of log}
Here, message will present in Terminal.app lively.
One thing more. If there is no any message log out, make sure Environment Variables -> OS_ACTIVITY_MODE ISNOT disable.
Download the safari technology review. With the simulator running, select develop > simulator > localhost
The easiest way to see the console logs is by adding consolelogs as a param:
ionic cordova run ios -l --consolelogs
And you'll able to see the console logs in your terminal.

Resources