-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rb
223 lines (165 loc) · 5.6 KB
/
test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'selenium-webdriver'
require 'pry'
class Selenium::WebDriver::Element
def children
self.find_elements(:xpath, "./*")
end
end
def a_single_tour
# Driver setup
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://spredfast.com/platform/intelligence/?env=kim-test"
# SSO login
elements = driver.find_elements(:class, "btn-primary")
sso_login = elements[0]
sso_login.click()
email = driver.find_element(:name, 'email')
email.send_keys '[email protected]'
password = driver.find_element(:name, 'password')
password.send_keys 'hackweek'
submit = driver.find_element(:class, 'btn-primary')
submit.click()
# Now logged in
sleep(5)
# Skip tutorial
hide_tutorial = driver.find_elements(:class, 'exit')
hide_tutorial[0].click()
# Enter the search bar
search_input = driver.find_element(:css => ".searchBarInput input")
hello_array = ['Ready?', 'Export to cloud', 'Hello', 'Hola', '您好', 'こんにちは']
count = 0
hello_array.each do |hello_string|
search_input_array = hello_string.split('')
char_count = 0
search_input_array.each { |c|
sleep(0.1)
search_input.send_keys c
# buffer time between phrases
next if char_count != search_input_array.length - 1
sleep(0.2)
}.each { |c|
sleep(0.1)
# search for last phrase
next if count == hello_array.length - 1
search_input.send_keys("\b")
}
count += 1
end
# Search the last keyword in array
search_input.send_keys(:enter)
sleep(3)
# Make sure media content loaded
search_input.send_keys(:arrow_down)
search_input.send_keys(:arrow_down)
# Click image for prank
sleep(1)
grab_firs_media_element = Proc.new {driver.find_elements(:css => '.media-0')}
first_media_elements = retry_method(&grab_firs_media_element)
first_media_element = first_media_elements[0]
first_media_element.click()
# Wait for modal to populate
sleep(3)
media_modal = driver.find_element(:css => '.modal-content')
media_modal.click()
# Close modal
sleep(2)
cancel_modal = driver.find_element(:css => '.close')
cancel_modal.click()
# Export to png
# Click the export modal
sleep(2)
export_modal = driver.find_element(:css => '.export-dropdown')
export_modal.click()
# Click Export as PNG
sleep(2)
export_as_png = driver.find_elements(:css => '.export')[2]
export_as_png.click()
# Choose comprehensive view
sleep(2)
export_png_as_comprehensive = driver.find_elements(:xpath, "//*[contains(text(), 'Comprehensive View')]")
export_png_as_comprehensive[0].click()
sleep(0.5)
# Export now and wait for cloud modal
export_png_now = driver.find_element(:css => '.modal-footer .btn')
export_png_now.click()
# Wait until it finish processing then cloud modal will pop out
grab_filepicker_modal = Proc.new { driver.find_elements(:xpath, "//iframe[contains(@id, 'filepicker_dialog')]") }
filepicker_modal = retry_method(&grab_filepicker_modal)
filepicker_modal = filepicker_modal[0]
filepicker_modal.click() # sanity check iframe exists
# Pause for intro a lil bit on cloud
sleep(5)
# Switch to iframe and click Box Cloud
driver.switch_to.frame(filepicker_modal)
driver.find_elements(:css => ".sbicon-box")[0].click()
connect_to_box = driver.find_elements(:css => "a").select { |a| a.text == "Connect to Box" }[0]
connect_to_box.click()
# We all know that open new tab take time
sleep(4)
# Switch to box tab to login
switch_tab(driver, 1)
grab_box_login = Proc.new { driver.find_elements(:css => ".login_email") }
box_login = retry_method(&grab_box_login)
box_login = box_login[0]
box_login.send_keys("[email protected]")
sleep(3)
grab_box_password = Proc.new { driver.find_elements(:css => ".login_password") }
box_password = retry_method(&grab_box_password)
box_password = box_password[0]
box_password.send_keys("abc"+"123"+"456")
sleep(8)
box_authorize = driver.find_element(:css => ".login_submit")
box_authorize.click()
# Grant box access
grab_grant_access_button = Proc.new { driver.find_elements(:id => "consent_accept_button") }
grant_access_button = retry_method(&grab_grant_access_button)
grant_access_button = grant_access_button[0]
# Before grant, box cloud prevent bot that click so fast
# Switch back to intell tab after click
sleep(3)
grant_access_button.click()
sleep(0.2)
switch_tab(driver, 0)
sleep(3)
# Back to intell tab but always have to go back iframe
driver.switch_to.frame(filepicker_modal)
# Save the file now. Your file is persistent on cloud
grab_save_your_file = Proc.new { driver.find_elements(:css => ".btn--primary") }
save_your_file = retry_method(&grab_save_your_file)
save_your_file = save_your_file[0]
save_your_file.click()
# binding.pry
sleep(20)
driver.close()
end
# Retry till the element exist
def retry_method(&block)
ele_arr = nil
while true do
ele_arr = block.call
break if ele_arr.length > 0
sleep(1)
puts 'take a sip of cold coffee >>>'
end
ele_arr
end
# Switch tab
def switch_tab(driver, which_num_tab)
window_title = driver.window_handles
driver.switch_to.window(window_title[which_num_tab])
puts window_title
end
# Main Driver Method
a_single_tour()
# End of driver
# 1. Useful iframe handling
# driver.switch_to.default_content()
# driver.switch_to.frame(0) # can feed in id, object
# 2. Switch tab
# wnd_titl = driver.window_handles.map do |w|
# driver.switch_to.window(w)
# [w,driver.title]
# end
# #required window
# win_id = wnd_titl.find { |e1,e2| e2 == 'My TITLE' }.first
# driver.switch_to.window(win_id) #switched to the required window