Class: BasePage
- Inherits:
-
Object
- Object
- BasePage
- Defined in:
- lib/rutl/base_page.rb
Overview
Base page class. It’s used to call the magical method_messing stuff to parse the page object files into actual objects.
Constant Summary collapse
- @@loaded_pages =
rubocop:enable Style/TrivialAccessors
[]
Instance Attribute Summary collapse
-
#interface ⇒ Object
writeonly
Written by Browser and only used internally.
Class Method Summary collapse
-
.url ⇒ Object
BUGBUG: Kludgy.
Instance Method Summary collapse
-
#add_method(context:, klass:, name:, setter: false) ⇒ Object
Dynamically add a method, :<name> (or :<name>= if setter) to the current class where that method creates an instance of klass.
- #go_to_here ⇒ Object
-
#initialize(interface) ⇒ BasePage
constructor
A new instance of BasePage.
- #loaded?(driver) ⇒ Boolean
-
#method_missing(element, *args, &_block) ⇒ Object
This creates a new element instance whenever it’s called.
-
#respond_to_missing?(*args) ⇒ Boolean
rubocop:enable Metrics/MethodLength.
- #url ⇒ Object
Constructor Details
#initialize(interface) ⇒ BasePage
Returns a new instance of BasePage.
24 25 26 27 28 29 30 31 32 |
# File 'lib/rutl/base_page.rb', line 24 def initialize(interface) @interface = interface # Dirty trick because we're loading all of page classes from files and then # initializing them, calling their layout methods to do magic. # The base_page class knows what pages are loaded. return if @@loaded_pages.include?(self.class) layout @@loaded_pages << self.class end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(element, *args, &_block) ⇒ Object
This creates a new element instance whenever it’s called. Because of that we can’t keep state in any element objects. That seems like a good thing, actually. Called by layout method on pages.
Hard to make shorter. rubocop:disable Metrics/MethodLength
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rutl/base_page.rb', line 78 def method_missing(element, *args, &_block) name, selectors, rest = args context = ElementContext.new(destinations: rest, interface: @interface, selectors: selectors) case element when /button/, /checkbox/, /link/ add_method(name: name, context: context, klass: element) when /text/ add_method(name: name, context: context, klass: element) add_method(name: name, context: context, klass: element, setter: true) else # TODO: replace with a super call. This is useful for debugging for now. raise "#{element} NOT FOUND WITH ARGS #{args}!!!" end end |
Instance Attribute Details
#interface=(value) ⇒ Object (writeonly)
Written by Browser and only used internally.
41 42 43 |
# File 'lib/rutl/base_page.rb', line 41 def interface=(value) @interface = value end |
Class Method Details
.url ⇒ Object
BUGBUG: Kludgy. What do I really want to do here? Make it easy to define a page’s default url and also matchers for page urls for pages with variable urls? rubocop:disable Style/TrivialAccessors
13 14 15 |
# File 'lib/rutl/base_page.rb', line 13 def self.url @url end |
Instance Method Details
#add_method(context:, klass:, name:, setter: false) ⇒ Object
Dynamically add a method, :<name> (or :<name>= if setter) to the current class where that method creates an instance of klass. context is an ElementContext
As it is, this seems silly to break into pieces for Rubocop. rubocop:disable Metrics/MethodLength
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rutl/base_page.rb', line 54 def add_method(context:, klass:, name:, setter: false) name = "#{name}_#{klass.downcase}" constant = Module.const_get(klass.capitalize) self.class.class_exec do if setter define_method("#{name}=") do |value| constant.new(context, value) end else define_method(name) do constant.new(context) end end end end |
#go_to_here ⇒ Object
34 35 36 37 38 |
# File 'lib/rutl/base_page.rb', line 34 def go_to_here # Ovveride this in base page to have something more # complicated than this. @interface.driver.navigate.to(url) end |
#loaded?(driver) ⇒ Boolean
43 44 45 |
# File 'lib/rutl/base_page.rb', line 43 def loaded?(driver) url == driver.current_url end |
#respond_to_missing?(*args) ⇒ Boolean
rubocop:enable Metrics/MethodLength
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rutl/base_page.rb', line 96 def respond_to_missing?(*args) # Is this right at all??? case args[0].to_s when /button/, /checkbox/, /link/, /text/, 'driver', 'url', 'children', 'loaded?' true when 'ok_link' raise 'OK LINK WAY DOWN HERE IN BASE PAGE!!!' else # I think it's good to raise but change the message. raise 'Drew, you hit this most often when checking current page ' \ "rather than current page class:\n\n #{args}" # I think I want to raise instead of returningn false. end end |
#url ⇒ Object
17 18 19 |
# File 'lib/rutl/base_page.rb', line 17 def url self.class.url end |