Class: SmartDriver
- Inherits:
-
Object
show all
- Defined in:
- lib/smart_driver.rb,
lib/smart_driver/version.rb
Constant Summary
collapse
- VERSION =
"1.0.1"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(url = nil, browser = :chrome) ⇒ SmartDriver
Returns a new instance of SmartDriver.
6
7
8
9
|
# File 'lib/smart_driver.rb', line 6
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
82
83
84
|
# File 'lib/smart_driver.rb', line 82
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__.
4
5
6
|
# File 'lib/smart_driver.rb', line 4
def __driver__
@__driver__
end
|
Instance Method Details
#click(selector) ⇒ Object
64
65
66
67
|
# File 'lib/smart_driver.rb', line 64
def click(selector)
logging :info, "click #{selector}..."
has?(selector) ? find(selector).click : false
end
|
#exec_js(js_code) ⇒ Object
74
75
76
|
# File 'lib/smart_driver.rb', line 74
def exec_js(js_code)
@__driver__.execute_script js_code
end
|
#find(selector) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/smart_driver.rb', line 20
def find(selector)
logging :info, "find #{selector}..."
@__driver__.find_element(css: selector)
rescue Selenium::WebDriver::Error::NoSuchElementError
logging :fail, "#{selector} cannot be found"
end
|
#find_by_text(text) ⇒ Object
34
35
36
37
38
39
|
# File 'lib/smart_driver.rb', line 34
def find_by_text(text)
logging :info, "find by text #{text}..."
@__driver__.find_element({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
rescue Selenium::WebDriver::Error::NoSuchElementError
logging :fail, "element with #{text} cannot be found"
end
|
#finds(selector) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/smart_driver.rb', line 27
def finds(selector)
logging :info, "finds #{selector}..."
@__driver__.find_elements(css: selector)
rescue Selenium::WebDriver::Error::NoSuchElementError
logging :fail, "#{selector} cannot be found"
end
|
#finds_by_text(text) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/smart_driver.rb', line 41
def finds_by_text(text)
logging :info, "finds by text #{text}..."
@__driver__.find_elements({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
rescue Selenium::WebDriver::Error::NoSuchElementError
logging :fail, "elements with #{text} cannot be found"
end
|
#go(url) ⇒ Object
11
12
13
14
|
# File 'lib/smart_driver.rb', line 11
def go(url)
logging :info, "visiting #{url}..."
@__driver__.navigate.to(url)
end
|
#has?(selector) ⇒ Boolean
56
57
58
|
# File 'lib/smart_driver.rb', line 56
def has?(selector)
!!find(selector)
end
|
#has_text?(text) ⇒ Boolean
60
61
62
|
# File 'lib/smart_driver.rb', line 60
def has_text?(text)
!!find_by_text(text)
end
|
#reload ⇒ Object
16
17
18
|
# File 'lib/smart_driver.rb', line 16
def reload
@__driver__.navigate.refresh
end
|
#save_html(file_path) ⇒ Object
78
79
80
|
# File 'lib/smart_driver.rb', line 78
def save_html(file_path)
File.open(file_path, 'w') { |f| f.write(@__driver__.page_source) }
end
|
49
50
51
52
53
54
|
# File 'lib/smart_driver.rb', line 49
def scroll(selector)
logging :info, "scroll to #{selector}..."
element = find(selector)
exec_js "window.scrollTo(#{element.location.x},#{element.location.y})"
element
end
|
#submit ⇒ Object
69
70
71
72
|
# File 'lib/smart_driver.rb', line 69
def submit
logging :info, "submit form ..."
$focus.submit if $focus
end
|
#switch_window(num) ⇒ Object
86
87
88
|
# File 'lib/smart_driver.rb', line 86
def switch_window(num)
@__driver__.switch_to.window @__driver__.window_handles[num]
end
|