XCode Test Automation For IPhone - iphone

I would like to have my iphone test app to be tested automatically in an IPhone. The following are the steps I would like to have:
compile, link and code sign the iphone app (Xcodebuild)
upload the newly built app to iphone
run the uploaded app in iphone automatically
collect the result from the gdb console
close the app
Right now, I have problem with step 2 and 3 where I cannot do it automatically (I can do it from XCode via "Build and Debug" button. This, however, will require manual clicking).
I did some research on automator and it does not answer my problem. Another option I am thinking about is to have the app compiled for iphone simulator and run it from there, but I am not sure how accurate the test result will be comparing to the real device.
I am new to Mac/IPhone development, perhaps someone has a better way of testing this. Any feedback and input are welcome. Thanks.

The comment section does not provide a good way of display the solution properly. Here is the summary of answer.
The task of building IPhone app, uploading and trigger the debug process on IPhone is done via AppleScript. Here is how the AppleScript looks like:
tell application "Xcode"
open "Users:chuan:Desktop:iphone_manual_client:iphone_manual_client.xcodeproj"
tell project "iphone_manual_client"
clean
build
(* for some reasons, debug will hang even the debug process has completed.
The try block is created to suppress the AppleEvent timeout error
*)
try
debug
end try
end tell
quit
end tell
AppleScript accepts ":" instead of "/" for file and folder separator.
The GDB console output can be captured by setting the GDB option to write it to file. this is done by typing the following command in Terminal:
defaults write com.apple.Xcode PBXGDBDebuggerLogToFile YES
defaults write com.apple.Xcode PBXGDBDebuggerLogFileName <path to my gdb output file>
Lastly, many thanks to various ppl who have helped to solve this problem.

The tool you probably want to use for the build and install is Applescript. Something like:
tell application "Xcode" to launch
I'm not pretending that this is a complete answer; there are still a lot of things to work out. But Applescript is going to be one of your key tools I believe.

Check out UISpec http://code.google.com/p/uispec/
It's a full automation test framework being developed for the iphone.

Check out iphone_testify, it works well with OCUnit and Google Toolbox For Mac, and I think it could be possible extend it for something like UISpec.
Regards

There's a command line tool "xcodebuild" you can call to kick off an XCode build without it being open. There are flags you can use to set targets and so on.

Dr Nic's testing with Ruby may help with some of this

Could you add a build phase to the target that runs a script to upload the binary to the iphone?
Right click the target, Add->New Build Phase->New Run Script Build Phase

xcodebuild will just build the binary and it will not upload the newly compiled binary to iphone.
actually it can upload (after signing it), but not run too bad...
maybe it can be run using gdb once connected to target but how (ip?, usb? usbnet?) ?

Related

How do I get code coverage for tests and build for iPhone in Xcode 4?

I have a couple of projects with unit tests where I want to measure coverage. So I follow the instructions detailed in the How-to for CoverStory and elsewhere on the web.
I add -fprofile-arcs and -ftest-coverage to my test target and the target under test and link to /Developer/usr/lib/libprofile_rt.dylib as specified.
And it works! When I run the tests scheme under the iPhone simulator, I get my code coverage, my tests and app build and run.
But then, when I go back to the Run scheme on actual iPhone hardware, I get a link error for libprofile_rt.dylib. Specifically, I get
ld: library not found for -lprofile_rt
What I suspect here is that libprofile_rt is actually a library for Mac hardware and the iPhone obviously can't link to a binary built for Mac, in general.
My question is How do run on iPhone hardware and get my code coverage?
I suspect I can do this with a copy of my Debug target that is only ever used for testing, but that has the additional trouble of maintaining yet another target. Is there any other way to get this working? Am I doing something silly?
might a little bit late but actually you can. it is explained in this presentation, slides 32 to 34.
took me a fair amount of time to find this hidden gem. hope this might help future visitor of this page.
Possible answer to this problem is here: https://stackoverflow.com/a/5140459
The trick is to get the *.gcda files out of the App's Documents directory via Xcode organizer, since they will, of course, be generated on device.
The answer is: you can't run on your device AND get coverage at the same time. The only way to get coverage is on the simulator: make a copy of your debug target, setup coverage and what not on that target and run that target, with your tests, in the simulator.
I go through all the details in an answer to this question on stackoverflow: Code coverage on iOS Using Xcode 4.2 on Lion

