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.
-
#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
-
#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 |
# 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.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 |
#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
63 64 65 |
# File 'lib/browsed/client.rb', line 63 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
67 68 69 |
# File 'lib/browsed/client.rb', line 67 def display_screenshot!(path) Launchy.open path if development? end |
#generate_browser_id ⇒ Object
84 85 86 |
# File 'lib/browsed/client.rb', line 84 def generate_browser_id SecureRandom.hex[0..15] end |
#quit!(retries: 3) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/browsed/client.rb', line 88 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
80 81 82 |
# File 'lib/browsed/client.rb', line 80 def reset_session! self.session.reset_session! end |
#resize!(res = nil) ⇒ Object
Resize the window separately and not based on initialization
72 73 74 75 76 77 78 |
# File 'lib/browsed/client.rb', line 72 def resize!(res = nil) res ||= self.resolution if res && res.size.eql?(2) && !self.driver.eql?(:chrome) && !self.driver.eql?(:poltergeist) # 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/browsed/client.rb', line 46 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 |