selenium_webdriver_helper 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +5 -4
- data/LICENSE.txt +1 -1
- data/lib/selenium_webdriver_helper/constants.rb +10 -0
- data/lib/selenium_webdriver_helper/version.rb +1 -1
- data/lib/selenium_webdriver_helper.rb +197 -2
- metadata +3 -4
- data/.rubocop.yml +0 -13
- data/sig/selenium_webdriver_helper.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d124968bcf54480e5dc80cc2e9c609b743e7777ba38e0638d28d264766ea2110
|
4
|
+
data.tar.gz: 3e2a846de538ccc648bc2128c2a966fe163aa86ee72829b745942192e8bf7a2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60e09a27671c024c9c27fee727d3caddfaba6496247041017ae34d1066c122c33cec38b4732b079aba6207c928f341f1a8cf662924abdeb2d14722498cfa3e0d
|
7
|
+
data.tar.gz: dd62e3d4632a50fb3f17d2587da02d1c42c911f82b0490de9ec7501aab1ab5af325b19e666a161286a4267551caecaea6a85813e0f70631ade259f17de055f27
|
data/Gemfile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in selenium_webdriver_helper.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem
|
9
|
-
|
10
|
-
gem
|
8
|
+
gem 'logger'
|
9
|
+
gem 'rake', '~> 13.0'
|
10
|
+
gem 'rubocop', '~> 1.21'
|
11
|
+
gem 'selenium-webdriver'
|
data/LICENSE.txt
CHANGED
@@ -1,8 +1,203 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2022 amit-singh-bisht
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in
|
15
|
+
# all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
# THE SOFTWARE.
|
4
24
|
|
25
|
+
require 'selenium-webdriver'
|
26
|
+
require 'logger'
|
27
|
+
require_relative 'selenium_webdriver_helper/version'
|
28
|
+
require_relative 'selenium_webdriver_helper/constants'
|
29
|
+
|
30
|
+
# below is the code to make life easier as it already has selenium webdriver methods defined
|
5
31
|
module SeleniumWebdriverHelper
|
32
|
+
|
6
33
|
class Error < StandardError; end
|
7
|
-
|
34
|
+
|
35
|
+
attr_accessor :driver
|
36
|
+
|
37
|
+
def log_info(message)
|
38
|
+
logger = Logger.new('selenium.log')
|
39
|
+
logger.info(message)
|
40
|
+
end
|
41
|
+
|
42
|
+
def log_debug(message)
|
43
|
+
logger = Logger.new('selenium.log')
|
44
|
+
logger.debug(message)
|
45
|
+
end
|
46
|
+
|
47
|
+
def log_error(message)
|
48
|
+
logger = Logger.new('selenium.log')
|
49
|
+
logger.error(message)
|
50
|
+
end
|
51
|
+
|
52
|
+
def selectors_from_page_objects(page_object, value = nil)
|
53
|
+
output = []
|
54
|
+
if page_object.is_a?(Array)
|
55
|
+
output << page_object.first
|
56
|
+
output << page_object.last
|
57
|
+
elsif page_object.is_a?(Hash)
|
58
|
+
output = page_object.first
|
59
|
+
elsif value.nil?
|
60
|
+
raise "Locator cannot be nil - #{page_object} #{value}"
|
61
|
+
end
|
62
|
+
output
|
63
|
+
end
|
64
|
+
|
65
|
+
def block_execution(retry_count = 3, &block)
|
66
|
+
block.call
|
67
|
+
rescue Selenium::WebDriver::Error => e
|
68
|
+
logger.info "#{e.message} \n #{e.backtrace}"
|
69
|
+
retry_count -= 1
|
70
|
+
retry if retry_count.positive?
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize_driver(driver, implicit_wait = LONGER_WAIT)
|
74
|
+
$driver = driver
|
75
|
+
$driver.manage.timeouts.implicit_wait = implicit_wait
|
76
|
+
$wait = Selenium::WebDriver::Wait.new(timeout: implicit_wait) # seconds
|
77
|
+
end
|
78
|
+
|
79
|
+
def execute_script(js, *args)
|
80
|
+
$driver.execute_script(js, *args)
|
81
|
+
rescue Selenium::WebDriver::Error::UnsupportedOperationError => e
|
82
|
+
log_error("[Exception] underlying webdriver instance does not support javascript #{e.message}")
|
83
|
+
end
|
84
|
+
|
85
|
+
def wait_for_page_load(custom_timeout = SHORTER_WAIT)
|
86
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
87
|
+
wait.until { execute_script('return document.readyState;') == 'complete' }
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_url(url, driver = $driver)
|
91
|
+
driver.get(url)
|
92
|
+
wait_for_page_load
|
93
|
+
log_info("visited #{url}")
|
94
|
+
end
|
95
|
+
|
96
|
+
def maximize_window(driver = $driver)
|
97
|
+
driver.manage.window.maximize
|
98
|
+
log_info('window maximized')
|
99
|
+
end
|
100
|
+
|
101
|
+
def save_screenshot(path, driver = $driver)
|
102
|
+
driver.save_screenshot(path)
|
103
|
+
log_info("screenshot captured and saved at path #{path}")
|
104
|
+
end
|
105
|
+
|
106
|
+
def get_element(selector, custom_timeout = LONGER_WAIT, driver = $driver)
|
107
|
+
how, what = selectors_from_page_objects(selector)
|
108
|
+
block_execution(3) do
|
109
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
110
|
+
begin
|
111
|
+
wait.until { driver.find_element(how, what).displayed? }
|
112
|
+
driver.find_element(how, what)
|
113
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
114
|
+
raise "Exception #{e.message} \n #{e.backtrace}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def get_elements(selector, custom_timeout: LONGER_WAIT, driver: $driver)
|
120
|
+
how, what = selectors_from_page_objects(selector)
|
121
|
+
block_execution(3) do
|
122
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
123
|
+
begin
|
124
|
+
wait.until { driver.find_elements(how, what)[0].displayed? }
|
125
|
+
driver.find_elements(how, what)
|
126
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
127
|
+
raise "Exception #{e.message} \n #{e.backtrace}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def element_text(selector, custom_timeout = LONGER_WAIT, driver = $driver)
|
133
|
+
block_execution(3) do
|
134
|
+
get_element(selector, custom_timeout, driver).text
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def find_element_and_send_keys(selector, value, driver = $driver)
|
139
|
+
block_execution(3) do
|
140
|
+
get_element(selector).send_keys(value)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def element_click(selector, custom_timeout = LONGER_WAIT, driver = $driver)
|
145
|
+
block_execution(3) do
|
146
|
+
get_element(selector, custom_timeout, driver).click
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def visible_element(selector)
|
151
|
+
element_list = get_elements(selector, false)
|
152
|
+
element_list.each do |element|
|
153
|
+
return element if element.displayed?
|
154
|
+
end
|
155
|
+
raise "No visible element found for selector - #{selector}"
|
156
|
+
end
|
157
|
+
|
158
|
+
def wait_for_element_visibility(selector_or_element, visibility = true, custom_timeout = LONG_WAIT)
|
159
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
160
|
+
if !(selector_or_element.is_a?(Array) || selector_or_element.is_a?(Hash))
|
161
|
+
wait.until { selector_or_element.displayed? == visibility }
|
162
|
+
else
|
163
|
+
wait.until { element_displayed?(selector_or_element) == visibility }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def wait_for_element(selector, driver = $driver)
|
168
|
+
how, what = selectors_from_page_objects(selector)
|
169
|
+
5.times do
|
170
|
+
driver.find_element(how, what)
|
171
|
+
break
|
172
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
173
|
+
logger.info "[Exception] Retrying to find element due to exception #{e.message}"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def get_child_element(parent_element, selector, custom_timeout = LONG_WAIT)
|
178
|
+
how, what = selectors_from_page_objects(selector)
|
179
|
+
block_execution(3) do
|
180
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
181
|
+
wait.until { parent_element.find_element(how, what).displayed? }
|
182
|
+
parent_element.find_element(how, what)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def get_child_elements(parent_element, selector, custom_timeout = LONG_WAIT)
|
187
|
+
how, what = selectors_from_page_objects(selector)
|
188
|
+
block_execution(3) do
|
189
|
+
wait = Selenium::WebDriver::Wait.new(timeout: custom_timeout)
|
190
|
+
wait.until { parent_element.find_elements(how, what)[0].displayed? }
|
191
|
+
parent_element.find_elements(how, what)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def page_refresh(driver = $driver)
|
196
|
+
driver.navigate.refresh
|
197
|
+
end
|
198
|
+
|
199
|
+
def page_refresh_js
|
200
|
+
execute_script('location.reload(true);')
|
201
|
+
end
|
202
|
+
|
8
203
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selenium_webdriver_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- amit-singh-bisht
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Write a longer description or delete this line.
|
14
14
|
email:
|
@@ -17,14 +17,13 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- ".rubocop.yml"
|
21
20
|
- Gemfile
|
22
21
|
- LICENSE.txt
|
23
22
|
- README.md
|
24
23
|
- Rakefile
|
25
24
|
- lib/selenium_webdriver_helper.rb
|
25
|
+
- lib/selenium_webdriver_helper/constants.rb
|
26
26
|
- lib/selenium_webdriver_helper/version.rb
|
27
|
-
- sig/selenium_webdriver_helper.rbs
|
28
27
|
homepage: https://github.com/amit-singh-bisht/selenium_webdriver_helper_ruby
|
29
28
|
licenses:
|
30
29
|
- MIT
|
data/.rubocop.yml
DELETED