rubycdp / cuprite

Headless Chrome/Chromium driver for Capybara
https://cuprite.rubycdp.com
MIT License
1.25k stars 92 forks source link

How to configure cuprite to test on mobile emulation ? #216

Open jean-francois-labbe opened 2 years ago

jean-francois-labbe commented 2 years ago

I'm trying to migrate to cuprite. I can't find any setup for mobile testing. I have the following selenium configuration.

  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--no-sandbox')
  options.add_argument('--headless') unless ENV['VIEW']
  options.add_argument('--disable-dev-shm-usage')
  options.add_argument('--disable-gpu')
  options.add_argument('--window-size=1024,1024')
  options.add_option(
    :mobileEmulation,
    {
      'deviceMetrics' => { 'width' => 360, 'height' => 640, 'pixelRatio' => 3.0 },
      'userAgent' => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) '\
      'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
    },
  )
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.open_timeout = 30 # seconds
  client.read_timeout = 10 # seconds

  Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options,
                                 http_client: client, **selenium_context.url_config)

I'd like to reproduce it with cuprite. I looked in the main repositories using cuprite (using this page). None is testing executing system tests on mobile.

Here is my current attempt. I tried to specify the mobileEmulation option in different places. The browsers is not using the mobile emulation.

Capybara.register_driver(:cuprite) do |app|
  Capybara::Cuprite::Driver.new(
    app,
    window_size: [360, 640],
    screen_size: [360, 640],
    browser_options: { 'no-sandbox': nil,
      'mobileEmulation': {
        'deviceMetrics' => { 'width' => 360, 'height' => 640, 'pixelRatio' => 3.0 },
        'userAgent' => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) '\
        'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
      },
    },
    headless: false,
    mobile_emulation: {
      'deviceMetrics' => { 'width' => 360, 'height' => 640, 'pixelRatio' => 3.0 },
      'userAgent' => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) '\
      'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
    },
  )
end

What am I missing ?

grekko commented 9 months ago

@jean-francois-labbe did you solve this?

jean-francois-labbe commented 9 months ago

@grekko sadly it's not solved. Selenium manages to make it work. I think it may need to configure to browser to work in a certain mode so that it all tabs and service worker uses the same user-agent, the one you configured.

grekko commented 9 months ago

I briefly checked the Ferrum issues page and found https://github.com/rubycdp/ferrum/issues/94 – so we'll have to wait for this being available before the ruby driver can make use of it.