Skip to content

Commit

Permalink
Fix protractor syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed May 23, 2015
1 parent bc3e5cf commit d65091f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
31 changes: 17 additions & 14 deletions spec/protractor_compatability.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# see protractor_compatability_spec for example usage
# designed for use with watir-webdriver

class ProtractorElement
attr_accessor :watir
Expand All @@ -7,7 +8,8 @@ def initialize watir
@watir = watir
end

def all args
# @return Watir::HTMLElementCollection
def all *args
watir.elements *args
end
end
Expand All @@ -16,8 +18,9 @@ def protractor_element
@pelement ||= ProtractorElement.new @browser
end

def element args=nil
return protractor_element unless args
# @return Watir::HTMLElement
def element *args
return protractor_element unless args.length > 0
browser.element *args
end

Expand All @@ -29,51 +32,51 @@ class << self
#

def class what
[:class, what]
{ class: what }
end

def class_name what
[:class_name, what]
{ class_name: what }
end

def css what
[:css, what]
{ css: what }
end

def id what
[:id, what]
{ id: what }
end

def link what
[:link, what]
{ link: what }
end

def link_text what
[:link_text, what]
{ link_text: what }
end

def name what
[:name, what]
{ name: what }
end

def partial_link_text what
[:partial_link_text, what]
{ partial_link_text: what }
end

def tag_name what
[:tag_name, what]
{ tag_name: what }
end

def xpath what
[:xpath, what]
{ xpath: what }
end

#
# Protractor locators
#

def binding what
[:binding, what]
{ binding: what }
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/protractor_compatability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
expect_no_element_error { element(by.binding('does not exist')).visible? }
expect_no_element_error { element(by.css('does not exist')).visible? }
end

it 'chains successfully' do
# watir syntax
browser.element(:binding, 'ok').element(:binding, 'ok')
browser.element(:binding, 'ok').elements(:binding, 'ok')

# mixed syntax
browser.element(by.binding('ok')).element(by.binding('ok'))
browser.element(by.binding('ok')).elements(by.binding('ok'))

# protractor syntax
element(by.binding('ok')).element(by.binding('ok'))
element(by.binding('ok')).all(by.binding('ok'))
end
end

describe 'element.all' do
Expand All @@ -34,5 +48,19 @@
expect_equal element.all(by.binding('does not exist')).to_a, []
expect_equal element.all(by.css('does not exist')).to_a, []
end

it 'errors when chained' do
# watir syntax
expect_error { browser.elements(:binding, 'ok').elements(:binding, 'ok') }
expect_error { browser.elements(:binding, 'ok').element(:binding, 'ok') }

# mixed syntax
expect_error { browser.elements(by.binding('ok')).elements(by.binding('ok')) }
expect_error { browser.elements(by.binding('ok')).element(by.binding('ok')) }

# protractor syntax
expect_error { element.all(by.binding('ok')).all(by.binding('ok')) }
expect_error { element.all(by.binding('ok')).element(by.binding('ok')) }
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ def expect_no_element_error &block
expect { block.call }.to raise_error no_such_element_error
driver.manage.timeouts.implicit_wait = implicit_wait_default
end

def expect_error &block
expect { block.call }.to raise_error
end
9 changes: 9 additions & 0 deletions spec/watir_always_locate_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
#

module Watir
module Container
#
# Alias of elements for Protractor
#

def all(*args)
elements(*args)
end
end # module Container

#
# Base class for HTML elements.
Expand Down

0 comments on commit d65091f

Please sign in to comment.