smart_driver 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/smart_driver/common_interface.rb +48 -0
- data/lib/smart_driver/version.rb +1 -1
- data/lib/smart_driver.rb +16 -83
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7f1e40525ae0561f12ad7d767e7327f9a9647c2
|
4
|
+
data.tar.gz: 487c68a8c6cb46e791dcb59444ccd6897671a589
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc48552df327c3b63ef5b08baf0503edaa024feafbaf8c9d3af040f5da8299026c71c4c6a6247d6a88f1c15de41a802ad3235d98a783dca8c63b13ed23ed6eb0
|
7
|
+
data.tar.gz: ed46c725f6e46070605f94087e32d1c86eafe17c0c5e3d1dd7d994e89b5f07c05ad51700ade91fee23a98aaf62133237a6335ffdddc82bb1e1e17bf03fac9b89
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class SmartDriver
|
2
|
+
module CommonInterface
|
3
|
+
def find(selector)
|
4
|
+
logging :info, "find #{selector}..."
|
5
|
+
@__driver__.find_element(css: selector)
|
6
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError
|
7
|
+
logging :fail, "#{selector} cannot be found"
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def finds(selector)
|
12
|
+
logging :info, "finds #{selector}..."
|
13
|
+
@__driver__.find_elements(css: selector)
|
14
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError
|
15
|
+
logging :fail, "#{selector} cannot be found"
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_text(text)
|
20
|
+
logging :info, "find text '#{text}'..."
|
21
|
+
@__driver__.find_element({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
|
22
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError
|
23
|
+
logging :fail, "text '#{text}' cannot be found"
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def finds_text(text)
|
28
|
+
logging :info, "finds text '#{text}'..."
|
29
|
+
@__driver__.find_elements({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
|
30
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError
|
31
|
+
logging :fail, "text #{text} cannot be found"
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_html
|
36
|
+
attribute("outerHTML")
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def logging(sym, text)
|
41
|
+
label = case sym
|
42
|
+
when :info then "INFO"
|
43
|
+
when :fail then "FAIL"
|
44
|
+
end
|
45
|
+
puts "[#{label}] #{text}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/smart_driver/version.rb
CHANGED
data/lib/smart_driver.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
require 'selenium-webdriver'
|
2
2
|
|
3
|
+
require 'smart_driver/common_interface'
|
4
|
+
|
3
5
|
class SmartDriver
|
4
6
|
attr_accessor :__driver__
|
7
|
+
attr_reader :log_dir_path
|
8
|
+
include SmartDriver::CommonInterface
|
5
9
|
|
6
|
-
def initialize(url=nil, browser=:chrome)
|
10
|
+
def initialize(url=nil, log_dir_path="./log", browser=:chrome)
|
7
11
|
@__driver__ = Selenium::WebDriver.for(browser)
|
12
|
+
FileUtils.mkdir_p log_dir_path
|
13
|
+
@log_dir_path = log_dir_path
|
8
14
|
go(url) if url
|
9
15
|
end
|
10
16
|
|
@@ -17,55 +23,6 @@ class SmartDriver
|
|
17
23
|
@__driver__.navigate.refresh
|
18
24
|
end
|
19
25
|
|
20
|
-
def find(selector)
|
21
|
-
logging :info, "find #{selector}..."
|
22
|
-
@__driver__.find_element(css: selector)
|
23
|
-
rescue Selenium::WebDriver::Error::NoSuchElementError
|
24
|
-
logging :fail, "#{selector} cannot be found"
|
25
|
-
end
|
26
|
-
|
27
|
-
def finds(selector)
|
28
|
-
logging :info, "finds #{selector}..."
|
29
|
-
@__driver__.find_elements(css: selector)
|
30
|
-
rescue Selenium::WebDriver::Error::NoSuchElementError
|
31
|
-
logging :fail, "#{selector} cannot be found"
|
32
|
-
end
|
33
|
-
|
34
|
-
def find_by_text(text)
|
35
|
-
logging :info, "find by text #{text}..."
|
36
|
-
@__driver__.find_element({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
|
37
|
-
rescue Selenium::WebDriver::Error::NoSuchElementError
|
38
|
-
logging :fail, "element with #{text} cannot be found"
|
39
|
-
end
|
40
|
-
|
41
|
-
def finds_by_text(text)
|
42
|
-
logging :info, "finds by text #{text}..."
|
43
|
-
@__driver__.find_elements({xpath: "//*[text()[contains(.,\"#{text}\")]]"})
|
44
|
-
rescue Selenium::WebDriver::Error::NoSuchElementError
|
45
|
-
logging :fail, "elements with #{text} cannot be found"
|
46
|
-
end
|
47
|
-
|
48
|
-
# http://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error
|
49
|
-
def scroll(selector)
|
50
|
-
logging :info, "scroll to #{selector}..."
|
51
|
-
element = find(selector)
|
52
|
-
exec_js "window.scrollTo(#{element.location.x},#{element.location.y})"
|
53
|
-
element
|
54
|
-
end
|
55
|
-
|
56
|
-
def has?(selector)
|
57
|
-
!!find(selector)
|
58
|
-
end
|
59
|
-
|
60
|
-
def has_text?(text)
|
61
|
-
!!find_by_text(text)
|
62
|
-
end
|
63
|
-
|
64
|
-
def click(selector)
|
65
|
-
logging :info, "click #{selector}..."
|
66
|
-
has?(selector) ? find(selector).click : false
|
67
|
-
end
|
68
|
-
|
69
26
|
def submit
|
70
27
|
logging :info, "submit form ..."
|
71
28
|
$focus.submit if $focus
|
@@ -75,8 +32,12 @@ class SmartDriver
|
|
75
32
|
@__driver__.execute_script js_code
|
76
33
|
end
|
77
34
|
|
78
|
-
def save_html(
|
79
|
-
File.open(
|
35
|
+
def save_html(file_name="log.html")
|
36
|
+
File.open("#{@log_dir_path}/#{file_name}", 'w') { |f| f.write(@__driver__.page_source) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def save_png(file_name="log.png")
|
40
|
+
@__driver__.save_screenshot "#{@log_dir_path}/#{file_name}"
|
80
41
|
end
|
81
42
|
|
82
43
|
def method_missing(method, *args, &block)
|
@@ -86,42 +47,14 @@ class SmartDriver
|
|
86
47
|
def switch_window(num)
|
87
48
|
@__driver__.switch_to.window @__driver__.window_handles[num]
|
88
49
|
end
|
89
|
-
|
90
|
-
private
|
91
|
-
def logging(sym, text)
|
92
|
-
label = case sym
|
93
|
-
when :info then "INFO"
|
94
|
-
when :fail then "FAIL"
|
95
|
-
end
|
96
|
-
puts "[#{label}] #{text}"
|
97
|
-
end
|
98
50
|
end
|
99
51
|
|
100
52
|
class Selenium::WebDriver::Element
|
53
|
+
include SmartDriver::CommonInterface
|
54
|
+
|
101
55
|
def fill(text)
|
102
56
|
$focus = self
|
103
|
-
logging :info, "fill #{text}
|
57
|
+
logging :info, "fill '#{text}'"
|
104
58
|
send_key(text)
|
105
59
|
end
|
106
|
-
|
107
|
-
def find(selector)
|
108
|
-
find_element(css: selector)
|
109
|
-
end
|
110
|
-
|
111
|
-
def finds(selector)
|
112
|
-
find_elements(css: selector)
|
113
|
-
end
|
114
|
-
|
115
|
-
def to_html
|
116
|
-
attribute("outerHTML")
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
def logging(sym, text)
|
121
|
-
label = case sym
|
122
|
-
when :info then "INFO"
|
123
|
-
when :fail then "FAIL"
|
124
|
-
end
|
125
|
-
puts "[#{label}] #{text}"
|
126
|
-
end
|
127
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_driver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gogotanaka
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- bin/setup
|
85
85
|
- exe/smart_driver
|
86
86
|
- lib/smart_driver.rb
|
87
|
+
- lib/smart_driver/common_interface.rb
|
87
88
|
- lib/smart_driver/version.rb
|
88
89
|
- smart_driver.gemspec
|
89
90
|
homepage: http://aisaac.in
|