How to get a brieflist file on my iPhone and run it

I'm testing mockup software Briefs (giveabrief.com) and managed to 'compile' one of the examples into a .brieflist file.
Can anyone explain the next step, how to get the file on my iphone and how to run it.
I have purchased DiskAid to access files on my iPhone but dont know what to do.
Any help would be appreciated, Thanks!
--- update ---
Ok, i now understand i have to get the Briefs application running on my iPhone. I've just started playing with xCode, having a hard time understanding the provisioning system...
I downloaded the code for the Briefs app (https://github.com/capttaco/Briefs). Can anyone hint me how to open a existing project in xCode?
Try to run it on the simulator first, this will avoid the whole code signing issue until you are signed up for Apple's dev program.
I think you should read documentation: Introduction, Building Briefs, Briefcasts & Sharing
Copy onto a Web Server (or Dropbox public folder)
In order to load the .brieflist into Briefs, you must first copy that onto a web-accessible location. You can use any server you wish, but if you have a Dropbox account it’s simple. Just copy the .brieflist into your Public folder, then right click that file and select “Copy Public Link” from the Dropbox menu. Continue reading ...
I think you can run it using Brifs application !
Good luck ...

Quickly testing iOS and objective-c code

I was wondering if is there any way to quickly check iOS/objective-c snippets or new code inside my xcode project without having to compile the whole app for that, open up the simulator just to get the NSLog message traced on the console.
I remember when life was easier using ruby's irb or node command for node.js :)
It just gives me the impression that on iOS development you have to learn how to deal with this high complexity and dependencies all the time, but I am just starting on this new world. so any help would be appreciated.
thanks a lot
This timely blog post should help; I'm using it often already.
Update: there is also Code Runner available in Mac App Store - this supports many languages, including Objective-C.
An Xcode project can contain multiple targets. One of the targets can be a test driver in which you can put some class/object/snippet exercise code and drive it from the command-line, results to the debug console. Another option is to create unit test targets.
If the code you're writing has known expected input-output pairs and you're just writing code to make them work, you may want to look into using XCode's unit testing capabilities. A link to a tutorial on using unit tests is at http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/UnitTesting/0-Introduction/introduction.html . If you want to test more interactive things, it is possible to use Instruments (a program in XCode) to automate those tests as well using the Automation instrument. A guide on using Instruments is available at http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/AboutTracing/AboutTracing.html .
Not really—Objective-C is a compiled language, while I believe Ruby is interpreted (which allows for interactive testing like what you describe). You can reduce some of the overhead of building and running your project by just leaving the simulator open; stopping the app from Xcode will close the app but leave the simulator running, meaning that a rebuilt version of the app will launch more quickly when you run it.

Clean way to programmatically control Safari on iPhone?

The title say's almost all. What I have is short lived server processes that serves a web-based javascript test suite with optionally accompanying back-ends for integration tests (see here for how it's done). I'm looking for a better way to direct the iPhone Emulator's Safari to the URL given.
More, possibly optional background:
Up until iOS SDK 4.0 I've managed to control XCode using AppleScript to start a specially prepared PhoneGap project in this fashion:
AppleScript (referred to as "AS") starts XCode (referred to as "XC")
AS instructs XC to load the previously set up PhoneGap project.
AS instructs XC to launch the PhonePap project.
The PhoneGap project will redirect to the server serving the test suite, and the test suite runs.
In iOS 4.0 SDK, this only works the first round, the next time the test suite runner tries to make XC do the above routine, it fails requesting the previous run to be stopped. And I can't for the life of me find a way to stop the debugging session with AS [1], so I can only run one test suite without manual intervention, witch is pretty much a show stopper for us since we rely a lot on nightly test runs.
Now, what I really want to do is just start the iPhone Emulator, and then start Safari on the emulator with the possibility of sending an arbitrary URL to the emulated Safari.
What I've found so far is iphonesim, but I'm running into this issue and can't get it to properly start pre-compiled apps. A way to circumvent this issue in iphonesim would also be a good way forward.
[1] I'd accept a way to do this as a valid answer too.
I have a very different solution for you.
Write a custom iPhone app that just contains a web view. It will behave pretty much the same as MobileSafari. Then, embed a little web server in the app.
You can then control your app by calling specific urls on that server.
This is a very simple app. Will take you 30 minutes to put together.
This is easily integrated in Xcode or a bigger (Apple) script. You can use commands like curl to open URLs.
This is possible now:
xcrun simctl openurl booted "https://google.com"
https://apple.stackexchange.com/a/198798/144271
Post scriptum: how it was solved in the end.
A while after I asked this question, a user at github added a comment in issue 3 that what you need for iphonesim to work correctly is to give the absolute path to the application to start. So now we have a prebuilt PhoneGap project where a script modifies the contents of the www directory inside build/Debug-iphonesimulator/PhoneGap.app. The absolute path to this directory is then sent to iphonesim, starting up the project now pointing towards the correct server. No AppleScript and no XCode (except for the initial build). The test suite now takes 1/7 of the time it took to run earlier.

