Created
March 9, 2016 04:09
-
-
Save jasonkarns/8cdbb5461d895e9d4cbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'axe/configuration' | |
module Axe | |
class FindsPage | |
WEBDRIVER_NAMES = [ :page, :browser, :driver, :webdriver ] | |
class << self | |
alias :in :new | |
end | |
def initialize(world) | |
@world = world | |
end | |
def page | |
from_configuration || implicit or raise "A page/browser/webdriver must be configured" | |
end | |
private | |
def configuration | |
Axe::Configuration.instance | |
end | |
def from_configuration | |
if configuration.page.is_a?(String) || configuration.page.is_a?(Symbol) | |
from_world(configuration.page) | |
else | |
configuration.page | |
end | |
end | |
def implicit | |
WEBDRIVER_NAMES.map(&method(:from_world)).drop_while(&:nil?).first | |
end | |
def from_world(name) | |
via_method(name) || via_ivar(name) | |
end | |
def via_method(name) | |
@world.__send__(name) if @world.respond_to?(name) | |
end | |
def via_ivar(name) | |
name = ensure_ivar_format(name) | |
@world.instance_variable_get(name) if @world.instance_variables.include?(name) | |
end | |
def ensure_ivar_format(name) | |
# ensure leading '@' | |
name.to_s.sub(/^([^@])/, '@\1').to_sym | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment