Skip to content

Commit

Permalink
Merge pull request #38 from jfitisoff/undefined-page-spec
Browse files Browse the repository at this point in the history
Undefined page spec
  • Loading branch information
jfitisoff committed Jul 14, 2018
2 parents 7b15f0d + 2279f12 commit cf55aee
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 23 deletions.
22 changes: 16 additions & 6 deletions lib/insite/insite.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'timeout'
require_relative 'methods/common_methods'

# Usage:
# require 'insite'
Expand All @@ -7,17 +8,20 @@
# include Insite
# end
module Insite
attr_reader :base_url, :unique_methods, :browser_type
attr_reader :base_url, :unique_methods, :browser_type, :site
attr_accessor :pages, :browser, :arguments, :most_recent_page

include CommonMethods

def self.class_to_tag(klass)
if klass.respond_to?(:collection) && klass.collection?
Watir.tag_to_class.key(klass) ||
Watir.tag_to_class.key(CLASS_MAP.key(klass)) ||
Watir.tag_to_class.key(CLASS_MAP.key(klass.collection_member_class))
(
Watir.tag_to_class.key(klass) ||
Watir.tag_to_class.key(CLASS_MAP.key(klass)) ||
Watir.tag_to_class.key(CLASS_MAP.key(klass.collection_member_class))
)
else
Watir.tag_to_class.key(klass) ||
Watir.tag_to_class.key(CLASS_MAP.key(klass))
Watir.tag_to_class.key(klass) || Watir.tag_to_class.key(CLASS_MAP.key(klass))
end
end

Expand Down Expand Up @@ -113,6 +117,7 @@ def driver?
# => 1
# TODO: Sort args.
def initialize(base_url = nil, hsh={})
@site = self
@arguments = hsh.with_indifferent_access
@base_url = base_url
@browser_type = (@arguments[:browser] ? @arguments[:browser].to_sym : nil)
Expand Down Expand Up @@ -193,6 +198,10 @@ def find_non_standard_tags
end.uniq.sort
end

def html
@browser.html
end

def html_tags
%i(html title head body) + Insite::METHOD_MAP.values.flatten.each do |mth|
elem = @browser.send(mth)
Expand Down Expand Up @@ -317,6 +326,7 @@ def page
# 1.)
# Before anything else, look to see if it's the most recent page:
return @most_recent_page if @most_recent_page && @most_recent_page.on_page?
process_browser
url = @browser.url

found_page = nil
Expand Down
44 changes: 40 additions & 4 deletions lib/insite/methods/common_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def document(xpath = nil)
end
alias nokogiri document

# Returns a string representation of the page.
def inspect
"#<#{self.class.name}:#{object_id} @url=#{@browser.url}>"
end

private
def process_browser
Expand Down Expand Up @@ -57,6 +53,46 @@ def process_browser

end
public
# private
# def process_browser
# if @site.browser.is_a?(Watir::Browser)
# begin
# if @site.browser.exists?
# return @site.browser
# else
# raise(
# Insite::Errors::BrowserClosedError,
# "Browser check failed. The browser is no longer present.\n\n"
# )
# end
# rescue(Insite::Errors::BrowserNotOpenError) => e
# raise e
# rescue => e
# raise(
# Insite::Errors::BrowserResponseError,
# <<~eos
# Browser check failed. The browser returned an #{e.class} (#{e}) when it was queried.
# Backtrace for the error:
# #{e.backtrace.join("\n")}
#
# eos
# )
# end
# elsif @site.browser.nil?
# raise(
# Insite::Errors::BrowserNotOpenError,
# "A browser has not been defined for the site. Try using Site#open to " \
# "start a browser.\n\n"
# )
# else
# raise(
# Insite::Errors::BrowserNotValidError,
# "Expected: Watir::Browser. Actual: #{@site.browser.class}.\n\n"
# )
# end
#
# end
# public

def update_object(hash_args = {})
rescues = [
Expand Down
20 changes: 10 additions & 10 deletions lib/insite/page/defined_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@ def set_url_matcher(regexp)
regexp ? @url_matcher = regexp : nil
end

def component_method(method_name, component_symbol, component_method, target_element)
@component_methods ||= []
@component_methods << method_name.to_sym unless @component_methods.include?(method_name.to_sym)

define_method(method_name) do |*args, &block|
self.class.const_get(component_symbol.to_s.camelize)
.new(@site, @site.send(target_element))
.send(component_method, *args, &block)
end
end
# def component_method(method_name, component_symbol, component_method, target_element)
# @component_methods ||= []
# @component_methods << method_name.to_sym unless @component_methods.include?(method_name.to_sym)
#
# define_method(method_name) do |*args, &block|
# self.class.const_get(component_symbol.to_s.camelize)
# .new(@site, @site.send(target_element))
# .send(component_method, *args, &block)
# end
# end
end # Self.

def defined?
Expand Down
23 changes: 20 additions & 3 deletions spec/site_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require_relative 'support/spec_helper'
require_relative 'support/ruby_lang_site'

describe "Page Object" do
# TODO: Needs organizing
describe "page objects" do

before(:all) do
@github = GithubSite.new('https://github.com/')
Expand All @@ -19,6 +20,20 @@
@github.close
end

context "site methods" do
it "returns false for #browser? when browser doesn't exist" do
expect(GithubSite.new('https://github.com/').browser?).to eq false
end

it "returns a driver object when #driver is called" do
expect(@github.driver).to be_instance_of Selenium::WebDriver::Chrome::Driver
end

it "returns false for #driver? when driver doesn't exist" do
expect(GithubSite.new('https://github.com/').driver?).to eq false
end
end

it "raises when the visit method is called on a page that does not allow navigation" do
@site.news_page.posts.first.link.click
expect(@site.news_post_page?).to be_truthy
Expand All @@ -44,7 +59,7 @@
end

it "raises a PageInitError when hash args don't include required param" do
@site.arguments[:language] = nil
@site.arguments[:language] = nil
expect { @site.foo_attr_page wrong: 'argument' }.to raise_error Insite::Errors::PageInitError
end

Expand Down Expand Up @@ -80,7 +95,9 @@
end

it "raises an error when a page matcher doesn't match when the page is visited" do
expect { @site.testing_page_bad_matcher.visit }.to raise_error Insite::Errors::WrongPageError
expect {
@site.testing_page_bad_matcher.visit
}.to raise_error Insite::Errors::WrongPageError
end

it "defines a ? method to verify page display" do
Expand Down
7 changes: 7 additions & 0 deletions spec/support/ruby_lang_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class BadSite
include Insite
end

# class SiteWithBadPageMatcher
# include Insite
#
# class TestingPageStringMatcher < RubyLangTemplate
# set_url_matcher 'whoops'
# end
# end
# class BadPage < BadSite::Page
# set_url_matcher 'invalid'
# end
Expand Down

0 comments on commit cf55aee

Please sign in to comment.