class SimCtl::Device

Attributes

is_available[R]
name[R]
os[R]
state[R]
udid[R]

Public Class Methods

new(args) click to toggle source
Calls superclass method
# File lib/simctl/device.rb, line 16
def initialize(args)
  args['is_available'] = args.delete('isAvailable')
  super
end

Public Instance Methods

==(other) click to toggle source
# File lib/simctl/device.rb, line 245
def ==(other)
  return false if other.nil?
  return false unless other.is_a? Device
  other.udid == udid
end
availability() click to toggle source
# File lib/simctl/device.rb, line 21
def availability
  is_available
end
available?() click to toggle source

Returns true/false if the device is available

@return [Bool]

# File lib/simctl/device.rb, line 29
def available?
  is_available !~ /unavailable/i
end
boot() click to toggle source

Boots the device

@return [void]

# File lib/simctl/device.rb, line 36
def boot
  SimCtl.boot_device(self)
end
delete() click to toggle source

Deletes the device

@return [void]

# File lib/simctl/device.rb, line 43
def delete
  SimCtl.delete_device(self)
end
devicetype() click to toggle source

Returns the device type

@return [SimCtl::DeviceType]

# File lib/simctl/device.rb, line 50
def devicetype
  @devicetype ||= SimCtl.devicetype(identifier: plist.deviceType)
end
erase() click to toggle source

Erases the device

@return [void]

# File lib/simctl/device.rb, line 57
def erase
  SimCtl.erase_device(self)
end
install(path) click to toggle source

Installs an app on a device

@param path Absolute path to the app that should be installed @return [void]

# File lib/simctl/device.rb, line 65
def install(path)
  SimCtl.install_app(self, path)
end
kill() click to toggle source

Kills the device

@return [void]

# File lib/simctl/device.rb, line 80
def kill
  SimCtl.kill_device(self)
end
launch(scale = 1.0, opts = {}) click to toggle source

Launches the Simulator

@return [void]

# File lib/simctl/device.rb, line 87
def launch(scale = 1.0, opts = {})
  SimCtl.launch_device(self, scale, opts)
end
launch_app(identifier, args = [], opts = {}) click to toggle source

Launches an app in the given device

@param opts [Hash] options hash - `{ wait_for_debugger: true/false }` @param identifier [String] the app identifier @param args [Array] optional launch arguments @return [void]

# File lib/simctl/device.rb, line 104
def launch_app(identifier, args = [], opts = {})
  SimCtl.launch_app(self, identifier, args, opts)
end
launchctl() click to toggle source

Returns the launchctl object

@ return [SimCtl::DeviceLaunchctl]

# File lib/simctl/device.rb, line 94
def launchctl
  @launchctl ||= DeviceLaunchctl.new(self)
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/simctl/device.rb, line 251
def method_missing(method_name, *args, &block)
  if method_name[-1] == '!'
    new_method_name = method_name.to_s.chop.to_sym
    if respond_to?(new_method_name)
      warn "[#{Kernel.caller.first}] `#{method_name}` is deprecated. Please use `#{new_method_name}` instead."
      return send(new_method_name, &block)
    end
  end
  super
end
open_url(url) click to toggle source

Opens the url on the device

@param url [String] The url to be opened on the device @return [void]

# File lib/simctl/device.rb, line 121
def open_url(url)
  SimCtl.open_url(self, url)
end
path() click to toggle source
# File lib/simctl/device.rb, line 125
def path
  @path ||= DevicePath.new(self)
end
privacy(action, service, bundle) click to toggle source

Change privacy settings

@param action [String] grant, revoke, reset @param service [String] all, calendar, contacts-limited, contacts, location,

location-always, photos-add, photos, media-library, microphone,
motion, reminders, siri

@param bundle [String] bundle identifier @return [void]

# File lib/simctl/device.rb, line 137
def privacy(action, service, bundle)
  SimCtl.privacy(self, action, service, bundle)
end
ready?() click to toggle source

Returns true/false if the device is ready Uses [SimCtl::DeviceLaunchctl] to look for certain services being running.

Unfortunately the 'booted' state does not mean the Simulator is ready for installing or launching applications.

@return [Bool]

# File lib/simctl/device.rb, line 148
def ready?
  running_services = launchctl.list.reject { |service| service.pid.to_i == 0 }.map(&:name)
  (required_services_for_ready - running_services).empty?
end
reload() click to toggle source

Reloads the device information

@return [void]

