Class: SmartDriver

Inherits:
Object
  • Object
show all
Includes:
CommonInterface
Defined in:
lib/smart_driver.rb,
lib/smart_driver/version.rb,
lib/smart_driver/common_interface.rb

Defined Under Namespace

Modules: CommonInterface

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonInterface

#find, #find_text, #finds, #finds_text, #to_html

Constructor Details

#initialize(url = nil, log_dir_path = "./log", browser = :chrome) ⇒ SmartDriver



10
11
12
13
14
15
# File 'lib/smart_driver.rb', line 10

def initialize(url=nil, log_dir_path="./log", browser=:chrome)
  @__driver__ = Selenium::WebDriver.for(browser)
  FileUtils.mkdir_p log_dir_path
  @log_dir_path = log_dir_path
  go(url) if url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



43
44
45
# File 'lib/smart_driver.rb', line 43

def method_missing(method, *args, &block)
  @__driver__.respond_to?(method) ? @__driver__.send(method, *args, &block) : super
end

Instance Attribute Details

#__driver__Object

Returns the value of attribute __driver__.



6
7
8
# File 'lib/smart_driver.rb', line 6

def __driver__
  @__driver__
end

#log_dir_pathObject (readonly)

Returns the value of attribute log_dir_path.



7
8
9
# File 'lib/smart_driver.rb', line 7

def log_dir_path
  @log_dir_path
end

Instance Method Details

#exec_js(js_code) ⇒ Object



31
32
33
# File 'lib/smart_driver.rb', line 31

def exec_js(js_code)
  @__driver__.execute_script js_code
end

#go(url) ⇒ Object



17
18
19
20
# File 'lib/smart_driver.rb', line 17

def go(url)
  logging :info, "visiting #{url}..."
  @__driver__.navigate.to(url)
end

#reloadObject



22
23
24
# File 'lib/smart_driver.rb', line 22

def reload
  @__driver__.navigate.refresh
end

#save_html(file_name = "log.html") ⇒ Object



35
36
37
# File 'lib/smart_driver.rb', line 35

def save_html(file_name="log.html")
  File.open("#{@log_dir_path}/#{file_name}", 'w') { |f| f.write(@__driver__.page_source) }
end

#save_png(file_name = "log.png") ⇒ Object



39
40
41
# File 'lib/smart_driver.rb', line 39

def save_png(file_name="log.png")
  @__driver__.save_screenshot "#{@log_dir_path}/#{file_name}"
end

#submitObject



26
27
28
29
# File 'lib/smart_driver.rb', line 26

def submit
  logging :info, "submit form ..."
  $focus.submit if $focus
end

#switch_window(num) ⇒ Object



47
48
49
# File 'lib/smart_driver.rb', line 47

def switch_window(num)
  @__driver__.switch_to.window @__driver__.window_handles[num]
end