selenium_webdriver_helper 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- 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 +143 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7349efe2787f5c5a2234be89e0a933b9250a879b8b93eb9dba2de373a3c8a1b6
|
4
|
+
data.tar.gz: 7ae2eaf291e8d58aa460b7ddc7099217c5a6fe5dddab0944b9624e0a27643c75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1e0a0a2b688e941bc9867b2aad62d0c5f88316cb891217ff91093a90cea2ce114b1fd54b4c16f5a470626c1bd437f49d54bfef7b0653de3413c1e7c787f545e
|
7
|
+
data.tar.gz: 77663ef3f73c5ed9ee489331aa94e28400ea051f84b7bc3b6a28e72503f2d61d95f07da3705d06addea6f69152f3b971dbf76ad65664c6e330f961e03628add3
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,8 +1,150 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
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.
|
24
|
+
|
25
|
+
require "selenium-webdriver"
|
3
26
|
require_relative "selenium_webdriver_helper/version"
|
4
27
|
|
28
|
+
# below is the code to make life easier as it already has selenium webdriver methods defined
|
5
29
|
module SeleniumWebdriverHelper
|
30
|
+
|
6
31
|
class Error < StandardError; end
|
7
|
-
|
32
|
+
|
33
|
+
attr_accessor :driver
|
34
|
+
|
35
|
+
def initialize(driver)
|
36
|
+
@driver = driver
|
37
|
+
end
|
38
|
+
|
39
|
+
def block_execution(retry_count = 3, &block)
|
40
|
+
block.call
|
41
|
+
rescue Selenium::WebDriver::Error => e
|
42
|
+
logger.info "#{e.message} \n #{e.backtrace}"
|
43
|
+
retry_count -= 1
|
44
|
+
retry if retry_count.positive?
|
45
|
+
end
|
46
|
+
|
47
|
+
def selectors_from_page_objects(page_object, value = nil)
|
48
|
+
output = []
|
49
|
+
if page_object.is_a?(Array)
|
50
|
+
output << page_object.first
|
51
|
+
output << page_object.last
|
52
|
+
elsif page_object.is_a?(Hash)
|
53
|
+
output = page_object.first
|
54
|
+
elsif value.nil?
|
55
|
+
raise "Locator cannot be nil - #{page_object} #{value}"
|
56
|
+
end
|
57
|
+
output
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_element(selector, custom_timeout: nil, driver2: nil)
|
61
|
+
how, what = selectors_from_page_objects(selector)
|
62
|
+
block_execution(3) do
|
63
|
+
wait = Selenium::WebDriver::Wait.new(timeout: (custom_timeout.nil? ? LONGER_WAIT : custom_timeout))
|
64
|
+
begin
|
65
|
+
wait.until { (driver2.nil? ? @driver : driver2).find_element(how, what).displayed? }
|
66
|
+
(driver2.nil? ? @driver : driver2).find_element(how, what)
|
67
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
68
|
+
raise "Exception #{e.message} \n #{e.backtrace}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_elements(selector, custom_timeout: nil, driver2: nil)
|
74
|
+
how, what = selectors_from_page_objects(selector)
|
75
|
+
block_execution(3) do
|
76
|
+
wait = Selenium::WebDriver::Wait.new(timeout: (custom_timeout.nil? ? LONGER_WAIT : custom_timeout))
|
77
|
+
begin
|
78
|
+
wait.until { (driver2.nil? ? @driver : driver2).find_elements(how, what)[0].displayed? }
|
79
|
+
(driver2.nil? ? @driver : driver2).find_elements(how, what)
|
80
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
81
|
+
raise "Exception #{e.message} \n #{e.backtrace}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def find_element_and_send_keys(selector, value)
|
87
|
+
block_execution(3) do
|
88
|
+
get_element(selector).send_keys(value)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def wait_for_page_load
|
93
|
+
wait = Selenium::WebDriver::Wait.new(LONG_WAIT)
|
94
|
+
wait.until { execute_script("return document.readyState;") == "complete" }
|
95
|
+
end
|
96
|
+
|
97
|
+
def visible_element(selector)
|
98
|
+
element_list = get_elements(selector, false)
|
99
|
+
element_list.each do |element|
|
100
|
+
return element if element.displayed?
|
101
|
+
end
|
102
|
+
raise "No visible element found for selector - #{selector}"
|
103
|
+
end
|
104
|
+
|
105
|
+
def wait_for_element_visibility(selector_or_element, visibility: true, timeout: LONG_WAIT)
|
106
|
+
wait = Selenium::WebDriver::Wait.new(timeout: timeout)
|
107
|
+
if !(selector_or_element.is_a?(Array) || selector_or_element.is_a?(Hash))
|
108
|
+
wait.until { selector_or_element.displayed? == visibility }
|
109
|
+
else
|
110
|
+
wait.until { element_displayed?(selector_or_element) == visibility }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def wait_for_element(selector, driver2: nil)
|
115
|
+
how, what = selectors_from_page_objects(selector)
|
116
|
+
5.times do
|
117
|
+
(driver2.nil? ? @driver : driver2).find_element(how, what)
|
118
|
+
break
|
119
|
+
rescue Selenium::WebDriver::Error::NoSuchElementError => e
|
120
|
+
logger.info "[Exception] Retrying to find element due to exception #{e.message}"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_child_element(parent_element, selector, custom_timeout: LONG_WAIT)
|
125
|
+
how, what = selectors_from_page_objects(selector)
|
126
|
+
block_execution(3) do
|
127
|
+
wait = Selenium::WebDriver::Wait.new(timeout: (custom_timeout.nil? ? LONGER_WAIT : custom_timeout))
|
128
|
+
wait.until { parent_element.find_element(how, what).displayed? }
|
129
|
+
parent_element.find_element(how, what)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def get_child_elements(parent_element, selector, custom_timeout: LONG_WAIT)
|
134
|
+
how, what = selectors_from_page_objects(selector)
|
135
|
+
block_execution(3) do
|
136
|
+
wait = Selenium::WebDriver::Wait.new(timeout: (custom_timeout.nil? ? LONGER_WAIT : custom_timeout))
|
137
|
+
wait.until { parent_element.find_elements(how, what)[0].displayed? }
|
138
|
+
parent_element.find_elements(how, what)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def page_refresh(driver2: nil)
|
143
|
+
(driver2.nil? ? @driver : driver2).navigate.refresh
|
144
|
+
end
|
145
|
+
|
146
|
+
def page_refresh_js(driver2: nil)
|
147
|
+
(driver2.nil? ? @driver : driver2).execute_script("location.reload(true);")
|
148
|
+
end
|
149
|
+
|
8
150
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- amit-singh-bisht
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- Rakefile
|
25
25
|
- lib/selenium_webdriver_helper.rb
|
26
|
+
- lib/selenium_webdriver_helper/constants.rb
|
26
27
|
- lib/selenium_webdriver_helper/version.rb
|
27
28
|
- sig/selenium_webdriver_helper.rbs
|
28
29
|
homepage: https://github.com/amit-singh-bisht/selenium_webdriver_helper_ruby
|