Class: Browsed::Client
- Inherits:
-
Object
- Object
- Browsed::Client
- Includes:
- Chrome, Firefox, Poltergeist, Capybara::DSL
- Defined in:
- lib/browsed/client.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
Returns the value of attribute browser.
-
#browser_id ⇒ Object
Returns the value of attribute browser_id.
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#device ⇒ Object
Returns the value of attribute device.
-
#driver ⇒ Object
Returns the value of attribute driver.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#headless ⇒ Object
Returns the value of attribute headless.
-
#manager ⇒ Object
Returns the value of attribute manager.
-
#maximum_processes ⇒ Object
Returns the value of attribute maximum_processes.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#resolution ⇒ Object
Returns the value of attribute resolution.
-
#session ⇒ Object
Returns the value of attribute session.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #can_start_new_process? ⇒ Boolean
- #display_screenshot!(path) ⇒ Object
- #generate_browser_id ⇒ Object
-
#initialize(configuration: ::Browsed.configuration, driver: :selenium_chrome, browser: :chrome, device: :desktop, proxy: nil, user_agent: nil, resolution: nil, environment: :production, options: {}, maximum_processes: nil) ⇒ Client
constructor
A new instance of Client.
- #quit!(retries: 3) ⇒ Object
- #reset_session! ⇒ Object
- #resizable_browser?(driver) ⇒ Boolean
-
#resize!(res = nil) ⇒ Object
Resize the window separately and not based on initialization.
- #setup_capybara(options: {}, retries: 3) ⇒ Object
Constructor Details
#initialize(configuration: ::Browsed.configuration, driver: :selenium_chrome, browser: :chrome, device: :desktop, proxy: nil, user_agent: nil, resolution: nil, environment: :production, options: {}, maximum_processes: nil) ⇒ Client
Returns a new instance of Client.
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 |
# File 'lib/browsed/client.rb', line 11 def initialize(configuration: ::Browsed.configuration, driver: :selenium_chrome, browser: :chrome, device: :desktop, proxy: nil, user_agent: nil, resolution: nil, environment: :production, options: {}, maximum_processes: nil) self.configuration = configuration self.driver = driver || self.configuration.driver self.browser = browser || self.configuration.browser self.environment = environment || self.configuration.environment self.headless = false self.browser_id = generate_browser_id self.device = device self.proxy = proxy self.manager = ::Browsed::Manager.new(browser: self.browser) self.maximum_processes = maximum_processes || self.configuration.maximum_processes set_user_agent(user_agent) set_resolution(resolution) .merge!(browser_id: self.browser_id) (options: ) end |
Instance Attribute Details
#browser ⇒ Object
Returns the value of attribute browser.
4 5 6 |
# File 'lib/browsed/client.rb', line 4 def browser @browser end |
#browser_id ⇒ Object
Returns the value of attribute browser_id.
4 5 6 |
# File 'lib/browsed/client.rb', line 4 def browser_id @browser_id end |
#configuration ⇒ Object
Returns the value of attribute configuration.
3 4 5 |
# File 'lib/browsed/client.rb', line 3 def configuration @configuration end |
#device ⇒ Object
Returns the value of attribute device.
6 7 8 |
# File 'lib/browsed/client.rb', line 6 def device @device end |
#driver ⇒ Object
Returns the value of attribute driver.
4 5 6 |
# File 'lib/browsed/client.rb', line 4 def driver @driver end |
#environment ⇒ Object
Returns the value of attribute environment.
4 5 6 |
# File 'lib/browsed/client.rb', line 4 def environment @environment end |
#headless ⇒ Object
Returns the value of attribute headless.
6 7 8 |
# File 'lib/browsed/client.rb', line 6 def headless @headless end |
#manager ⇒ Object
Returns the value of attribute manager.
7 8 9 |
# File 'lib/browsed/client.rb', line 7 def manager @manager end |
#maximum_processes ⇒ Object
Returns the value of attribute maximum_processes.
7 8 9 |
# File 'lib/browsed/client.rb', line 7 def maximum_processes @maximum_processes end |
#proxy ⇒ Object
Returns the value of attribute proxy.
5 6 7 |
# File 'lib/browsed/client.rb', line 5 def proxy @proxy end |
#resolution ⇒ Object
Returns the value of attribute resolution.
6 7 8 |
# File 'lib/browsed/client.rb', line 6 def resolution @resolution end |
#session ⇒ Object
Returns the value of attribute session.
5 6 7 |
# File 'lib/browsed/client.rb', line 5 def session @session end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
6 7 8 |
# File 'lib/browsed/client.rb', line 6 def user_agent @user_agent end |
Instance Method Details
#can_start_new_process? ⇒ Boolean
64 65 66 |
# File 'lib/browsed/client.rb', line 64 def can_start_new_process? self.maximum_processes.nil? || self.manager.can_start_more_processes?(max_count: self.maximum_processes) end |
#display_screenshot!(path) ⇒ Object
68 69 70 |
# File 'lib/browsed/client.rb', line 68 def display_screenshot!(path) Launchy.open path if development? end |
#generate_browser_id ⇒ Object
90 91 92 |
# File 'lib/browsed/client.rb', line 90 def generate_browser_id SecureRandom.hex[0..15] end |
#quit!(retries: 3) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/browsed/client.rb', line 94 def quit!(retries: 3) begin self.session.driver.quit rescue Exception retries -= 1 retry if retries > 0 end # If Selenium/Phantom somehow isn't able to shut down the browser, force a shutdown using kill -9 self.manager.set_command(browser_id: self.browser_id) self.manager.kill_processes! self.session = nil end |
#reset_session! ⇒ Object
86 87 88 |
# File 'lib/browsed/client.rb', line 86 def reset_session! self.session.reset_session! end |
#resizable_browser?(driver) ⇒ Boolean
81 82 83 84 |
# File 'lib/browsed/client.rb', line 81 def resizable_browser?(driver) non_resizable = [:poltergeist, :selenium_chrome, :selenium_chrome_headless] !non_resizable.include?(driver.to_sym) end |
#resize!(res = nil) ⇒ Object
Resize the window separately and not based on initialization
73 74 75 76 77 78 79 |
# File 'lib/browsed/client.rb', line 73 def resize!(res = nil) res ||= self.resolution if res && res.size.eql?(2) && resizable_browser?(self.driver) # Resolutions for Chrome & Poltergeist are set in the driver self.session.current_window.resize_to(res.first, res.last) # [width, height] end end |
#setup_capybara(options: {}, retries: 3) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/browsed/client.rb', line 47 def (options: {}, retries: 3) self.manager.kill_stale_processes! if can_start_new_process? register_driver!() .default_driver = self.driver .javascript_driver = self.driver .default_max_wait_time = .fetch(:wait_time, 30) #seconds self.session = ::Session.new(self.driver) else raise Browsed::TooManyProcessesError, "Too many #{self.browser} processes running, reached maximum allowed number of #{self.maximum_processes} processes." end end |