Skip to content

Commit

Permalink
Update compare_specs.rb
Browse files Browse the repository at this point in the history
$ rspec ./gen/compare_specs.rb

compare ruby and protractor specs
/basic/actions_spec.js     describes: 1,  its: 1
/basic/elements_spec.js    describes: 4,  its: 46
/basic/locators_spec.js    describes: 10, its: 38
/basic/synchronize_spec.js describes: 1,  its: 8
  ruby and protractor specs have identical "describe" and "it"

Finished in 0.00833 seconds (files took 0.36045 seconds to load)
1 example, 0 failures
  • Loading branch information
bootstraponline committed Jun 5, 2015
1 parent a912aaa commit b36d114
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 16 deletions.
89 changes: 80 additions & 9 deletions gen/compare_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

=begin
rspec ./gen/compare_specs.rb
Generates a list of all /spec/upstream/ specs and compares them to the
protractor/spec/ specs.
Uses regex to extract 'it' and 'do' from the JS and Ruby specs.
=end

describe 'compare ruby and protractor specs' do
Expand All @@ -28,33 +33,66 @@ def ruby_protractor_spec_folder
# [[ruby_spec, js_spec]]
spec_pairs = []

relative_paths = []

# validate all Ruby protractor specs have matching JavaScript protractor specs
ruby_protractor_files.each do |spec|
rb_spec_relative = ruby_to_js_rel spec
relative_paths << rb_spec_relative

js_spec = File.join(js_protractor_spec_folder, rb_spec_relative)
raise "Matching protractor spec does not exist\n#{js_spec}" unless File.exist?(js_spec)

spec_pairs << [spec, js_spec]
end

@longest_rel_path = relative_paths.max_by(&:length).length

@spec_pairs = spec_pairs
end

def longest_rel_path
@longest_rel_path
end

def spec_pairs
@spec_pairs
end

def js_keyword keyword
/#{keyword}\W*\(\W*['|"](.*?)['|"]\W*,/
end

def ruby_keyword keyword
/#{keyword}\W*['|"](.*?)['|"]\W*do/
end

def puts_array array
indent = ' ' * 2
puts indent + array.join("\n" + indent)
end

# Ruby specs must have the same describes as the JS files.
it 'ruby specs have idential describes to protractor specs' do
it 'ruby and protractor specs have identical "describe" and "it"' do
spec_pairs

spec_pairs.each do |ruby_spec, js_spec|
puts "Comparing: #{ruby_to_js_rel(ruby_spec)}"
current_rel_path = ruby_to_js_rel(ruby_spec)
print "#{current_rel_path}"
js_spec = File.read js_spec
ruby_spec = File.read ruby_spec

# match describes
js_describes = js_spec.scan(/describe\W*\(\W*['|"](.*?)['|"]\W*,/).flatten
ruby_describes = ruby_spec.scan(/describe\W*['|"](.*?)['|"]\W*do/).flatten
# match 'describe'
#
# describe('ElementFinder', function() {
describe = 'describe'
js_describes = js_spec.scan(js_keyword describe).flatten
# describe 'ElementFinder' do
ruby_describes = ruby_spec.scan(ruby_keyword describe).flatten

indent = ' ' * ((longest_rel_path - current_rel_path.length) + 1)
desc_count = js_describes.length
print "#{indent}describes: #{desc_count},#{' ' * (desc_count.to_s.length - 3).abs}"

difference = js_describes - ruby_describes
none = []
Expand All @@ -64,17 +102,50 @@ def spec_pairs
expect(js_describes.length).to eq(ruby_describes.length)

# expect order to be the same
js_describes.each_with_index do |describe, index|
js_describes.each_with_index do |expected, index|
begin
expect(describe).to eq(ruby_describes[index])
rescue
puts js_describes.join("\n")
actual = ruby_describes[index]
expect(actual).to eq(expected)
rescue Exception
puts_array js_describes
raise
end
end

# and they should be identical
expect(js_describes).to eq(ruby_describes)

# -- it

# match 'it'
#
# it('should return the same result as browser.findElement', function() {
it = 'it'
js_its = js_spec.scan(js_keyword it).flatten
# it 'should return the same result as browser.findElement' do
ruby_its = ruby_spec.scan(ruby_keyword it).flatten

puts "its: #{js_its.length}"

difference = js_its - ruby_its

# redundant checks for more human readable rspec failure messages
expect(difference).to eq(none)
expect(js_its.length).to eq(ruby_its.length)

# expect order to be the same
js_its.each_with_index do |expected, index|
begin
actual = ruby_its[index]
expect(actual).to eq(expected)
rescue Exception
puts_array js_its
raise
end
end

# and they should be identical
expect(js_its).to eq(ruby_its)
end
end
end
19 changes: 12 additions & 7 deletions spec/upstream/basic/elements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@

# catching errors in a callback is node.js specific
#
# it 'should allow handling errors'
# it 'should allow handling chained errors'
# it 'should allow handling errors' do
# it 'should allow handling chained errors' do

it 'isPresent() should not raise error on chained finders' do
visit 'form'
Expand Down Expand Up @@ -126,11 +126,11 @@

# exception propagation is node.js specific
#
# it 'should propagate exceptions'
# it 'should propagate exceptions' do
#
# infinite looping promises is thankfully node specific
#
# it 'should be returned from a helper without infinite loops'
# it 'should be returned from a helper without infinite loops' do

it 'should be usable in WebDriver functions via getWebElement' do
# note that ruby doesn't need the getWebElement call thanks to the watir patching
Expand All @@ -139,6 +139,11 @@
driver.execute_script(
'arguments[0].scrollIntoView', greeting)
end

# success handlers are specific to the JavaScript bindings
#
# it 'should allow null as success handler' do

end # describe 'ElementFinder'

# --
Expand Down Expand Up @@ -204,10 +209,10 @@
end

it 'filter should be compoundable' do
isDog = lambda do |element|
isDog = lambda do |element|
element.text.include? 'dog'
end
isBig = lambda do |element|
isBig = lambda do |element|
element.text.include? 'big'
end

Expand Down Expand Up @@ -473,5 +478,5 @@ def newExpected expectedLabel
# Ruby has no shortcut css notation ($$ and $ are JS only)
#
# describe 'shortcut css notation' do
# it 'should grab by css'
# it 'should grab by css' do
# it 'should chain $$ with $' do
10 changes: 10 additions & 0 deletions spec/upstream/basic/locators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,14 @@
end
end # describe 'by deep css'
end # if browser_name

it 'should determine if an element is present' do
expect(element(by.binding('greet')).present?).to eq(true)
no_wait { expect(element(by.binding('nopenopenope')).present?).to eq(false) }
end

it 'should determine if an ElementFinder is present' do
expect(element(by.binding('greet')).present?).to eq(true)
no_wait { expect(element(by.binding('nopenopenope')).present?).to eq(false) }
end
end # describe 'locators'

0 comments on commit b36d114

Please sign in to comment.