Skip to content

Commit

Permalink
Fix and test rspec helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed Jun 8, 2015
1 parent 1d11610 commit bf94300
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
38 changes: 25 additions & 13 deletions lib/angular_webdriver/protractor/protractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def initialize opts={}

# must be local var for use with define element below.
protractor_element = AngularWebdriver::ProtractorElement.new @watir
driver = @driver

# Top level element method to enable protractor syntax.
# redefine element to point to the new protractor element instance.
Expand All @@ -291,28 +292,39 @@ def initialize opts={}
# by/element within rspec tests when used with install_rspec_helpers.
toplevel_main = eval('self', TOPLEVEL_BINDING)
[toplevel_main, AngularWebdriver::RSpecHelpers].each do |obj|
obj.send :define_singleton_method, :element do |*args|
# define singleton on toplevel main, otherwise use regular define method
# if we use define singleton on the rspec helpers, then rspec won't
# be able to handle block parameters.
#
# Also rspec requires config.include AngularWebdriver::RSpecHelpers
# for the no_wait helper to accept the block parameter.
method_type = obj == toplevel_main ? :define_singleton_method : :define_method

obj.send method_type, :element do |*args|
protractor_element.element *args
end

obj.send :define_singleton_method, :by do
obj.send method_type, :by do
AngularWebdriver::By
end
end

toplevel_main.send :define_singleton_method, :no_wait do |&block|
max_wait = driver.max_wait_seconds
max_page_wait = driver.max_page_wait_seconds
obj.send method_type, :no_wait do |&block|
max_wait = driver.max_wait_seconds
max_page_wait = driver.max_page_wait_seconds

driver.set_max_wait 0
driver.set_max_page_wait 0
driver.set_max_wait 0
driver.set_max_page_wait 0

result = block.call
begin
raise ArgumentError, 'Tried to use no_wait without a block' unless block
result = block.call
ensure
driver.set_max_wait max_wait
driver.set_max_page_wait max_page_wait
end

driver.set_max_wait max_wait
driver.set_max_page_wait max_page_wait

result
result
end
end

self
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def expect_no_method_error &block
# Expects block to raise error
#
# Does **not** modify client wait.
def expect_error &block
expect { block.call }.to raise_error
def expect_error error=Exception, &block
expect { block.call }.to raise_error error
end
36 changes: 36 additions & 0 deletions spec/rspec_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative 'spec_helper'

describe 'rspec_helpers' do
def toplevel_main
@toplevel_main ||= eval('self', TOPLEVEL_BINDING)
end

it 'no_wait exists within rspec context' do
toplevel_main.no_wait { true }

no_wait do
true
end

# no_wait raises an arg error when not passed a block
expect_error(ArgumentError) { toplevel_main.no_wait }
expect_error(ArgumentError) { no_wait }
end

it 'by exists within rspec' do
expected = { css: 'ok' }

expect_equal toplevel_main.by.css('ok'), expected
expect_equal by.css('ok'), expected

expect_error(ArgumentError) { toplevel_main.by.css }
expect_error(ArgumentError) { by.css }
end

it 'element exists within rspec' do
expected = AngularWebdriver::ProtractorElement

expect_equal toplevel_main.element.class, expected
expect_equal element.class, expected
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def browser_name
end

RSpec.configure do |config|
# Required for no_wait &block passing to work correctly.
config.include AngularWebdriver::RSpecHelpers

config.before(:all) do
# @browser = Watir::Browser.new browser_name

Expand Down

0 comments on commit bf94300

Please sign in to comment.