Automate Xcode Simulator: how to build and launch an app from terminal - xcodebuild

Is there a way to build an application for the iPhone, and have it running in the simulator, but using just the console?
I am trying to test an app, that will launch; send some data and then quit; if I run it via Xcode, it works fine; the simulator launch and everything is good.
But using xcodebuild; the app build but won't launch the simulator.
Is there any command or flag to do what Xcode IDE does, but using xcodebuild?
I have found an undocumented flag for the simulator, which is called "SimulateApplication", but sadly the usage is not documented; which makes it a bit hard to use.
Tried with ios-sim but my project won't work with it.

One way is to use instruments. In a way, you'll run it under "UI automation", but you don't actually automate the UI at all. The steps are:
Compile the app:
xcodebuild -sdk iphonesimulator7.1
Create a dummy UI automation script that does nothing (myscript.js):
while(true) {
UIATarget.localTarget().delay(100);
}
Then run the app via instruments:
instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate "$(pwd)/$(find . -name '*.app' | grep iphonesimulator)" -e UIASCRIPT myscript.js
(The find | grep monster there just translates to the full path of the compiled version of the app. You are of course free to type it manually as well if you so prefer.)

Related

Instrumentation Test not starting on Android only on a real device. Working on Simulator

I am mainly facing an issue with running detox tests on android real device. The main problem I face is that await device.launchApp() just doesn't launch the app. After adding some log traces I see that android instrumentation command is the one that is not actually starting the app. Detox internally calls the command /Users/<uname>/Library/Android/sdk/platform-tools/adb -s 39bc3158 shell am instrument -w -r -e detoxServer ws://localhost:51414 -e detoxSessionId 5c04ed58-aa77-64bb-aa2b-3a008f21ac2b -e debug false com.realapp.app.test/androidx.test.runner.AndroidJUnitRunner. This command hangs and Process Times out after sometime and doesn't launch the app.
If I create an instrumentation test from android studio also I am seeing the same issue and this problem only occurs on a real device and not on a simulator. Simulator works absolutely fine and the test cases run smooth.
I have looked into different issues, used 4.1.2 android studio, Invalidated cached and restarted upgraded to latest beta android studio and nothing seems to help.
Also the instrumentation tests run fine on the real device if i open the app manually after starting the tests. The tests are just not launching the app automatically.

How is detox code inserted into the app bundle

I use detox to run e2e tests on react native.
This artcile written by one of the people working on detox says
Gray box essentially uses a piece of code that is planted in the app, it can help us see what’s going on inside.
My question is, how does that happen? Looking at the docs the command used for detox build is
xcodebuild -project ios/YourProject.xcodeproj -scheme YourProject -sdk iphonesimulator -derivedDataPath ios/build
No detox-specific mention to be seen there.
Also, the docs state here that the build command is optional and that
You can also choose not to use it and provide a compiled app by yourself.
Well, how do I do that? How do I inject detox / make sure detox is not injected into my app? If I download compiled app from my CI server it does not work with detox (not surprisingly since the detox code is not planted into the app). But if I use the detox build command (which seem to just run whatever the build command in the config specifies), the tests work. There seems to be some "magic" happening which is not understandable for a newcomer.
Thanks.
It's magic!
Well, actually it isn't. The build step is completely optional, and as stated in the documentation, Detox works with any valid app.
In order to load Detox, we tell the dynamic linker to load it using the DYLD_INSERT_LIBRARIES environment variable. So it only loads Detox when you run detox test from the command line.
This is easy on the simulator as it has access to your Mac's file system. Once we implement support for testing on actual hardware devices, we will still use that environment variable, but we will have to inject it inside your IPA file directly.

How to Allow quick development in Appcelerator Studio?

How do I re-enable the below behavior?
I USED to have the below behavior...now I don't. I have to re-compile the whole project for every change which is killing productivity for me.
I cut and pasted the below text from another question as it explains it much better than I can....
For an iphone simulator build, the .js files are run directly by the simulator without going through the compile step needed for a distribution build. While this saves some time by itself, the real advantage is that the simulator will dynamically use whatever changes you make to a .js file when you navigate to a window using an external .js reference (i.e. the url property). So changes to app.js still need to relaunch the project. But for windows opened later, you can navigate to the window to see how it looks or test code, then just hit the back button in the navigation bar, tweak the .js, and navigate back to the desired window and immediately see the new layout or test the code changes.
This makes tweaking UI layout stuff incredibly fast compared to the android emulator, not to mention code/debug cycles for some *.js logic is as quick as backing up a screen, revising the code, and showing the screen again. Then when you get the logic worked out, switch to android and retest.
If you have Appcelerator Studio (not Titanium Studio) you can enable LiveView, which attaches a filesystem service to your project and pipes file changes at runtime, bypassing the build process. (aka hotloader, etc)
A) Turn on in Studio
B) Use the command line:
ti build -p ios —-liveview
Be sure you have the latest updates from Appcelerator to ensure parity with target compilers.
$ sudo npm install -g appcelerator
$ appc use latest
$ appc setup
If you don't have Appcelerator Studio, you could try third party solutions such as TiShadow:
$ sudo npm install -g tishadow
$ ti build -p ios --shadow
$ tishadow server
$ tishadow # run --shadow
The quickest development feature you can use is LiveView. In Appcelerator Studio, before you run the project you have a little eye icon in the toolbar to enable live view. Then Every change you save to your project will automatically refresh the emulator/device on which you are running.
You can also have a look at a project called TiShadow which basically does the same and is not related to the Studio.

How to run mtouch command to launch Xamarin.iOS app on simulator for automated testing?

I am new to Xamarin and I need to be able to launch a Xamarin iOS app on iOS simulator from command line. I have tried running mtouch but obviously am missing on something as I am not able to successfully launch the app on simulator. I don't know if I am missing on setting any environment variable or am running it from a wrong location as I am getting "Cannot find command" error.
mtouch -launchsim Hello.app
mtouch docs are here
On a Mac, the mtouch binary should be here
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mtouch

Debugging iOS app built from commandline

I am building an app for iOS via commandline but I am having problems debugging it. Had I built using XCode, it would have let me "build and debug" on device without problems. But now, I don't know how to launch it on device using gdb and step through it. If I try to "add a custom target/executable" to a blank XCode project and then launch it, XCode complains about not finding the executable. Same thing happens if I install the app on device manually and try to attach to it using gdb within XCode. Any pointers on how to go about debugging this app? Changing the build process to use XCode is not an option.
This site gives a nice tutorial on how to manually set up a remote debugging session on an iOS device. The process boils down to creating a remote debugging server on the device and attaching to it with a local GDB instance.