Class: Robro::Browser

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/robro/browser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Browser

Returns a new instance of Browser.



11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/robro/browser.rb', line 11

def initialize(application)
  @application = application.to_sym
  @headless = false # FIXME ENV['GUI'].nil?

  Capybara.configure do |config|
    config.run_server = false
    config.default_max_wait_time = 5
  end

  # FIXME Register chrome_jack_headless
  require 'selenium-webdriver'
  Capybara.register_driver :chrome_jack do |app|
    options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
      opts.args << '--start-maximized'
      opts.args << '--disable-blink-features'
      opts.args << '--disable-blink-features=AutomationControlled'
      opts.args << '--excludeSwitches=enable-automation'
      opts.args << '--disable-gpu'
    end
    driver = Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
    bridge = driver.browser.send(:bridge)

    path = '/session/:session_id/chromium/send_command'
    path[':session_id'] = bridge.session_id

    javascript = <<~JAVASCRIPT
      Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});
      Object.defineProperty(document, 'hidden', {value: false, writable: true});
      document.dispatchEvent(new Event("visibilitychange"));

      Object.defineProperty(window, 'navigator', {
        value: new Proxy(navigator, {
        has: (target, key) => (key === 'webdriver' ? false : key in target),
        get: (target, key) =>
          key === 'webdriver'
          ? undefined
          : typeof target[key] === 'function'
          ? target[key].bind(target)
          : target[key]
        })
      });
    JAVASCRIPT

    bridge.http.call(:post, path, cmd: 'Page.addScriptToEvaluateOnNewDocument',
                     params: {
      source: javascript,
    })

    driver
  end

  Capybara.current_driver = driver

  Capybara::Screenshot.register_driver(Capybara.current_driver) do |driver, path|
    driver.browser.save_screenshot(path)
  end

  Robro.logger.debug "Browser User-Agent: '#{page.execute_script 'return navigator.userAgent'}'"

  Robro.logger.debug "Capybara driver: #{Capybara.current_driver} (application: #{application}, headless: #{headless?})"
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



7
8
9
# File 'lib/robro/browser.rb', line 7

def application
  @application
end

Instance Method Details

#clearObject



77
78
79
# File 'lib/robro/browser.rb', line 77

def clear
  visit 'file:///dev/null'
end

#close_other_tabsObject



81
82
83
84
85
86
87
88
89
# File 'lib/robro/browser.rb', line 81

def close_other_tabs
  current_tab = page.driver.browser.window_handle
  tabs = page.driver.browser.window_handles - [current_tab]
  tabs.each do |tab|
    page.driver.browser.switch_to.window(tab)
    page.driver.browser.close
  end
  page.driver.browser.switch_to.window current_tab
end

#headless?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/robro/browser.rb', line 73

def headless?
  @headless
end

#keep_only_tabs_with(url:) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/robro/browser.rb', line 91

def keep_only_tabs_with(url:)
  tabs = page.driver.browser.window_handles

  tabs.each do |tab|
    page.driver.browser.switch_to.window(tab)
    page.driver.browser.close unless page.current_url.start_with?(url_start_with)
  end
end