Class: BlackStack::PampaBrowser

Inherits:
Watir::Browser
  • Object
show all
Defined in:
lib/browserfactory.rb

Overview

Custom Selenium browser to track all browser activity, and stream the browser window.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ PampaBrowser

Returns a new instance of PampaBrowser.


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/browserfactory.rb', line 88

def initialize(driver)
  # TODO: enable this when you move to the "frequency-based-streaming" model
  #fname = PAMPA_BROWSER_CHANNEL_LOCK_FILENAME.gsub('%PROFILE_NAME%', self.profile_name.to_s)
  #self.lockfile = File.open(fname,"w")
  
  #
  super(driver)

  # TODO: enable this when you move to the "frequency-based-streaming" model
=begin
  # Can't operate the same browser from more than 1 thread.
  # More information here:
  # https://stackoverflow.com/questions/28093347/is-selenium-webdriver-thread-safe  
  #
  # https://bibwild.wordpress.com/2015/01/15/ruby-threads-gotcha-with-local-vars-and-shared-state/
  # https://stackoverflow.com/questions/55895952/ruby-how-do-i-share-a-global-variable-amongst-threads-that-are-running-an-objec
  #
  t0 = Thread.new {
    minimum_enlapsed_seconds = 5.to_f
    while true 
      start_time = Time.now
      t1 = Thread.new(self) do |t_browser|
        #while true
          begin
            self.lock_channel
            sleep(1) # para esperar a que se ejecute la sentencia t1.join debajo
            t_browser.push_screenshot
            self.release_channel
          rescue => e
            # Communication with the chrome driver errors may occur.
            # If an exception happens here and it is not catched, then
            # then the thread will stop workng and the streaming will
            # stop.
          end
        #end # while true
      end # Thread.new
      t1.join
      end_time = Time.now
      elapsed_seconds = (end_time - start_time).to_f
      if (elapsed_seconds < minimum_enlapsed_seconds)
        sleep_seconds = minimum_enlapsed_seconds - elapsed_seconds 
        sleep(sleep_seconds.to_i + 1)
      end # if
    end # while true            
  } # Thread.new
=end
end

Instance Attribute Details

#agent_nameObject

TODO: enable this when you move to the “frequency-based-streaming” model PAMPA_BROWSER_CHANNEL_LOCK_FILENAME = './browserfactory.channel.%PROFILE_NAME%.lock' # manejo de concurrencia en la creación de browsers attr_accessor :lockfile


78
79
80
# File 'lib/browserfactory.rb', line 78

def agent_name
  @agent_name
end

#lnuserObject

TODO: enable this when you move to the “frequency-based-streaming” model PAMPA_BROWSER_CHANNEL_LOCK_FILENAME = './browserfactory.channel.%PROFILE_NAME%.lock' # manejo de concurrencia en la creación de browsers attr_accessor :lockfile


78
79
80
# File 'lib/browserfactory.rb', line 78

def lnuser
  @lnuser
end

#profile_nameObject

TODO: enable this when you move to the “frequency-based-streaming” model PAMPA_BROWSER_CHANNEL_LOCK_FILENAME = './browserfactory.channel.%PROFILE_NAME%.lock' # manejo de concurrencia en la creación de browsers attr_accessor :lockfile


78
79
80
# File 'lib/browserfactory.rb', line 78

def profile_name
  @profile_name
end

#proxyObject

TODO: enable this when you move to the “frequency-based-streaming” model PAMPA_BROWSER_CHANNEL_LOCK_FILENAME = './browserfactory.channel.%PROFILE_NAME%.lock' # manejo de concurrencia en la creación de browsers attr_accessor :lockfile


78
79
80
# File 'lib/browserfactory.rb', line 78

def proxy
  @proxy
end

Instance Method Details

#backObject


193
194
195
196
197
# File 'lib/browserfactory.rb', line 193

def back
  self.notify("back")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#execute_script(script, *args) ⇒ Object


205
206
207
208
209
# File 'lib/browserfactory.rb', line 205

def execute_script(script, *args)
  self.notify("execute_script")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#forwardObject


199
200
201
202
203
# File 'lib/browserfactory.rb', line 199

def forward
  self.notify("forward")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#goto(url) ⇒ Object


