-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenium.rb
115 lines (97 loc) · 2.15 KB
/
selenium.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'selenium-webdriver'
module Neo
module Selenium
def initialize
@driver = nil
@wait = nil
end
def get(link)
if @driver.nil?
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => {
'args' => %w(test-type disable-popup-blocking)
})
@driver = ::Selenium::WebDriver.for :chrome,desired_capabilities: caps
@wait = ::Selenium::WebDriver::Wait.new timeout: 20
end
@driver.get link
end
def maximize
@driver.manage.window.maximize
end
def find(css_directive)
elem = nil
@wait.until { elem = @driver.find_element :css, css_directive }
elem
end
def find_all(css_directive)
elem = nil
@wait.until {
elem = @driver.find_elements :css, css_directive
elem.length > 0
}
elem
end
def page_contains?(text)
source.include? text
end
def save_screen(file_name)
@driver.save_screenshot(file_name)
end
def source
@driver.page_source
end
def url
@driver.current_url
end
def navigate_to(direction)
if direction == :back
@driver.navigate.back
elsif direction == :forward
@driver.navigate.forward
elsif direction == :refresh
@driver.navigate.refresh
else
@driver.navigate.to direction
end
end
def switch_to(window)
if window == :last
@driver.switch_to.window get_tab_list.last
else
@driver.switch_to.window window
end
end
def handle
@driver.window_handle
end
def get_tab_list
@driver.window_handles
end
def new_tab(url=nil)
@driver.execute_script "window.open('#{url}')"
end
def close_tab(tab_handle)
current_tab = handle
if tab_handle == :last
switch_to :last
tab_handle = handle
else
switch_to tab_handle
end
is_self = (current_tab == tab_handle)
@driver.close
if is_self
switch_to :last
else
switch_to current_tab
end
end
def ctrl_click(clickable_element)
@driver.action.key_down(:control).
click(clickable_element).
key_up(:control).
perform
end
make_modular
end
end