Skip to content

Commit

Permalink
Launch simulator for specific device
Browse files Browse the repository at this point in the history
- Added --simulator flag to 'exec' and 'simulator open'
- Modifies the app's Info.plist to force device
  • Loading branch information
enriquez committed Sep 14, 2013
1 parent 1aeb07f commit da4de8f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Running `uiauto help exec` prints the following message.
# Default: ./uiauto/results/trace
[--app=APP] # Location of your application bundle. Defaults to your project's most recent build located in the standard location.
[--device=DEVICE] # Run scripts on a connected device. Specify a UDID to target a specific device.
[--simulator=SIMULATOR] # Run the simulator for a specific device.
# Possible values: iPad, iPad (Retina), iPhone, iPhone (Retina 3.5-inch), iPhone (Retina 4-inch)
[--format=FORMAT] # Formatter to use for output. Combine with --require to include a custom formatter. Built-in Formatters:
# ColorIndentFormatter: Adds readability to instruments output by filtering noise and adding color and indents.
# InstrumentsFormatter: Unmodified instruments output.
Expand Down
68 changes: 68 additions & 0 deletions helpers/choose_sim_device
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env osascript

(*
Copyright (c) 2012 Jonathan Penn (http://cocoamanifest.net/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Chooses a device type from the iPhone Simulator using menu
selection events.
To use, make sure this file is executable, then run from the terminal:
bin/choose_sim_device "iPad (Retina)"
Originally, I tried to do this by editing the Preference file for the
simulator, and it worked under Xcode 4.3, but now it ignores those changes
often enough that I chose to use this menu-selection route.
*)

on run argv
set simType to item 1 of argv

activate application "iPhone Simulator"
tell application "System Events"
tell process "iOS Simulator"
tell menu bar 1
-- Hardware menu bar item
tell menu bar item 5
-- Hardware menu
tell menu 1
-- Device menu item
tell menu item 1
-- Device sub menu
tell menu 1
click menu item simType
end tell
end tell
end tell
end tell
end tell
end tell
end tell

-- Need to show the simulator again after changing device,
-- or else the simulator be hidden when launched by instruments
-- for some odd reason.
tell application "System Events"
set visible of process "iOS Simulator" to true
end tell

end run
8 changes: 7 additions & 1 deletion lib/uiauto/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def save(data_path)
end

desc "open", "Opens the simulator"
method_option :simulator,
:enum => Simulator::DEVICES,
:desc => %q{Run the simulator for a specific device.}
def open
Simulator.open
Simulator.open(options[:simulator])
end

desc "close", "Closes the simulator"
Expand All @@ -50,6 +53,9 @@ class CLI < Thor
:desc => %q{Location of your application bundle. Defaults to your project's most recent build located in the standard derived data location.}
method_option :device,
:desc => %q{Run scripts on a connected device. Specify a UDID to target a specific device.}
method_option :simulator,
:enum => Simulator::DEVICES,
:desc => %q{Run the simulator for a specific device.}
method_option :format,
:default => "ColorIndentFormatter",
:desc => %q{Formatter to use for output. Combine with --require to include a custom formatter. Built-in Formatters:
Expand Down
38 changes: 31 additions & 7 deletions lib/uiauto/instruments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
require 'fileutils'
require 'cfpropertylist'
require 'pty'
require 'uiauto/simulator'

module UIAuto
class Instruments
attr_accessor :trace, :app, :results, :script, :device
attr_accessor :trace, :app, :results, :script, :device, :simulator

def initialize(script, reporter, opts = {})
@reporter = reporter
@script = script
@trace = opts[:trace]
@results = opts[:results]
@device = opts[:device]
@app = opts[:app] || default_application
@reporter = reporter
@script = script
@trace = opts[:trace]
@results = opts[:results]
@device = opts[:device]
@simulator = opts[:simulator]
@app = opts[:app] || default_application

FileUtils.mkdir_p(@results) unless File.exists?(@results)
end
Expand All @@ -31,6 +33,9 @@ def command
end

def execute
launch_simulator
select_device_family

instruments = ChildProcess.build(*command.split(" "))
master, slave = if PTY.respond_to?(:open)
PTY.open
Expand Down Expand Up @@ -103,5 +108,24 @@ def device_id
end
end

def launch_simulator
Simulator.open(@simulator)
end

def select_device_family
if @simulator && !@simulator.empty?
info_plist = CFPropertyList::List.new(:file => File.join(@app, "Info.plist"))
data = CFPropertyList.native_types(info_plist.value)
if @simulator.start_with?("iPhone")
data["UIDeviceFamily"] = [1]
elsif @simulator.start_with?("iPad")
data["UIDeviceFamily"] = [2]
end

info_plist.value = CFPropertyList.guess(data)
info_plist.save
end
end

end
end
17 changes: 16 additions & 1 deletion lib/uiauto/simulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
module UIAuto
class Simulator
CURRENT_IOS_SDK_VERSION = "6.1"
DEVICES = [
"iPad",
"iPad (Retina)",
"iPhone",
"iPhone (Retina 3.5-inch)",
"iPhone (Retina 4-inch)"
]

def initialize(sdk_version = CURRENT_IOS_SDK_VERSION)
@sdk_version = sdk_version
Expand Down Expand Up @@ -35,11 +42,19 @@ def self.close
`killall "iPhone Simulator" &> /dev/null || true`
end

def self.open
def self.open(simulator = nil)
xcode_path = `xcode-select -p`.strip
simulator_path = File.join(xcode_path, "/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app")

`open "#{simulator_path}"`

if DEVICES.include?(simulator)
uiauto_root = Gem::Specification.find_by_name("uiauto").gem_dir
choose_sim_device = File.join(uiauto_root, "helpers/choose_sim_device")
`#{choose_sim_device} "#{simulator}"`
elsif !simulator.nil?
puts "Invalid simulator: \"#{simulator}\""
end
end

private
Expand Down

0 comments on commit da4de8f

Please sign in to comment.