iPhone Device Debugging

Is it possible to actually use the Xcode debugger when running an iPhone app on the device rather than the simulator? i.e., can I have the device stop at breakpoints that I set in my code and step through the code as it runs on the device?
EDIT: I should mention that I am a registered developer with Apple and have a valid certificate. In fact, I can build and run iPhone applications on my device just fine. However, even in Debug mode, my application will not stop on breakpoints or output to the console when I run it on my device. If it helps, my application will function normally in debug mode (e.g., stopping at breakpoints) when I run in the simulator.
EDIT 2: I've tried resetting my phone, completely removing and reinstalling Xcode, creating a new project, changing all the settings mentioned in the answers on this page (as well as others), cleaning and building my project, Build and Debug... If I click Pause, it says "Error from Debugger: Quit". :( Maybe I'll try to debug on this device using Xcode on another computer and see what happens.
EDIT 3: Well, I tried using another computer using a fresh install of Xcode in a new project, and I still can't get device debugging to work. I have a suspicion that it's my iPhone or cable perhaps? I'd like to think that I'm not so incompetent that I've missed something so embarrassingly obvious, but I've double- and triple-checked all of the suggestions mentioned here.
EDIT 4: FINALLY got device debugging to work. I have a feeling that something low-level on my device was causing issues...nothing I did worked on my system. However, I installed Xcode on another system and device debugging on the same device (with the same cable) worked flawlessly. Directly after that, device debugging began working on my original machine, leading me to believe that some strange hardware flag was screwed up and somehow "reset" on the second machine. In any case, it works now... Thanks for all the help. :)
Also, I'm unsure if it's better to leave this question unanswered or pick the most "helpful" answer...
Yes -- just use a device build. You have to get certificates and provision it.
This is a good description:
http://boga.wordpress.com/2008/07/16/debugging-ipod-provision-profilescertificates/
Make sure to start debugging by pressing "Cmd-Y"... and not "Cmd-R" :-)
You may try last trick in case nothing can help.
If your application launched successful on your device from XCode but did not stop at breakpoints - try press "pause" button after start in debug mode. If you will see application paused properly (do not react to user's action) add new breakpoint then press "run" to continue application execution. In some cases it may helps to stop at new breakpoint.
Yes, just set your project to build for the Device.
If you need to setup debugging for jailbroken iPhone (up to 2.2.1) (not pay 99 to Apple yet) follow next steps:
Install MobileInstallationPatch for your iPhone via Cydia (use Search Tab)
Switch off PROVISIONING_PROFILE at XCode: Go to /Developer/Platforms/iPhoneOS.platform/Info.plist, add next keys:
<key>PROVISIONING_PROFILE_ALLOWED</key>
<string>NO</string>
<key>PROVISIONING_PROFILE_REQUIRED</key>
<string>NO</string>
You may also add these keys to any
project particularly at XCode, Project
Build Settings, User-Defined
variables.
Add key to your project Info.plist at XCode
SignerIdentity = Apple iPhone OS Application Signing
Don't forget to install and add to your project settings self-signing certificate from Apple
http://developer.apple.com/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html#//apple_ref/doc/uid/TP40005929-CH4-SW2
"Build and Go", you can connect iPhone and start debugging.
Important! You may need to install and launch at least one legal app (any free one) from App Store first (before "Build and Go" step). Otherwise your application will be successfully installed by XCode to your device but may not be launched. Try this troubleshoot first in case of problem with app launching.
Yes. Debugger and performance tools work fine on the device. That's where I do most of my debugging.
Xcode>preferences>debugging uncheck load symbols lazily
fixed the breakpoints not holding in device problem for me.