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
1865 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)
88
wait = Wait.new(timeout: 25, ignore: Error::NoSuchElementError)
89
wait.until { driver.find_element(locator) }
90
end
91
92
def wait_for_new_url(old_url)
93
wait = Wait.new(timeout: 5)
94
wait.until do
95
url = driver.current_url
96
!(url.empty? || url.include?(old_url))
97
end
98
end
99
100
def wait(timeout = 10)
101
Wait.new(timeout: timeout)
102
end
103
104
def png_size(path)
105
png = File.read(path, mode: 'rb')[0x10..0x18]
106
width = png.unpack1('NN')
107
height = png.unpack('NN').last
108
109
if Platform.mac? # Retina
110
width /= 2
111
height /= 2
112
end
113
114
[width, height]
115
end
116
117
def create_tempfile
118
Tempfile.new.tap do |file|
119
file.write('This is a dummy test file')
120
file.close
121
end
122
end
123
end # Helpers
124
end # SpecSupport
125
end # WebDriver
126
end # Selenium
127
128