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.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonInterface

#find, #find_text, #finds, #finds_text, #has?, #has_text?, #to_html

Constructor Details

#initialize(url = nil, browser = :chrome) ⇒ SmartDriver

Returns a new instance of SmartDriver.



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

def initialize(url=nil, browser=:chrome)
  @__driver__ = Selenium::WebDriver.for(browser)
  go(url) if url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



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

def exec_js(js_code)
  @__driver__.execute_script js_code
end

#go(url) ⇒ Object



15
16
17
18
# File 'lib/smart_driver.rb', line 15

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

#maybe(&block) ⇒ Object



55
56
57
58
59
# File 'lib/smart_driver.rb', line 55

def maybe(&block)
  block.call()
rescue Selenium::WebDriver::Error::NoSuchElementError
  nil
end

#reloadObject



20
21
22
# File 'lib/smart_driver.rb', line 20

def reload
  @__driver__.navigate.refresh
end

#save_html(file_path) ⇒ Object



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

def save_html(file_path)
  File.open(file_path, 'w') { |f| f.write(@__driver__.page_source) }
end

#save_png(file_path) ⇒ Object



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

def save_png(file_path)
  @__driver__.save_screenshot file_path
end

#submit(n = 20) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/smart_driver.rb', line 24

def submit(n=20)
  logging :info, "submit form ..."
  $focus.submit
  if block_given?
    n.times do
      break if yield()
      sleep 0.5
    end
  end
end

#switch_window(num) ⇒ Object



51
52
53
# File 'lib/smart_driver.rb', line 51

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