Skip to content

Instantly share code, notes, and snippets.

@dmcnulla
Created August 20, 2015 02:55
Show Gist options
  • Save dmcnulla/965b970118a6cdd5a46a to your computer and use it in GitHub Desktop.
Save dmcnulla/965b970118a6cdd5a46a to your computer and use it in GitHub Desktop.
class PageObject
attr_reader :object_type, :name, :find_by
def initialize(object_type, name, find_by)
@object_type = object_type
@name = name
@find_by = find_by
end
end
class PageObjectLooker
attr_reader :lines, :name, :objects, :final_list
def initialize(file_name)
@objects = []
@lines = read(file_name)
@strings = {}
@find_bys = []
@elements = []
@final_list = ''
begin
grab_page_objects
create_page_objects
create_ruby_page_object_file
puts "name is #{@name}"
write(@name.underscore)
rescue Exception => e
File.open('error.log', 'a') do |f|
f.puts "Could not save #{file_name}."
f.puts e.message
f.puts e.backtrace
f.puts "\n"
end
end
end
def read(file_name)
File.read(file_name).split("\n")
end
def write(file_name)
file_name = "#{file_name}.tmp"
File.open(file_name, 'w') do |f|
f.write(@final_list)
end
puts "Created file #{file_name}"
end
def grab_page_objects
grab_name
grab_lines('public static final String').collect { |st| clean_string(st) }
grab_lines('@FindBy').collect { |st| clean_findby(st) }
grab_lines('private WebElement').collect { |st| clean_element(st) }
check_counts(@find_bys, @elements)
end
def create_page_objects
@find_bys.count.times do |i|
@objects.push PageObject.new('text_field', @elements[i], @find_bys[i])
end
end
def create_ruby_page_object_file
output = []
output.push "class #{@name}"
output.push ' include PageObject'
output.push ''
@objects.each do |object|
output.push " #{object.object_type}(:#{object.name}, #{object.find_by})"
end
output.push 'end'
@final_list = output.join("\n")
end
def grab_name
@name = @lines.find { |l| is_valid(l) }.strip.split[2]
end
def is_valid(st)
st.match(/ extends .*Page/)
end
def grab_lines(contains_string)
@lines.select { |l| l.include?(contains_string) }
end
def create_element_string(_find_by, _element)
end
def clean_findby(st)
# puts "clean_findby(#{st})"
fail "FindBy String cannot be nil (#{st})" if st.nil?
p1 = st.strip.remove('@FindBy')
p2 = p1.remove('(')
p3 = p2.remove(')')
if p3.include?(' = ')
p4 = p3.split(' = ')
else
p4 = p3.split('=')
end
if p4[1].include?('"')
p4[1] = p4[1].gsub('"', '')
else
p4[1] = @strings[p4[1]]
end
@find_bys << "#{p4[0]}: '#{p4[1]}'"
end
def clean_element(st)
p1 = st.strip.split[2].remove(';')
@elements << p1.downcase
end
def clean_string(st)
p1 = st.strip
p2 = p1.sub('public static final String', '')
p3 = p2.split(' = ')
@strings[p3[0] => p3[1]]
end
def check_counts(find_bys, elements)
comp_message = "FindBy (#{find_bys.count}) v. elements (#{elements})"
fail "ERROR: #{comp_message}" unless find_bys.count == elements.count
end
end
class String
def underscore
gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr('-', '_')
.downcase
end
def remove(token)
gsub(token, '')
end
end
def usage
puts "ruby page_object.rb <file_to_convert>"
puts " e.g. 'ruby page_object.rb LoginPage.java"
end
if __FILE__ == $PROGRAM_NAME
if ARGV.count != 1
usage
exit
end
if ARGV[0].include?('*')
Dir[ARGV[0]].each do |f|
# puts f
PageObjectLooker.new(f)
end
else
PageObjectLooker.new(ARGV[0])
end
end
@dmcnulla
Copy link
Author

Converts java page object files to... something close to a ruby page object file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment