Class: PageFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/test-factory/page_factory.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser, visit = false) ⇒ PageFactory

Returns a new instance of PageFactory.



3
4
5
6
7
8
# File 'lib/test-factory/page_factory.rb', line 3

def initialize browser, visit = false
  @browser = browser
  goto if visit
  expected_element if respond_to? :expected_element
  has_expected_title? if respond_to? :has_expected_title?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



10
11
12
# File 'lib/test-factory/page_factory.rb', line 10

def method_missing sym, *args, &block
  @browser.send sym, *args, &block
end

Class Method Details

.action(method_name, &block) ⇒ Object Also known as: pgmd



44
45
46
47
48
# File 'lib/test-factory/page_factory.rb', line 44

def action method_name, &block
  define_method method_name.to_s do |*thing|
    Proc.new(&block).call *thing, self
  end
end

.element(element_name) ⇒ Object Also known as: value, thing



35
36
37
38
39
40
# File 'lib/test-factory/page_factory.rb', line 35

def element element_name
  raise "#{element_name} is being defined twice in #{self}!" if self.instance_methods.include?(element_name.to_sym)
  define_method element_name.to_s do
    yield self
  end
end

.expected_element(element_name, timeout = 30) ⇒ Object



22
23
24
25
26
# File 'lib/test-factory/page_factory.rb', line 22

def expected_element element_name, timeout=30
  define_method 'expected_element' do
    self.send(element_name).wait_until_present timeout
  end
end

.expected_title(expected_title) ⇒ Object



28
29
30
31
32
33
# File 'lib/test-factory/page_factory.rb', line 28

def expected_title expected_title
  define_method 'has_expected_title?' do
    has_expected_title = expected_title.kind_of?(Regexp) ? expected_title =~ @browser.title : expected_title == @browser.title
    raise "Expected title '#{expected_title}' instead of '#{@browser.title}'" unless has_expected_title
  end
end

.page_url(url) ⇒ Object



16
17
18
19
20
# File 'lib/test-factory/page_factory.rb', line 16

def page_url url
  define_method 'goto' do
    @browser.goto url
  end
end