Class: Browsed::Client

Inherits:
Object
  • Object
show all
Includes:
Chrome, Firefox, Poltergeist, Capybara::DSL
Defined in:
lib/browsed/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  
  options.merge!(browser_id: self.browser_id)
  setup_capybara(options: options)
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



4
5
6
# File 'lib/browsed/client.rb', line 4

def browser
  @browser
end

#browser_idObject

Returns the value of attribute browser_id.



4
5
6
# File 'lib/browsed/client.rb', line 4

def browser_id
  @browser_id
end

#configurationObject

Returns the value of attribute configuration.



3
4
5
# File 'lib/browsed/client.rb', line 3

def configuration
  @configuration
end

#deviceObject

Returns the value of attribute device.



6
7
8
# File 'lib/browsed/client.rb', line 6

def device
  @device
end

#driverObject

Returns the value of attribute driver.



4
5
6
# File 'lib/browsed/client.rb', line 4

def driver
  @driver
end

#environmentObject

Returns the value of attribute environment.



4
5
6
# File 'lib/browsed/client.rb', line 4

def environment
  @environment
end

#headlessObject

Returns the value of attribute headless.



6
7
8
# File 'lib/browsed/client.rb', line 6

def headless
  @headless
end

#managerObject

Returns the value of attribute manager.



7
8
9
# File 'lib/browsed/client.rb', line 7

def manager
  @manager
end

#maximum_processesObject

Returns the value of attribute maximum_processes.



7
8
9
# File 'lib/browsed/client.rb', line 7

def maximum_processes
  @maximum_processes
end

#proxyObject

Returns the value of attribute proxy.



5
6
7
# File 'lib/browsed/client.rb', line 5

def proxy
  @proxy
end

#resolutionObject

Returns the value of attribute resolution.



6
7
8
# File 'lib/browsed/client.rb', line 6

def resolution
  @resolution
end

#sessionObject

Returns the value of attribute session.



5
6
7
# File 'lib/browsed/client.rb', line 5

def session
  @session
end

#user_agentObject

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

Returns:

  • (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_idObject



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

Returns:

  • (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 setup_capybara(options: {}, retries: 3)
  self.manager.kill_stale_processes!
  
  if can_start_new_process?
    register_driver!(options)
  
    Capybara.default_driver         =   self.driver
    Capybara.javascript_driver      =   self.driver
  
    Capybara.default_max_wait_time  =   options.fetch(:wait_time, 30) #seconds
  
    self.session                    =   Capybara::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