Module: PageObjectStubs
- Defined in:
- lib/page_object_stubs/version.rb,
lib/page_object_stubs/raketask.rb,
lib/page_object_stubs/ast_processor.rb,
lib/page_object_stubs/page_object_stubs.rb
Defined Under Namespace
Classes: ProcessPageObjects
Constant Summary collapse
- VERSION =
'0.0.2'- DATE =
'2015-05-10'
Class Method Summary collapse
-
.add_stubs_task(opts = {}) ⇒ Object
Adds stubs task to Rake.
-
.generate(opts = {}) ⇒ Object
Creates stubs from target Ruby files in output folder with the format target_filename_stub.rb.
Class Method Details
.add_stubs_task(opts = {}) ⇒ Object
Adds stubs task to Rake
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/page_object_stubs/raketask.rb', line 12 def add_stubs_task opts={} task_name = opts.fetch(:task_name, 'stubs') task_desc = opts.fetch(:task_desc, 'Generate stubs') # Get the dir that contains the Rakefile via Rake (__dir__ will not work) targets = opts.fetch(:targets, lambda { Dir.glob(File.join(Rake.application.original_dir, 'page', '*_page.rb')) }) output_folder = opts.fetch(:output_folder, lambda { File.join(Rake.application.original_dir, 'helper', 'stub') }) angularjs = opts.fetch(:angularjs, true) raise 'targets must be a lambda' unless targets.lambda? raise 'output_folder must be a lambda' unless output_folder.lambda? Rake.application.last_description = task_desc Rake::Task.define_task task_name do |task| PageObjectStubs.generate targets: targets.call, output_folder: output_folder.call, angularjs: angularjs end end |
.generate(opts = {}) ⇒ Object
Creates stubs from target Ruby files in output folder with the format target_filename_stub.rb
“‘ruby targets = Dir.glob(File.join(__dir__, ’..‘, ’page’, ‘*_page.rb’)) output = File.join(__dir__, ‘..’, ‘stub’) PageObjectStubs.generate targets: targets, output: output “‘
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/page_object_stubs/page_object_stubs.rb', line 16 def generate opts={} angularjs = !!opts.fetch(:angularjs, false) targets = opts.fetch(:targets) targets = targets.select do |target| File.file?(target) && File.readable?(target) end output_folder = File.(opts.fetch(:output_folder)) FileUtils.mkdir_p output_folder targets.each do |f| ast = Parser::CurrentRuby.parse File.read f page_objects = ProcessPageObjects.new page_objects.process ast file_name = File.basename(f, '.*') file_module_name = file_name.split('_').map(&:capitalize).join file_method_name = file_name.downcase output_prefix = "module Stub\n module \#{file_module_name}\nclass << self\n" output = '' def wrap method_name ' ' * 6 + "def #{method_name}; fail('stub called!'); end" + "\n" end page_objects.name_type_pairs.each do |pair| element_type = pair.first if element_type == 'page_url' output += wrap 'goto' # visit loads goto then runs the Protractor waitForAngular JavaScript output += wrap 'visit' if angularjs next end element_name = pair.last output += wrap "#{element_name}" output += wrap "#{element_name}_element" output += wrap "#{element_name}?" output += wrap "#{element_name}=" if element_type == 'text_field' end stub_file = File.join(output_folder, file_name + '_stub.rb') output_postfix = "end\n end\nend\n\nmodule Kernel\n def \#{file_method_name}\nStub::\#{file_module_name}\n end\nend unless Kernel.respond_to? :\#{file_method_name}\n" output = output_prefix + output + output_postfix File.open(stub_file, 'w') do |file| file.write output.strip end end # if 'page_url' exists, then we need `def goto` # for all others, we need the name of the element to generate the remaining methods. end |