# File lib/simctl/device.rb, line 156
def reload
  device = SimCtl.device(udid: udid)
  device.instance_variables.each do |ivar|
    instance_variable_set(ivar, device.instance_variable_get(ivar))
  end
end
rename(name) click to toggle source

Renames the device

@return [void]

# File lib/simctl/device.rb, line 166
def rename(name)
  SimCtl.rename_device(self, name)
  @name = name
end
reset() click to toggle source

Resets the device

@return [void]

# File lib/simctl/device.rb, line 174
def reset
  SimCtl.reset_device name, devicetype, runtime
end
runtime() click to toggle source

Returns the runtime object

@return [SimCtl::Runtime]

# File lib/simctl/device.rb, line 181
def runtime
  @runtime ||= SimCtl.runtime(identifier: plist.runtime)
end
screenshot(file, opts = {}) click to toggle source

Saves a screenshot to a file

@param file Path where the screenshot should be saved to @param opts Optional hash that supports two keys:

  • type: Can be png, tiff, bmp, gif, jpeg (default is png)

  • display: Can be main or tv for iOS, tv for tvOS and main for watchOS

@return [void]

# File lib/simctl/device.rb, line 192
def screenshot(file, opts = {})
  SimCtl.screenshot(self, file, opts)
end
settings() click to toggle source

Returns the settings object

@ return [SimCtl::DeviceSettings]

# File lib/simctl/device.rb, line 199
def settings
  @settings ||= DeviceSettings.new(path)
end
shutdown() click to toggle source

Shuts down the runtime

@return [void]

# File lib/simctl/device.rb, line 206
def shutdown
  SimCtl.shutdown_device(self)
end
spawn(path, args = [], opts = {}) click to toggle source

Spawn a process on a device

@param path [String] path to executable @param args [Array] arguments for the executable @return [void]

# File lib/simctl/device.rb, line 215
def spawn(path, args = [], opts = {})
  SimCtl.spawn(self, path, args, opts)
end
status_bar() click to toggle source

Returns the status bar object

@return [SimCtl::StatusBar]

# File lib/simctl/device.rb, line 229
def status_bar
  @status_bar ||= SimCtl::StatusBar.new(self)
end
terminate_app(identifier, args = []) click to toggle source

Terminates an app on the given device

@param identifier [String] the app identifier @param args [Array] optional terminate arguments @return [void]

# File lib/simctl/device.rb, line 113
def terminate_app(identifier, args = [])
  SimCtl.terminate_app(self, identifier, args)
end
uninstall(app_id) click to toggle source

Uninstall an app from a device

@param app_id App identifier of the app that should be uninstalled @return [void]

# File lib/simctl/device.rb, line 73
def uninstall(app_id)
  SimCtl.uninstall_app(self, app_id)
end
wait(timeout = SimCtl.default_timeout) { |device(udid: udid)| ... } click to toggle source

Reloads the device until the given block returns true

@return [void]

# File lib/simctl/device.rb, line 236
def wait(timeout = SimCtl.default_timeout)
  Timeout.timeout(timeout) do
    loop do
      break if yield SimCtl.device(udid: udid)
    end
  end
  reload
end

Private Instance Methods

plist() click to toggle source
# File lib/simctl/device.rb, line 264
def plist
  @plist ||= OpenStruct.new(CFPropertyList.native_types(CFPropertyList::List.new(file: path.device_plist).value))
end
required_services_for_ready() click to toggle source
# File lib/simctl/device.rb, line 268
def required_services_for_ready
  case runtime.type
  when :tvos, :watchos
    if Xcode::Version.gte? '8.0'
      [
        'com.apple.mobileassetd',
        'com.apple.nsurlsessiond'
      ]
    else
      [
        'com.apple.mobileassetd',
        'com.apple.networkd'
      ]
    end
  when :ios
    if Xcode::Version.gte? '9.0'
      [
        'com.apple.backboardd',
        'com.apple.mobile.installd',
        'com.apple.CoreSimulator.bridge',
        'com.apple.SpringBoard'
      ]
    elsif Xcode::Version.gte? '8.0'
      [
        'com.apple.SimulatorBridge',
        'com.apple.SpringBoard',
        'com.apple.backboardd',
        'com.apple.mobile.installd'
      ]
    else
      [
        'com.apple.SimulatorBridge',
        'com.apple.SpringBoard',
        'com.apple.mobile.installd'
      ]
    end
  else
    []
  end
end