Class: Watir::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/watir-device/device.rb

Overview

Automate Chrome in mobile emulation mode. Devices availabe for emulation are available in the Dev Tool preferences, including “Apple iPhone 6 Plus”, “Samsung Galaxy Note 3”, “Google Nexus 10”, etc.

Author:

  • Johnson Denen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_name, *args) ⇒ Watir::Device

Emulate a device browser with Chrome’s mobile emulation tool.

Parameters:

  • device_name (String)

    device to emulate



37
38
39
40
41
# File 'lib/watir-device/device.rb', line 37

def initialize device_name, *args
  @driver = device_driver(device_name)
  @device_name = device_name
  @browser = Watir::Browser.new driver
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mthd, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Undefined methods are passed to Watir::Browser.

See Also:



77
78
79
# File 'lib/watir-device/device.rb', line 77

def method_missing mthd, *args
  browser.send(mthd, *args)
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



12
13
14
# File 'lib/watir-device/device.rb', line 12

def browser
  @browser
end

#device_nameObject (readonly)

Returns the value of attribute device_name.



12
13
14
# File 'lib/watir-device/device.rb', line 12

def device_name
  @device_name
end

#driverObject (readonly)

Returns the value of attribute driver.



12
13
14
# File 'lib/watir-device/device.rb', line 12

def driver
  @driver
end

Class Method Details

.start(url, device_name, *args) ⇒ Watir::Device

Spin up an instance and navigate to a URL.

Examples:

Open iPhone 6+ to google.com

Watir::Device.start "www.google.com", "Apple iPhone 6 Plus"

Parameters:

  • url (String)

    URL to visit

  • device_name (String)

    device to emulate

Returns:



26
27
28
# File 'lib/watir-device/device.rb', line 26

def start url, device_name, *args
  new(device_name, *args).tap { |dev| dev.goto url }
end

Instance Method Details

#deviceString

Get the emulated device’s name.

Returns:

  • (String)


48
49
50
# File 'lib/watir-device/device.rb', line 48

def device
  device_name
end

#device_driver(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a WebDriver to emulate the device.



64
65
66
67
68
69
70
71
72
# File 'lib/watir-device/device.rb', line 64

def device_driver name
  opts = {
    "mobileEmulation" => {
      "deviceName" => name
    }
  }
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => opts)
  Selenium::WebDriver.for(:chrome, desired_capabilities: capabilities)
end

#inspectString

Closed instances will return string ending in CLOSED.

Returns:

  • (String)

See Also:

  • Object#inspect


58
59
60
# File 'lib/watir-device/device.rb', line 58

def inspect
  '<#%s:0x%x device=%s url=%s>' % [self.class, hash*2, device, browser.url] rescue '<#%s:0x%x device=%s CLOSED>' % [self.class, hash*2, device]
end