Class: Simplexframe::Navigator

Inherits:
Object
  • Object
show all
Defined in:
lib/simplexframe/simple_navigator.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Navigator

Returns a new instance of Navigator.



5
6
7
8
9
10
11
12
# File 'lib/simplexframe/simple_navigator.rb', line 5

def initialize config
	@config = config.clone
	defined_browser?
	@browser = start_browser
	@browser.cookies.clear
	raise "can not start browser, maybe you need to download the driver for #{@config.browser}" if @browser.nil?
	define_goto_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/simplexframe/simple_navigator.rb', line 56

def method_missing(m, *args, &blk)
	if @browser.respond_to? m
		@browser.send(m, *args, &blk)
	else
		super
	end #if
end

Instance Method Details

#define_goto_methodsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simplexframe/simple_navigator.rb', line 34

def define_goto_methods
	p Module.constants.grep(/Page$/) if $debug
	Module.constants.grep(/Page$/).each do |page_klass|
		if simplexframe_page?(page_klass)
			# define_method is private,so using send
			self.class.send :define_method, "goto_#{page_klass.to_s.underscore}" do
				page = Module.const_get(page_klass).new(@browser)
				page.goto
				page
			end #define_method
			puts "defined goto_#{page_klass.to_s.underscore}" if $debug
		end #if
	end #each
end

#defined_browser?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/simplexframe/simple_navigator.rb', line 14

def defined_browser?
	if !@config.respond_to?(:browser) || @config.browser.empty?
		raise IncorrectConfigFileError, 'you should define browser in your config file'
	end #if
end

#start_browserObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simplexframe/simple_navigator.rb', line 20

def start_browser
	
	unless @config.url.nil?
		capabilities = Selenium::WebDriver::Remote::Capabilities.new
		capabilities.browser_name = @config.browser.to_sym

		browser = Watir::Browser.new( 
			:remote, 
			:url => @config.url, 
			:desired_capabilities => capabilities)
	else
		Watir::Browser.new @config.browser.to_sym
	end
end

#valid_page_klass?(klass) ⇒ Boolean Also known as: simplexframe_page?

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/simplexframe/simple_navigator.rb', line 49

def valid_page_klass? klass
	# skip Simplexframe::Page
	return false if klass.eql?(:Page)
	Module.const_get(klass) < Simplexframe::Page
end