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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e08ab77d20d3af314a73856fcfacbb67f67c8631f1b92b950c953ee6de1426ff
4
- data.tar.gz: 24bf1affb19c70ef8165f58bec05dceb29fea32c1d4b42abf8a0685caefdc441
3
+ metadata.gz: d124968bcf54480e5dc80cc2e9c609b743e7777ba38e0638d28d264766ea2110
4
+ data.tar.gz: 3e2a846de538ccc648bc2128c2a966fe163aa86ee72829b745942192e8bf7a2c
5
5
  SHA512:
6
- metadata.gz: dfda801b0245d864ea1d974ecc15dea69c74a5e0df756e25fde1e6c2fa7f130b8c42ea9ddc9d65e28fc1d598f43c12f5cd13783e69c9a03f21675f0c8c82258f
7
- data.tar.gz: 9b2ebd792fa7f9b1c162146758a4773954a305d67763d21475a758e254f2a6cf90f0e380f0483a5386ecaf7e35b2378364c0965a63020d9173ee7f4bc0ccc8b5
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 "https://rubygems.org"
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 "rake", "~> 13.0"
9
-
10
- gem "rubocop", "~> 1.21"
8
+ gem 'logger'
9
+ gem 'rake', '~> 13.0'
10
+ gem 'rubocop', '~> 1.21'
11
+ gem 'selenium-webdriver'
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 amitsingh-bisht
3
+ Copyright (c) 2022 amit-singh-bisht
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,10 @@
1
+ SUPER_ULTRA_SHORT_WAIT = 0.5
2
+ ULTRA_SHORT_WAIT = 1
3
+ SHORTEST_WAIT = 2
4
+ SHORTER_WAIT = 5
5
+ SHORT_WAIT = 10
6
+ LONG_WAIT = 15
7
+ LONGER_WAIT = 30
8
+ LONGEST_WAIT = 45
9
+ ULTRA_LONG_WAIT = 60
10
+ SUPER_ULTRA_LONG_WAIT = 90
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SeleniumWebdriverHelper
4
- VERSION = "0.1.0"
4
+ VERSION = '0.3.0'
5
5
  end
@@ -1,8 +1,203 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "selenium_webdriver_helper/version"
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
- # Your code goes here...
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.1.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-15 00:00:00.000000000 Z
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
@@ -1,13 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 2.5
3
-
4
- Style/StringLiterals:
5
- Enabled: true
6
- EnforcedStyle: double_quotes
7
-
8
- Style/StringLiteralsInInterpolation:
9
- Enabled: true
10
- EnforcedStyle: double_quotes
11
-
12
- Layout/LineLength:
13
- Max: 120
@@ -1,4 +0,0 @@
1
- module SeleniumWebdriverHelper
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end