Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/spec_support/helpers.rb
4091 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module SpecSupport
23
module Helpers
24
def driver
25
GlobalTestEnv.driver_instance
26
end
27
28
def reset_driver!(...)
29
GlobalTestEnv.reset_driver!(...)
30
end
31
32
def quit_driver
33
GlobalTestEnv.quit_driver
34
end
35
36
def create_driver!(...)
37
GlobalTestEnv.create_driver!(...)
38
end
39
40
def url_for(filename)
41
GlobalTestEnv.url_for filename
42
end
43
44
def fix_windows_path(path)
45
return path unless WebDriver::Platform.windows?
46
47
if GlobalTestEnv.browser == :ie
48
path = path[%r{file://(.*)}, 1]
49
path = WebDriver::Platform.windows_path(path)
50
51
"file://#{path}"
52
else
53
path.sub(%r[file:/{0,2}], 'file:///')
54
end
55
end
56
57
def long_wait
58
@long_wait ||= Wait.new(timeout: 30)
59
60
return @long_wait unless block_given?
61
62
result = nil
63
@long_wait.until { result = yield }
64
result
65
end
66
67
def short_wait
68
@short_wait ||= Wait.new(timeout: 3)
69
70
return @short_wait unless block_given?
71
72
result = nil
73
@short_wait.until { result = yield }
74
result
75
end
76
77
def wait_for_alert
78
wait = Wait.new(timeout: 5, ignore: Error::NoSuchAlertError)
79
wait.until { driver.switch_to.alert }
80
end
81
82
def wait_for_no_alert
83
wait = Wait.new(timeout: 5, ignore: Error::UnexpectedAlertOpenError)
84
wait.until { driver.title }
85
end
86
87
def wait_for_element(locator, timeout = 25)
88
wait = Wait.new(timeout: timeout, ignore: Error::NoSuchElementError, message_provider: lambda {
89
url = "page url: #{driver.current_url};\n"
90
source = "page source: #{driver.find_element(css: 'body').attribute('innerHTML')}\n"
91
"could not find element #{locator} in #{timeout} seconds;\n#{url}#{source}"
92
})
93
wait.until { driver.find_element(locator) }
94
end
95
96
def wait_for_url(new_url, timeout = 15)
97
wait = Wait.new(timeout: timeout, message_provider: lambda {
98
"could not wait for URL #{new_url} in #{timeout} seconds;\nactual page url: #{driver.current_url};\n"
99
})
100
wait.until do
101
driver.current_url.include?(new_url)
102
end
103
end
104
105
def wait_for_devtools_target(target_type:)
106
wait = Wait.new(timeout: 3, ignore: Error::NoSuchTargetError)
107
wait.until { driver.devtools(target_type: target_type).target }
108
end
109
110
def wait_for_title(title:)
111
wait = Wait.new(timeout: 15)
112
wait.until { driver.title == title }
113
end
114
115
def open_file(file_name)
116
driver.navigate.to 'about:blank'
117
driver.navigate.to url_for('blank.html')
118
driver.navigate.to url_for(file_name)
119
wait_for_url(file_name)
120
end
121
122
def wait(timeout = 10)
123
Wait.new(timeout: timeout)
124
end
125
126
def png_size(path)
127
png = File.read(path, mode: 'rb')[0x10..0x18]
128
width = png.unpack1('NN')
129
height = png.unpack('NN').last
130
131
if Platform.mac? # Retina
132
width /= 2
133
height /= 2
134
end
135
136
[width, height]
137
end
138
139
def create_tempfile
140
Tempfile.new.tap do |file|
141
file.write('This is a dummy test file')
142
file.close
143
end
144
end
145
end # Helpers
146
end # SpecSupport
147
end # WebDriver
148
end # Selenium
149
150