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 =
'2.0.1'
DATE =
'2016-06-08'

Class Method Summary collapse

Class Method Details

.add_stubs_task(opts = {}) ⇒ Object

Adds stubs task to Rake

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :task_name (String)

    the name of the stubs task (optional)

  • :task_desc (String)

    the description of the stubs task (optional)

  • :targets (lambda)

    lambda that will return an array of targets (optional)

  • :output (lambda)

    lambda that will return the output folder (optional)



11
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 11

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    = opts.fetch(:output, lambda { File.join(Rake.application.original_dir, 'helper', 'stub') })
  exclude   = opts.fetch(:exclude, false)

  raise 'targets must be a lambda' unless targets.lambda?
  raise 'output must be a lambda' unless output.lambda?
  raise 'exclude must be a regex' if exclude && exclude.class != Regexp

  Rake.application.last_description = task_desc
  Rake::Task.define_task task_name do
    PageObjectStubs.generate targets: targets.call,
                             output:  output.call,
                             exclude: exclude
  end
end

.generate(opts = {}) ⇒ Object

Creates stubs from target Ruby files in output folder with the format target_filename_stub.rb

Note the output folder is DELETED each time stubs are generated.

“‘ruby targets = Dir.glob(File.join(__dir__, ’..‘, ’page’, ‘*_page.rb’)) output = File.join(__dir__, ‘..’, ‘stub’) exclude = /#Regexp.escape(‘base_page.rb’)/ PageObjectStubs.generate targets: targets, output: output, exclude: exclude “‘

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :targets (Array<File>)

    Array of target files to create stubs from (required)

  • :output (Dir)

    Folder to create stubs in (required)

  • :exclude (Regexp)

    Exclusion regex use to reject targets (optional)



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/page_object_stubs/page_object_stubs.rb', line 19

def generate opts={}
  targets = opts.fetch(:targets)
  targets = targets.select do |target|
    File.file?(target) && File.readable?(target)
  end

  regex = opts.fetch(:exclude, false)
  targets.reject! { |t| t.match regex } if regex

  output_folder = File.expand_path(opts.fetch(:output))
  FileUtils.rm_rf 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    = <<R
module Stub
  module #{file_module_name}
class << self
R
    data = ''

    def wrap method_name
      ' ' * 6 + "def #{method_name}; fail('stub called!'); end" + "\n"
    end

    page_objects.name_type_pairs.each do |pair|
      # process custom methods that aren't defined in page_object
      if pair.length == 1
        data += wrap "#{pair.first}(*args)"
        next
      end

      element_type = pair.first
      # if 'page_url' exists, then we need `def goto`
      # for all others, we need the name of the element to generate the remaining methods.
      if element_type == 'page_url'
        data += wrap 'goto'
        next
      end

      element_name = pair.last

      data += wrap "#{element_name}"
      data += wrap "#{element_name}_element"
      data += wrap "#{element_name}?"

      data += wrap "#{element_name}=" if element_type == 'text_field'
    end


    stub_file      = File.join(output_folder, file_name + '_stub.rb')

    # Note that the page method is defined as a singleton method on the
    # top level 'main' object. This ensures we're not polluting the global
    # object space by defining methods on every object which would happen
    # if Kernel was used instead.

    output_postfix = <<R
end
  end
end

module RSpec
  module Core
class ExampleGroup
  def #{file_method_name}
    Stub::#{file_module_name}
  end
end
  end
end
R

    data = output_prefix + data + output_postfix

    File.open(stub_file, 'w') do |file|
      file.write data
    end
  end
end