Skip to content

Instantly share code, notes, and snippets.

@makolesnik
Last active May 11, 2021 16:03
Show Gist options
  • Save makolesnik/f36e967d2d8b9d5758a9 to your computer and use it in GitHub Desktop.
Save makolesnik/f36e967d2d8b9d5758a9 to your computer and use it in GitHub Desktop.
Testing response with browsermob-proxy, capybara/selenium and chrome
require 'capybara'
require 'capybara/dsl'
require 'browsermob/proxy'
require 'selenium-webdriver'
include Capybara::DSL
# Download browsermob-proxy.
# Set JAVA_HOME for browsermob proxy
# Before
server = BrowserMob::Proxy::Server.new('path/to/browsermob-proxy-2.1.0-beta-3/bin/browsermob-proxy')
server.start
proxy = server.create_proxy
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy.selenium_proxy)
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => caps)
end
Capybara.current_driver = :chrome
# Test
proxy.new_har "Stackoverflow"
visit "http://stackoverflow.com/questions/6588390/where-is-java-home-on-osx-yosemite-10-10-mavericks-10-9-mountain-lion-10"
har = proxy.har
# work with *.har
puts har.entries.first.request.url # => http://stackoverflow.com/questions/6588390/where-is-java-home-on-osx-yosemite-10-10-mavericks-10-9-mountain-lion-10
puts har.entries.first.response.status # => 200
har.save_to "stackoverflow.har"
# After
proxy.close
require 'selenium-webdriver'
require 'browsermob/proxy'
# Download browsermob-proxy.
# Set JAVA_HOME for browsermob proxy:
# # (Mac) Open file vim ~/.bash_profile and add this line: export JAVA_HOME=$(/usr/libexec/java_home)
# # Link: http://stackoverflow.com/questions/6588390/where-is-java-home-on-osx-yosemite-10-10-mavericks-10-9-mountain-lion-10
# Before
server = BrowserMob::Proxy::Server.new("/path/to/browsermob-proxy-2.1.0-beta-3/bin/browsermob-proxy")
server.start
proxy = server.create_proxy
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy.selenium_proxy)
driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)
# Test
proxy.new_har "Stackoverflow"
driver.get "http://stackoverflow.com/questions/6588390/where-is-java-home-on-osx-yosemite-10-10-mavericks-10-9-mountain-lion-10"
har = proxy.har
# work with *.har
puts har.entries.first.request.url # => http://stackoverflow.com/questions/6588390/where-is-java-home-on-osx-yosemite-10-10-mavericks-10-9-mountain-lion-10
puts har.entries.first.response.status # => 200
har.save_to "stackoverflow.har"
# After
proxy.close
driver.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment