Class: Kirchhoff::Driver

Inherits:
Object
  • Object
show all
Includes:
CommonInterface
Defined in:
lib/kirchhoff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonInterface

#find, #find_text, #multi_find, #multi_find_text, #to_nokogiri

Constructor Details

#initialize(browser: :chrome, default_timeout: 6) ⇒ Driver

Returns a new instance of Driver.



29
30
31
32
# File 'lib/kirchhoff.rb', line 29

def initialize(browser: :chrome, default_timeout: 6)
  @__driver__      = Selenium::WebDriver.for(browser)
  @default_timeout = default_timeout
end

Instance Attribute Details

#__driver__Object

Returns the value of attribute __driver__.



9
10
11
# File 'lib/kirchhoff.rb', line 9

def __driver__
  @__driver__
end

#default_timeoutObject

Returns the value of attribute default_timeout.



9
10
11
# File 'lib/kirchhoff.rb', line 9

def default_timeout
  @default_timeout
end

#log_dir_pathObject (readonly)

Returns the value of attribute log_dir_path.



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

def log_dir_path
  @log_dir_path
end

Instance Method Details

#current_urlObject



21
22
23
# File 'lib/kirchhoff.rb', line 21

def current_url
  @__driver__.current_url
end

#exec_js(js_code) ⇒ Object



48
49
50
# File 'lib/kirchhoff.rb', line 48

def exec_js(js_code)
  @__driver__.execute_script js_code
end

#find_element(selector) ⇒ Object



13
14
15
# File 'lib/kirchhoff.rb', line 13

def find_element selector
  @__driver__.find_element selector
end

#find_elements(selector) ⇒ Object



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

def find_elements selector
  @__driver__.find_elements selector
end

#go(url) ⇒ Object



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

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

#quitObject



25
26
27
# File 'lib/kirchhoff.rb', line 25

def quit
  @__driver__.quit
end

#reloadObject



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

def reload
  @__driver__.navigate.refresh
end

#save_html(file_path) ⇒ Object



52
53
54
# File 'lib/kirchhoff.rb', line 52

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

#save_png(file_path) ⇒ Object



56
57
58
# File 'lib/kirchhoff.rb', line 56

def save_png(file_path)
  @__driver__.save_screenshot file_path
end

#submitObject



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

def submit
  $focus.submit
  Kirchhoff::Logger.call :info, "submit form ..."
end

#switch_window(num) ⇒ Object



60
61
62
# File 'lib/kirchhoff.rb', line 60

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

#to_htmlObject



82
83
84
# File 'lib/kirchhoff.rb', line 82

def to_html
  self.__driver__.page_source
end

#wait_element(selector, maybe: true, t: nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/kirchhoff.rb', line 64

def wait_element(selector, maybe: true, t: nil)
  wait = Selenium::WebDriver::Wait.new(timeout: (t || @default_timeout))
  wait.until { self.find_element(css: selector) }
rescue Selenium::WebDriver::Error::TimeOutError
  unless maybe
    raise Selenium::WebDriver::Error::TimeOutError, "selector: #{selector}"
  end
end

#wait_text(text, maybe: true, t: nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/kirchhoff.rb', line 73

def wait_text(text, maybe: true, t: nil)
  wait = Selenium::WebDriver::Wait.new(timeout: (t || @default_timeout))
  wait.until { self.find_element(xpath: "//*[text()[contains(.,\"#{text}\")]]") }
rescue Selenium::WebDriver::Error::TimeOutError
  unless maybe
    raise Selenium::WebDriver::Error::TimeOutError, "text: #{text}"
  end
end