211
212
213
214
215
# File 'lib/browserfactory.rb', line 211

def goto(url)
#      self.notify("goto")
#      BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#inspectObject


235
236
237
238
239
# File 'lib/browserfactory.rb', line 235

def inspect
  self.notify("inspect")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#lock_channelObject


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

def lock_channel()
  self.lockfile.flock(File::LOCK_EX)
end

#notify(method) ⇒ Object

def initialize


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/browserfactory.rb', line 136

def notify(method)
  url = "#{BlackStack::Pampa::api_protocol}://#{PROCESS.ws_url}:#{PROCESS.ws_port}/api1.3/pampa/browser/notify.json"
  res = BlackStack::Netting::call_post(url, {
    :api_key => BlackStack::Pampa::api_key,
    :filename => $0,
    :method => method,
    :worker_name => PROCESS.fullWorkerName,
    :id_proxy => self.proxy.nil? ? nil : self.proxy.id,
    :id_lnuser => self.lnuser.nil? ? nil : self.lnuser.id,
    :worker_assigned_process => PROCESS.worker.assigned_process,
    :profile_name => self.profile_name,
    :agent_name => self.agent_name,
  })        
  parsed = JSON.parse(res.body)
  if parsed['status'] != "success"
    raise "Error Tracing BrowserActivity: #{parsed['status']}"
  end
end

#push_screenshot(filename = nil) ⇒ Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/browserfactory.rb', line 155

def push_screenshot(filename=nil)
=begin
  filename = "#{PROCESS.fullWorkerName}.png" if filename.nil?
  BrowserFactory::screenshot(filename)

  image = MiniMagick::Image.open(filename)
  w = (image[:width].to_f * 0.25).to_i
  h = (image[:height].to_f * 0.25).to_i
  image.resize "#{w.to_s}x#{h.to_s}"
  image.format "png"
  image.write filename

  url = "#{BlackStack::Pampa::api_protocol}://#{PROCESS.ws_url}:#{PROCESS.ws_port}/api1.3/pampa/browser/screenshot.json"
  res = RestClient::Request.execute(
    :api_key => BlackStack::Pampa::api_key,
    :id_client => PROCESS.id_client,
    :verify_ssl => false,
    :url => url, 
    :method => :post, 
    :headers => {
      :accept => 'application/json',
      :params => {
        :api_key => BlackStack::Pampa.api_key,
        :filename => filename,          
      },
    },
    :payload => {
      :file => File.new(filename, "r"),
      :multipart => false,     
    }
  )        
  parsed = JSON.parse(res.body)
  if parsed['status'] != "success"
    raise "Error Uploading Screenshot: #{parsed['status']}"
  end
=end
end

#refreshObject


229
230
231
232
233
# File 'lib/browserfactory.rb', line 229

def refresh
  self.notify("refresh")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#release_channelObject


84
85
86
# File 'lib/browserfactory.rb', line 84

def release_channel()
  self.lockfile.flock(File::LOCK_UN)
end

#reset!Object


223
224
225
226
227
# File 'lib/browserfactory.rb', line 223

def reset!
  self.notify("reset!")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#screenshotObject


217
218
219
220
221
# File 'lib/browserfactory.rb', line 217

def screenshot
  self.notify("screenshot")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#send_keys(*args) ⇒ Object


241
242
243
244
245
246
# File 'lib/browserfactory.rb', line 241

def send_keys(*args)
  # no se notifica el evento, porque incremente el # de llamadas a la API de 800/hora a 5000/hora, y eso requiere mas infraestructura
  #self.notify("send_keys")
  #BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#textObject


248
249
250
251
252
# File 'lib/browserfactory.rb', line 248

def text
  self.notify("text")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#titleObject


254
255
256
257
258
# File 'lib/browserfactory.rb', line 254

def title
  self.notify("title")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#urlObject


260
261
262
263
264
# File 'lib/browserfactory.rb', line 260

def url
  self.notify("url")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end

#wait(timeout = 5) ⇒ Object


266
267
268
269
270
# File 'lib/browserfactory.rb', line 266

def wait(timeout = 5)
  self.notify("wait")
  BlackStack::BrowserFactory.browser.push_screenshot
  super
end