Module: PageObject::PageInstanceMethods
- Defined in:
- lib/site-object/page.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#has_fragment ⇒ Object
readonly
Returns the value of attribute has_fragment.
-
#page_attributes ⇒ Object
readonly
Returns the value of attribute page_attributes.
-
#page_elements ⇒ Object
readonly
Returns the value of attribute page_elements.
-
#page_features ⇒ Object
readonly
Returns the value of attribute page_features.
-
#page_url ⇒ Object
readonly
Returns the value of attribute page_url.
-
#query_arguments ⇒ Object
readonly
Returns the value of attribute query_arguments.
-
#required_arguments ⇒ Object
readonly
Returns the value of attribute required_arguments.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
-
#url_matcher ⇒ Object
readonly
Returns the value of attribute url_matcher.
-
#url_template ⇒ Object
readonly
Returns the value of attribute url_template.
Instance Method Summary collapse
-
#expect_page(page) ⇒ Object
Takes the name of a page class.
-
#initialize(site, args = nil) ⇒ Object
There’s no need to ever call this directly.
-
#inspect ⇒ Object
Custom inspect method so that console output doesn’t get in the way when debugging.
- #navigation_disabled? ⇒ Boolean
- #on_page? ⇒ Boolean
-
#refresh ⇒ Object
Refreshes the page.
-
#visit ⇒ Object
Navigates to the page that it’s called on.
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def arguments @arguments end |
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def browser @browser end |
#has_fragment ⇒ Object (readonly)
Returns the value of attribute has_fragment.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def has_fragment @has_fragment end |
#page_attributes ⇒ Object (readonly)
Returns the value of attribute page_attributes.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def page_attributes @page_attributes end |
#page_elements ⇒ Object (readonly)
Returns the value of attribute page_elements.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def page_elements @page_elements end |
#page_features ⇒ Object (readonly)
Returns the value of attribute page_features.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def page_features @page_features end |
#page_url ⇒ Object (readonly)
Returns the value of attribute page_url.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def page_url @page_url end |
#query_arguments ⇒ Object (readonly)
Returns the value of attribute query_arguments.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def query_arguments @query_arguments end |
#required_arguments ⇒ Object (readonly)
Returns the value of attribute required_arguments.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def required_arguments @required_arguments end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def site @site end |
#url_matcher ⇒ Object (readonly)
Returns the value of attribute url_matcher.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def url_matcher @url_matcher end |
#url_template ⇒ Object (readonly)
Returns the value of attribute url_template.
324 325 326 |
# File 'lib/site-object/page.rb', line 324 def url_template @url_template end |
Instance Method Details
#expect_page(page) ⇒ Object
Takes the name of a page class. If the current page is of that class then it returns a page object for the page. Raises a SiteObject::WrongPageError if that’s not the case. It’s generally not a good idea to put error checking inside a page object. This should only be used in cases where there is a page transition and that transition is always expected to work.
330 331 332 |
# File 'lib/site-object/page.rb', line 330 def expect_page(page) @site.expect_page(page) end |
#initialize(site, args = nil) ⇒ Object
There’s no need to ever call this directly. Initializes a page object within the context of a site object. Takes a site object and a hash of configuration arguments. The site object will handle all of this for you.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/site-object/page.rb', line 337 def initialize(site, args=nil) @browser = site.browser @page_attributes = self.class.page_attributes @page_url = self.class.page_url @page_elements = self.class.page_elements @page_features = self.class.page_features @required_arguments = self.class.required_arguments @site = site @url_matcher = self.class.url_matcher @url_template = self.class.url_template @query_arguments = self.class.query_arguments @has_fragment = self.class.has_fragment # Try to expand the URL template if the URL has parameters. @arguments = {}.with_indifferent_access # Stores the param list that will expand the url_template after examining the arguments used to initialize the page. if @required_arguments.present? && !args @required_arguments.each do |arg| if @site.respond_to?(arg) @arguments[arg]= site.send(arg) else raise SiteObject::PageInitError, "No arguments provided when attempting to initialize #{self.class.name}. This page object requires the following arguments for initialization: :#{@required_arguments.join(', :')}.\n\n#{caller.join("\n")}" end end elsif @required_arguments.present? @required_arguments.each do |arg| # Try to extract each URL argument from the hash or object provided, OR from the site object. if args.is_a?(Hash) && args.present? args = args.with_indifferent_access if args[arg] #The hash has the required argument. @arguments[arg]= args[arg] elsif @site.respond_to?(arg) @arguments[arg]= site.send(arg) else raise SiteObject::PageInitError, "A required page argument is missing. #{args.class} was provided, but this object did not respond to :#{arg}, which is necessary to build an URL for the #{self.class.name} page.\n\n#{caller.join("\n")}" end elsif args # Some non-hash object was provided. if args.respond_to?(arg) #The hash has the required argument. @arguments[arg]= args.send(arg) elsif @site.respond_to?(arg) @arguments[arg]= site.send(arg) else raise SiteObject::PageInitError, "A required page argument is missing. #{args.class} was provided, but this object did not respond to :#{arg}, which is necessary to build an URL for the #{self.class.name} page.\n\n#{caller.join("\n")}" end else # Do nothing here yet. end end elsif @required_arguments.empty? && args # If there are no required arguments then nothing should be provided. raise SiteObject::PageInitError, "#{args.class} was provided as a #{self.class.name} initialization argument, but the page URL doesn't require any arguments.\n\n#{caller.join("\n")}" else # Do nothing here yet. end @url = @url_template.(@arguments).to_s @page_features ||= [] @page_features.each do |arg| self.class_eval do klass = eval("#{arg.to_s.camelize}") if klass.alias define_method(klass.alias) do klass.new(@browser, args) end else define_method(arg) do klass.new(@browser, args) end end end end @site.most_recent_page = self unless on_page? if raise SiteObject::PageNavigationNotAllowedError, "Navigation is intentionally disabled for the #{self.class.name} page. You can only call the accessor method for this page when it's already being displayed in the browser.\n\nCurrent URL:\n------------\n#{@site.browser.url}\n\n#{caller.join("\n")}" end visit end end |
#inspect ⇒ Object
Custom inspect method so that console output doesn’t get in the way when debugging.
418 419 420 |
# File 'lib/site-object/page.rb', line 418 def inspect "#<#{self.class.name}:#{object_id} @url_template=#{@url_template.inspect}>" end |
#navigation_disabled? ⇒ Boolean
454 455 456 |
# File 'lib/site-object/page.rb', line 454 def @page_attributes.include? :navigation_disabled end |
#on_page? ⇒ Boolean
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/site-object/page.rb', line 422 def on_page? if @browser.is_a? Watir::Browser url = @browser.url elsif @browser.is_a? Selenium::WebDriver::Driver url = @browser.current_url else raise SiteObject::BrowserLibraryNotSupportedError, "Unsupported browser library: #{@browser.class}" end if query_arguments if @has_fragment url = url.split(/#/)[0] end else url = url.split(/\?/)[0] end if @url_matcher && @url_matcher =~ url return true elsif @url_template.match(url) if @arguments.empty? return true else if pargs = @url_template.extract(Addressable::URI.parse(url)) pargs = pargs.with_indifferent_access @required_arguments.all? { |k| pargs[k] == @arguments[k].to_s } end end end end |
#refresh ⇒ Object
Refreshes the page.
459 460 461 462 463 464 465 466 467 468 |
# File 'lib/site-object/page.rb', line 459 def refresh # TODO: Isolate browser library-specific code so that the adding a new browser library is cleaner. if @browser.is_a?(Watir::Browser) @browser.refresh elsif @browser.is_a?(Selenium::WebDriver::Driver) @browser.navigate.refresh else raise SiteObject::BrowserLibraryNotSupportedError, "Only Watir-Webdriver and Selenium Webdriver are currently supported. Class of browser object: #{@browser.class.name}" end self end |
#visit ⇒ Object
Navigates to the page that it’s called on. Raises a SiteObject::PageNavigationNotAllowedError when navigation has been disabled for the page. Raises a SiteObject::WrongPageError if the specified page isn’t getting displayed after navigation.
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/site-object/page.rb', line 473 def visit if raise SiteObject::PageNavigationNotAllowedError, "Navigation has been disabled for the #{self.class.name} page. This was done when defining the page class and usually means that the page can't be reached directly through a URL and requires some additional work to access." end if @browser.is_a?(Watir::Browser) @browser.goto(@url) elsif @browser.is_a?(Selenium::WebDriver::Driver) @browser.get(@url) else raise SiteObject::BrowserLibraryNotSupportedError, "Only Watir-Webdriver and Selenium Webdriver are currently supported. Class of browser object: #{@browser.class.name}" end if @url_matcher raise SiteObject::WrongPageError, "Navigation check failed after attempting to access the #{self.class.name} page. Current URL #{@browser.url} did not match #{@url_template.pattern}. A URL matcher was also defined for the page and the secondary check against the URL matcher also failed. URL matcher: #{@url_matcher}" unless on_page? else raise SiteObject::WrongPageError, "Navigation check failed after attempting to access the #{self.class.name} page. Current URL #{@browser.url} did not match #{@url_template.pattern}" unless on_page? end @site.most_recent_page = self self end |