Class: Puppeteer::EmulationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/emulation_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ EmulationManager

Returns a new instance of EmulationManager.

Parameters:



5
6
7
8
9
# File 'lib/puppeteer/emulation_manager.rb', line 5

def initialize(client)
  @client = client
  @emulating_mobile = false
  @has_touch = false
end

Instance Method Details

#emulate_viewport(viewport) ⇒ true|false

Parameters:

Returns:

  • (true|false)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppeteer/emulation_manager.rb', line 13

def emulate_viewport(viewport)
  mobile = viewport.mobile?
  width = viewport.width
  height = viewport.height
  device_scale_factor = viewport.device_scale_factor
  # /** @type {Protocol.Emulation.ScreenOrientation} */
  # const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
  has_touch = viewport.has_touch?

  await_all(
    @client.async_send_message('Emulation.setDeviceMetricsOverride',
      mobile: mobile,
      width: width,
      height: height,
      deviceScaleFactor: device_scale_factor,
      # screenOrientation: screen_orientation,
    ),
    @client.async_send_message('Emulation.setTouchEmulationEnabled',
      enabled: has_touch,
    ),
  )

  reload_needed = @emulating_mobile != mobile || @hasTouch != has_touch
  @emulating_mobile = mobile
  @has_touch = has_touch
  reload_needed
end