class SmartDriver
Constants
- VERSION
Attributes
__driver__[RW]
log_dir_path[R]
Public Class Methods
new(url=nil, browser=:chrome)
click to toggle source
# File lib/smart_driver.rb, line 10 def initialize(url=nil, browser=:chrome) @__driver__ = Selenium::WebDriver.for(browser) go(url) if url end
Public Instance Methods
exec_js(js_code)
click to toggle source
# File lib/smart_driver.rb, line 39 def exec_js(js_code) @__driver__.execute_script js_code end
go(url, t=10, &block)
click to toggle source
# File lib/smart_driver.rb, line 15 def go(url, t=10, &block) logging :info, "visiting #{url}..." @__driver__.navigate.to(url) if block_given? wait = Selenium::WebDriver::Wait.new(timeout: t) wait.until(&block) end end
maybe(&block)
click to toggle source
# File lib/smart_driver.rb, line 59 def maybe(&block) block.call() rescue Selenium::WebDriver::Error::NoSuchElementError nil end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/smart_driver.rb, line 51 def method_missing(method, *args, &block) @__driver__.respond_to?(method) ? @__driver__.send(method, *args, &block) : super end
reload()
click to toggle source
# File lib/smart_driver.rb, line 24 def reload @__driver__.navigate.refresh end
save_html(file_path)
click to toggle source
# File lib/smart_driver.rb, line 43 def save_html(file_path) File.open(file_path, 'w') { |f| f.write(@__driver__.page_source) } end
save_png(file_path)
click to toggle source
# File lib/smart_driver.rb, line 47 def save_png(file_path) @__driver__.save_screenshot file_path end
submit(n=20) { || ... }
click to toggle source
# File lib/smart_driver.rb, line 28 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)
click to toggle source
# File lib/smart_driver.rb, line 55 def switch_window(num) @__driver__.switch_to.window @__driver__.window_handles[num] end