Skip to content

Commit

Permalink
Expose the capybara session in the ruby API
Browse files Browse the repository at this point in the history
This allows pages that require authentication to be benchmarked
  • Loading branch information
mariovisic committed Jun 21, 2013
1 parent bdab766 commit 5753447
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ Server response times will be reported if the application is a ruby/rack applica
You can programatically run the benchmarks. Simply specify the URL and
optionally the amount of runs.

Here is an example of running a benchmark in chrome looping around 3 times.

```ruby

require 'wbench'

results = WBench::Benchmark.run('https://www.desktoppr.co/', :loops => 3, :browser => :chrome) # => WBench::Results
benchmark = WBench::Benchmark.new('https://www.desktoppr.co/', :browser => :chrome)
results = benchmark.run(3) # => WBench::Results

results.app_server # =>
[25, 24, 24]
Expand All @@ -89,6 +92,31 @@ results.latency # =>
"ssl.google-analytics.com"=>[497, 14, 14], "www.desktoppr.co"=>[191, 210, 203]}
```

### Benchmarking authenticated pages

Benchmarking authenticated pages is possible using the ruby API. The API
provides direct access to the selenium session. The session allows us to visit
a login page before our test page, for example:

```ruby
require 'wbench'

benchmark = WBench::Benchmark.new('https://www.desktoppr.co/dashboard', :browser => :chrome)
benchmark.before_each do
visit 'https://www.desktoppr.co/login'
fill_in 'Login', :with => 'mario'
fill_in 'Password', :with => 'super secret'
click_button 'Log In'
end

results = benchmark.run(3) # => WBench::Results
```

Please note that by visiting pages before each run, your browser may cache some
assets. This means that when the benchmark is run against the authenticated
page, some assets may be loaded from the cache, and the result may appear
quicker than an uncahed visit.

### Gisting results

You can install the [Github gist gem](https://github.com/defunkt/gist) and pipe in the results of wbench
Expand Down
5 changes: 5 additions & 0 deletions lib/wbench/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ def initialize(url, options = {})
@browser = Browser.new(url, options)
end

def before_each(&blk)
@before_each = blk
end

def run(loops)
Results.new(@url, loops).tap do |results|
loops.times do
@browser.run(&@before_each)
@browser.visit { results.add(app_server_results, browser_results, latency_results) }
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/wbench/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class Browser
attr_accessor :url

def initialize(url, options = {})

Capybara.register_driver(CAPYBARA_DRIVER) do |app|
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = CAPYBARA_TIMEOUT
Expand Down Expand Up @@ -32,6 +31,10 @@ def evaluate_script(script)
session.evaluate_script(script)
end

def run(&blk)
session.instance_eval(&blk) if block_given?
end

private

def add_selenium_args(options, arg)
Expand Down

0 comments on commit 5753447

Please sign in to comment.