Class: Robro::Browser

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

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#click_through_overlay, #remove_elements

Constructor Details

#initialize(application, headless: false) ⇒ Browser



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
72
73
74
# File 'lib/robro/browser.rb', line 14

def initialize(application, headless: false)
  @application = application.to_sym
  @headless = headless

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

  require 'selenium-webdriver'
  Capybara.register_driver :chrome_jack do |app|
    options = ::Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--headless') if headless
    options.add_argument('--start-maximized')
    options.add_argument('--disable-blink-features')
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument('--excludeSwitches=enable-automation')
    options.add_argument('--disable-gpu')

    driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
    bridge = driver.browser.send(:bridge)

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

    javascript = "      Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});\n      Object.defineProperty(document, 'hidden', {value: false, writable: true});\n      document.dispatchEvent(new Event(\"visibilitychange\"));\n\n      Object.defineProperty(window, 'navigator', {\n        value: new Proxy(navigator, {\n        has: (target, key) => (key === 'webdriver' ? false : key in target),\n        get: (target, key) =>\n          key === 'webdriver'\n          ? undefined\n          : typeof target[key] === 'function'\n          ? target[key].bind(target)\n          : target[key]\n        })\n      });\n    JAVASCRIPT\n\n    bridge.http.call(:post, path, cmd: 'Page.addScriptToEvaluateOnNewDocument',\n                     params: {\n      source: javascript,\n    })\n\n    driver\n  end\n\n  Capybara.current_driver = driver\n\n  Capybara::Screenshot.register_driver(Capybara.current_driver) do |driver, path|\n    driver.browser.save_screenshot(path)\n  end\n\n  Robro.logger.debug \"Browser User-Agent: '\#{page.execute_script 'return navigator.userAgent'}'\"\n\n  Robro.logger.debug \"Capybara driver: \#{Capybara.current_driver} (application: \#{application}, headless: \#{headless?})\"\nend\n"

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



9
10
11
# File 'lib/robro/browser.rb', line 9

def application
  @application
end

Instance Method Details

#clearObject



80
81
82
# File 'lib/robro/browser.rb', line 80

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

#close_other_tabsObject



84
85
86
87
88
89
90
91
92
# File 'lib/robro/browser.rb', line 84

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



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

def headless?
  @headless
end

#keep_only_tabs_with(url:) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/robro/browser.rb', line 94

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