class UlePage::Page

Attributes

urls[R]

Public Class Methods

inherited(subclass) click to toggle source

e.g. is_order_detail?

# File lib/ule_page/page.rb, line 30
def self.inherited(subclass)
  method_name = "is_#{subclass.module_parent_name.demodulize.singularize.underscore}_#{subclass.name.demodulize.singularize.underscore}?"
  subclass.send(:define_method, method_name) do
    true
  end
end
set_urls(*urls) click to toggle source
# File lib/ule_page/page.rb, line 19
def self.set_urls(*urls)
  @urls = urls
  add_to_page_map @urls
  set_first_url
end

Private Class Methods

add_to_page_map(urls = []) click to toggle source
# File lib/ule_page/page.rb, line 165
def self.add_to_page_map(urls = [])
  urls.each { |x| PageMap.instance.pages[x] = new } unless urls.nil?
end
set_first_url() click to toggle source
# File lib/ule_page/page.rb, line 175
def self.set_first_url
  return unless @urls.any?

  first_url = @urls.first
  first_url = first_url.gsub(/:(\w+)/, '{\1}')
  set_url first_url
end

Public Instance Methods

check_element_have_content(selector, content) click to toggle source

usage: check_element_have_content '.nav li a', 'my brand'

# File lib/ule_page/page.rb, line 143
def check_element_have_content(selector, content)
  should have_selector(selector, text: content)
end
check_element_have_no_content(selector, content) click to toggle source
# File lib/ule_page/page.rb, line 147
def check_element_have_no_content(selector, content)
  should have_no_selector(selector, text: content)
end
check_form(hashtable, fields = [], map = {}) click to toggle source

usage: check_form hashtabe, [:id, :name], {:id => “id2”} check_form hashtable, [], {:id => “id2”} check_form hashtable if the fields is empty, it will use all the keys of the hashtable

precondition: there are the elements mapped to the hashtable keys.

# File lib/ule_page/page.rb, line 80
def check_form(hashtable, fields = [], map = {})
  hashtable = wrapper_hash(hashtable)
  map = wrapper_hash(map)

  fields = hashtable.keys.map(&:to_sym) if fields.empty?

  fields.each do |f|
    key = f
    key = map[f] if map.key?(f)

    next unless respond_to? f.to_sym

    el_content = send(f).send(:value)

    expect(el_content).to eq hashtable[key.to_s]
  end
end
check_have_content(content, container = nil) click to toggle source
# File lib/ule_page/page.rb, line 98
def check_have_content(content, container = nil)
  container ||= page
  if container.respond_to?(:has_text?)
    expect(container.has_text?(content)).to be_truthy
  else
    expect(container).to have_content(content)
  end
end
check_have_hashtable_content(hashtable, keys = [], container = nil) click to toggle source

usage: check_have_hashtable_content hashtable usage: check_have_hashtable_content hashtable, [:id, :name]

# File lib/ule_page/page.rb, line 118
def check_have_hashtable_content(hashtable, keys = [], container = nil)
  hashtable = wrapper_hash(hashtable)
  keys = hashtable.keys if keys.empty?

  keys.each do |k|
    begin
      check_have_content hashtable[k.to_s], container
    rescue RSpec::Expectations::ExpectationNotMetError => e
      puts "The key: '#{k}'' has no matched content!!"
      raise
    end
  end
end
check_have_no_hashtable_content(hashtable, keys = [], container = nil) click to toggle source
# File lib/ule_page/page.rb, line 132
def check_have_no_hashtable_content(hashtable, keys = [], container = nil)
  hashtable = wrapper_hash(hashtable)

  keys = hashtable.keys if keys.empty?

  keys.each do |k|
    check_have_not_content hashtable[k.to_s], container
  end
end
check_have_not_content(content, container = nil) click to toggle source
# File lib/ule_page/page.rb, line 107
def check_have_not_content(content, container = nil)
  container ||= page
  if container.respond_to?(:has_no_text?)
    expect(container.has_no_text?(content)).to be_truthy
  else
    expect(container).not_to have_content(content)
  end
end
fill_form(hashtable, fields = [], map = {}) click to toggle source

usage: fill_form hashtable, [:id, :name], {:id => “id2”} fill_form hashtable, [], {:id => “id2”} fill_form hashtable if the fields is empty, it will use all the keys of the hashtable

# File lib/ule_page/page.rb, line 47
def fill_form(hashtable, fields = [], map = {})
  hashtable = wrapper_hash(hashtable)
  map = wrapper_hash(map)

  fields = hashtable.keys.map(&:to_sym) if fields.empty?

  fields.each do |f|
    key = f
    key = map[f] if map.key?(f)

    el = send(f)
    val = hashtable[key]
    tag = el.send(:tag_name)
    it = el.send(:[], :type)

    if tag == 'input' && it == 'checkbox' # checkbox
      el.send(:set, val == 'true')
    elsif tag == 'select' # select
      el.send(:select, val)
    else
      el.send(:set, val)
    end
  end
end
find_row(content, rows = nil) click to toggle source
# File lib/ule_page/page.rb, line 151
def find_row(content, rows = nil)
  rows = page.all('tr') if rows.nil?

  rows.each do |r|
    return r if r.has_content?(content)
  end
end
go_back() click to toggle source
# File lib/ule_page/page.rb, line 159
def go_back
  click_link_or_button '返回'
end
open(expansion = {}) click to toggle source
# File lib/ule_page/page.rb, line 37
def open(expansion = {})
  load expansion
  self
end

Private Instance Methods

wrapper_hash(hash) click to toggle source
# File lib/ule_page/page.rb, line 169
def wrapper_hash(hash)
  return hash unless hash.is_a?(Hash)

  ActiveSupport::HashWithIndifferentAccess.new hash
end