Class: PageObjectify::Generator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/page-objectify/generator.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(**config) ⇒ Generator

Returns a new instance of Generator.



12
13
14
# File 'lib/page-objectify/generator.rb', line 12

def initialize(**config)
  @config = Config.new(generator_class: self.class.to_s, **config)
end

Instance Method Details

#generate!Object

Calls #visit, generates a page class for the current page, then calls #teardown Raises a NotImplementedError if #visit is not implemented Raises a RuntimeError if @browser is not a Watir::Browser object

Raises:

  • (NotImplementedError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/page-objectify/generator.rb', line 19

def generate!
  raise NotImplementedError, "The #visit method does not exist! Please implement it!" unless respond_to?(:visit)
  visit
  execute_runtime_checks

  @url_at_generation = @browser.url
  logger.info "About to parse HTML! Current URL: #{@url_at_generation}"

  doc = Nokogiri::HTML(@browser.html)
  @code = DOMToRuby.new(DOM.new(doc), @config).unparse

  logger.debug "** BEGIN GENERATED CODE **"
  @code.each_line { |line| logger.debug line.chomp }
  logger.debug "** END GENERATED CODE **"

  write_to_disk

  teardown
end

#teardownObject



39
40
41
# File 'lib/page-objectify/generator.rb', line 39

def teardown
  @browser.quit
end

#wait_for_ajax(timeout = 3, interval = 0.5) ⇒ Object

Wait until the number of pending AJAX requests drops to zero. Assumes jQuery is available on the page; behavior is undefined if it’s not.

Raises Watir::Wait::TimeoutError exception if timeout is exceeded. Raises Selenium::WebDriver::Error::UnknownError: { “errorMessage”:“Can’t find variable: jQuery”, … } if jQuery is not loaded on page.

Raises:

  • (Watir::Wait::TimeoutError)


48
49
50
51
52
53
54
55
56
# File 'lib/page-objectify/generator.rb', line 48

def wait_for_ajax(timeout = 3, interval = 0.5)
  require "watir-webdriver/wait" unless defined?(Watir)
  (timeout / interval).to_i.times do
    return true if @browser.execute_script('return jQuery.active').to_i == 0
    sleep interval
  end

  raise Watir::Wait::TimeoutError, "Timeout of #{timeout} seconds exceeded on waiting for Ajax."
end