teber-library 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Teber
2
+
3
+ Teber Library gem is to have all the common methods that will be used in functional UI automation.
4
+
5
+ [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)
6
+ [![Made with Ruby](https://img.shields.io/badge/Made%20with-Ruby-red.svg)](https://www.ruby-lang.org/en/)
7
+ [![Published Gem](https://img.shields.io/badge/gem-teber-library-red.svg)](https://rubygems.org/gems/teber-library)
8
+ [![StackOverflow](http://img.shields.io/badge/Stack%20Overflow-Ask-blue.svg)]( https://stackoverflow.com/users/10505289/naresh-sekar )
9
+ [![Contributions Welcome](https://img.shields.io/badge/Contributions-Welcome-brightgreen.svg)](CONTRIBUTING.md)
10
+ [![email me](https://img.shields.io/badge/Contact-Email-green.svg)](nareshnavinash@gmail.com)
11
+
12
+
13
+ ![alt text](lib/teber/Teber-Library-Gem.png)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'teber-library'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install teber-library
30
+
31
+ ## Usage
32
+
33
+ This gem is to replace the library methods in [Teber-Ruby](https://github.com/nareshnavinash/Teber-Ruby) framework. This allows us to share the methods among different teams and completely ignore the repetitive work. For more details on Page object model for functional UI automation verify [Teber Ruby Documentation](https://nareshnavinash.github.io/Teber-Ruby/) page.
34
+
35
+ ### Adding new methods
36
+
37
+ Add all the new methods inside /lib/teber/ path with the module name as 'Teber'. If added a new file, include the newly added file to the /lib/teber/teber.rb so that those methods will be available when we use the gem.
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nareshnavinash/Teber-Gem/. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
48
+
49
+ ## Authors
50
+
51
+ * **[Naresh Sekar](https://github.com/nareshnavinash)**
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [GPL-3.0 License](https://opensource.org/licenses/GPL-3.0).
56
+
57
+ ## Code of Conduct
58
+
59
+ Everyone interacting in the Teber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nareshnavinash/Teber-Gem/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "teber"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/teber.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "teber/version"
2
+ require "teber/driver"
3
+ require "teber/wait"
4
+ require "teber/locator"
5
+
6
+ module Teber
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
Binary file
@@ -0,0 +1,301 @@
1
+ module Teber
2
+ class Driver
3
+ attr_accessor :driver
4
+ $focus_driver = nil
5
+ @driver = nil
6
+ @main_window = nil
7
+ @click_exception_count = nil
8
+ @@drivers = []
9
+ @@drivers_with_names = {}
10
+
11
+ def initialize(driver_name = "Driver", browser = $conf["browser"])
12
+ begin
13
+ start(driver_name,browser)
14
+ puts "#{driver_name} is initialized"
15
+ rescue Exception => e
16
+ puts "#{driver_name} is failed to initialize \n\n #{e.backtrace}\n\nRetrying to initialize #{driver_name}"
17
+ start(driver_name,browser)
18
+ puts "#{driver_name} is initialized after an exception"
19
+ end
20
+ end
21
+
22
+ ##############################
23
+ # Custom methods of driver #
24
+ ##############################
25
+
26
+ def start(driver_name, browser)
27
+
28
+ case browser
29
+
30
+ when 'chrome'
31
+ options = Selenium::WebDriver::Chrome::Options.new
32
+ if ENV['MODE'] == "headless" or $conf["mode"] == "headless"
33
+ switches = ["disable-infobars", "disable-gpu", "disable-dev-shm-usage", "no-sandbox", "headless"]
34
+ else
35
+ switches = ["disable-infobars", "disable-gpu", "disable-dev-shm-usage", "no-sandbox"]
36
+ end
37
+ switches.map { |k| options.add_argument(k) }
38
+ @driver = Selenium::WebDriver.for(:chrome, options: options)
39
+ @driver.manage.timeouts.implicit_wait = $conf["implicit_wait"]
40
+
41
+ when 'firefox', 'ff'
42
+ # to be added
43
+
44
+ when 'ie', 'internet_explorer'
45
+ # to be added
46
+
47
+ when 'edge'
48
+ # to be added
49
+
50
+ when 'safari'
51
+ # to be added
52
+
53
+ else
54
+ raise ArgumentError, "Specify a proper browser while initiating a driver \n \n#{browser.inspect}"
55
+ end
56
+
57
+ target_size = Selenium::WebDriver::Dimension.new(1200, 700)
58
+ @driver.manage.window.size = target_size
59
+ @click_exception_count=0
60
+ @@drivers.push(self)
61
+ @@drivers_with_names[self] = "#{driver_name}"
62
+ $focus_driver = self
63
+ puts "#{driver_name} - #{self}"
64
+ return self
65
+ end
66
+
67
+ def add_to_allure_env(params)
68
+ location = File.join("#{Pathname.pwd}","reports/allure/environment.properties")
69
+ file = File.open(location,"a")
70
+ file.puts "#{params}\n"
71
+ end
72
+
73
+ def self.direct_add_to_allure_env(params)
74
+ location = File.join("#{Pathname.pwd}","reports/allure/environment.properties")
75
+ file = File.open(location,"a")
76
+ file.puts "#{params}\n"
77
+ end
78
+
79
+ def get(url)
80
+ $focus_driver = self
81
+ @driver.get(url)
82
+ add_to_allure_env("URL = #{url}")
83
+ puts "#{$focus_driver} loaded with - #{url}"
84
+ end
85
+
86
+ def refresh
87
+ $focus_driver = self
88
+ navigate.refresh
89
+ puts "#{$focus_driver} is refreshed"
90
+ end
91
+
92
+ def find_element(locator)
93
+ $focus_driver = self
94
+ TestBdd::Wait.wait_for_element(locator)
95
+ return @driver.find_element(locator.how,locator.what)
96
+ end
97
+
98
+ def find_elements(locator)
99
+ $focus_driver = self
100
+ return @driver.find_elements(locator.how,locator.what)
101
+ end
102
+
103
+ def mouse_over(locator,index=1)
104
+ $focus_driver = self
105
+ element=find_elements(locator)[index-1]
106
+ @driver.action.move_to(element).perform
107
+ puts "mouse over for the element - #{locator.how} => #{locator.what} is done"
108
+ end
109
+
110
+ def mouse
111
+ $focus_driver = self
112
+ return @driver.mouse
113
+ end
114
+
115
+ def action
116
+ $focus_driver = self
117
+ return @driver.action
118
+ end
119
+
120
+ def move_and_click(locator)
121
+ $focus_driver = self
122
+ ele=find_element(locator)
123
+ @driver.action.move_to(ele).click.perform
124
+ puts "Mouse over the locator and then click for - #{locator.how} => #{locator.what} is done"
125
+ end
126
+
127
+ def browser
128
+ $focus_driver = self
129
+ @driver.browser
130
+ end
131
+
132
+ def capabilities
133
+ $focus_driver = self
134
+ @driver.capabilities
135
+ end
136
+
137
+ def current_url
138
+ $focus_driver = self
139
+ @driver.current_url
140
+ end
141
+
142
+ def execute_async_script(script, *args)
143
+ $focus_driver = self
144
+ @driver.execute_async_script(script, *args)
145
+ end
146
+
147
+ def execute_script(script, *args)
148
+ $focus_driver = self
149
+ @driver.execute_script(script, *args)
150
+ end
151
+
152
+ def inspect
153
+ $focus_driver = self
154
+ @driver.inspect
155
+ end
156
+
157
+ def manage
158
+ $focus_driver = self
159
+ @driver.manage
160
+ end
161
+
162
+ def navigate
163
+ $focus_driver = self
164
+ @driver.navigate
165
+ end
166
+
167
+ def page_source
168
+ $focus_driver = self
169
+ @driver.page_source
170
+ end
171
+
172
+ def body_text
173
+ $focus_driver = self
174
+ @driver.find_element(:css, 'body').text
175
+ end
176
+
177
+ def save_screenshot(file_name = nil)
178
+ $focus_driver = self
179
+ file_name = "#{Pathname.pwd}/#{$conf['screenshot_location']}/#{Time.new.strftime("%Y-%m-%d-%H-%M-%S-%L-%N")}.png" if file_name.nil?
180
+ puts "#{$focus_driver}'s Screenshot saved in this path => #{file_name}"
181
+ @driver.save_screenshot(file_name)
182
+ end
183
+
184
+ def switch_to_frame(locator)
185
+ $focus_driver = self
186
+ @main_window=@driver.window_handle
187
+ @driver.switch_to.frame(find_element(locator))
188
+ puts "Switched to iframe - #{locator.how} => #{locator.what} on #{$focus_driver}"
189
+ return @main_window
190
+ end
191
+
192
+ def switch_to_window(locator=nil)
193
+ $focus_driver = self
194
+ @main_window=@driver.window_handle
195
+ locator.click if locator != nil
196
+ windows=@driver.window_handles
197
+ new_window=nil;
198
+ windows.length.times do |i|
199
+ if windows[i] != @main_window
200
+ new_window=windows[i]
201
+ end
202
+ end
203
+ @driver.switch_to.window(new_window)
204
+ puts "Switched to new window on #{$focus_driver}"
205
+ return @main_window
206
+ end
207
+
208
+ def scroll_to_locator(locator)
209
+ $focus_driver = self
210
+ element = find_element(locator)
211
+ @driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center', inline: 'nearest'});",element)
212
+ puts "Scroll to this locator - #{locator.how} => #{locator.what} on #{$focus_driver}"
213
+ sleep 1
214
+ end
215
+
216
+ def revert_to(window=nil)
217
+ $focus_driver = self
218
+ if window != nil
219
+ @driver.switch_to.window(window)
220
+ puts "Switched back to another window - #{window} in #{$focus_driver}"
221
+ else
222
+ @driver.switch_to.window(@main_window)
223
+ puts "Switched back to main window in #{focus_driver}"
224
+ end
225
+ end
226
+
227
+ def close
228
+ $focus_driver = self
229
+ @driver.close
230
+ puts "Closed the browser - #{$focus_driver}"
231
+ end
232
+
233
+ def quit
234
+ @driver.quit
235
+ @@drivers.delete(self)
236
+ $focus_driver = @@drivers[0]
237
+ puts "Quit the browser - #{$focus_driver}"
238
+ end
239
+
240
+ def quit_all
241
+ @@drivers.each do |driver|
242
+ driver.quit if driver != self
243
+ end
244
+ self.quit
245
+ puts "deleted all the browsers"
246
+ end
247
+
248
+ def self.quit_all_drivers
249
+ @@drivers.each do |driver|
250
+ driver.quit if driver != self
251
+ end
252
+ puts "deleted all the browsers"
253
+ end
254
+
255
+ def self.get_all_drivers
256
+ return @@drivers_with_names
257
+ end
258
+
259
+ def self.get_current_driver
260
+ return $focus_driver
261
+ end
262
+
263
+ def alert(ok_cancel)
264
+ sleep 2
265
+ alert = @driver.switch_to.alert
266
+ alertMsg=alert.text
267
+ if ok_cancel
268
+ alert.accept
269
+ puts "The alert was accepted in #{$focus_driver} with alert message #{alertMsg}"
270
+ else
271
+ alert.dismiss
272
+ puts "The alert was dismissed in #{$focus_driver} with alert message #{alertMsg}"
273
+ end
274
+ return alertMsg
275
+ end
276
+
277
+ def is_alert_present?
278
+ begin
279
+ alert = @driver.switch_to.alert
280
+ alertMsg=alert.text
281
+ return true
282
+ rescue Exception => e
283
+ return false
284
+ end
285
+ end
286
+
287
+ def self.switch_to(driver)
288
+ $focus_driver = driver
289
+ end
290
+
291
+ def drag_and_drop(source_locator, target_locator)
292
+ source = find_element(source_locator)
293
+ target = find_element(target_locator)
294
+ @driver.action.click_and_hold(source).perform
295
+ @driver.action.move_to(target).release.perform
296
+ sleep 3
297
+ puts "In driver #{$focus_driver} - #{source_locator.how} => source_locator.what locator was dragged and moved to this locator #{target_locator.how} => #{target_locator.what}"
298
+ end
299
+
300
+ end
301
+ end
@@ -0,0 +1,200 @@
1
+ module Teber
2
+ class Locator
3
+ attr_accessor :how
4
+ attr_accessor :what
5
+ attr_accessor :options
6
+
7
+ def initialize(
8
+ how,
9
+ what,
10
+ options = {
11
+ "hidden" => false,
12
+ "ajax_load" => false,
13
+ })
14
+ @how = how
15
+ @what = what
16
+ @options = options
17
+ end
18
+
19
+ def self.mouse_over(element, driver = $focus_driver)
20
+ driver.action.move_to(element).perform
21
+ end
22
+
23
+ def click(driver = $focus_driver)
24
+ begin
25
+ driver.find_element(self).click
26
+ puts "Clicked - #{self.how} => #{self.what}"
27
+ rescue Exception => e
28
+ puts "Not clicked at - #{self.how} => #{self.what}"
29
+ puts e.message
30
+ end
31
+ end
32
+
33
+ def text(driver = $focus_driver)
34
+ return driver.find_element(self).text
35
+ end
36
+
37
+ def texts(driver = $focus_driver)
38
+ elements_text = []
39
+ driver.find_elements(self).each do |element|
40
+ elements_text.push(element.text)
41
+ end
42
+ return elements_text
43
+ end
44
+
45
+ def attribute(name, driver = $focus_driver)
46
+ driver.find_element(self).attribute(name)
47
+ end
48
+
49
+ def css_value(prop, driver = $focus_driver)
50
+ driver.find_element(self).css_value(prop)
51
+ end
52
+
53
+ def displayed?(driver = $focus_driver)
54
+ driver.find_element(self).displayed?
55
+ end
56
+
57
+ def enabled?(driver = $focus_driver)
58
+ driver.find_element(self).enabled?
59
+ end
60
+
61
+ def is_enabled_with_wait?(timeout = $conf["implicit_wait"], driver = $focus_driver)
62
+ index = 0
63
+ while driver.find_element(self).enabled? == false
64
+ sleep 1
65
+ break if index == timeout
66
+ index += 1
67
+ end
68
+ end
69
+
70
+ def hash(driver = $focus_driver)
71
+ driver.find_element(self).hash
72
+ end
73
+
74
+ def location(driver = $focus_driver)
75
+ driver.find_element(self).location
76
+ end
77
+
78
+ def location_once_scrolled_into_view(driver = $focus_driver)
79
+ driver.find_element(self).location_once_scrolled_into_view
80
+ end
81
+
82
+ def property(driver = $focus_driver)
83
+ driver.find_element(self).property
84
+ end
85
+
86
+ def selected?(driver = $focus_driver)
87
+ driver.find_element(self).selected?
88
+ end
89
+
90
+ def size(driver = $focus_driver)
91
+ driver.find_element(self).size
92
+ end
93
+
94
+ def style(prop, driver = $focus_driver)
95
+ driver.find_element(self).style(prop)
96
+ end
97
+
98
+ def submit(driver = $focus_driver)
99
+ driver.find_element(self).submit
100
+ end
101
+
102
+ def count(driver = $focus_driver)
103
+ element_list = driver.find_elements(self)
104
+ return element_list.count
105
+ end
106
+
107
+ def tag_name(driver = $focus_driver)
108
+ driver.find_element(self).tag_name
109
+ end
110
+
111
+ def is_present?(driver = $focus_driver)
112
+ driver.driver.manage.timeouts.implicit_wait = 0
113
+ begin
114
+ return driver.driver.find_element(self.how, self.what).displayed?
115
+ rescue Exception => e
116
+ driver.driver.manage.timeouts.implicit_wait = $conf["implicit_wait"]
117
+ return false
118
+ ensure
119
+ driver.driver.manage.timeouts.implicit_wait = $conf["implicit_wait"]
120
+ end
121
+ end
122
+
123
+ def is_not_present?(driver = $focus_driver)
124
+ return !is_present?(driver)
125
+ end
126
+
127
+ def is_present_with_wait?(timeout = $conf["implicit_wait"], driver = $focus_driver)
128
+ Wait.wait_for_element(self, timeout, driver)
129
+ is_present?(driver)
130
+ end
131
+
132
+ def is_not_present_with_wait?(timeout = $conf["implicit_wait"], driver = $focus_driver)
133
+ Wait.wait_for_element_hide(self, timeout, driver)
134
+ return !is_present?(driver)
135
+ end
136
+
137
+ def click_if_present(driver = $focus_driver)
138
+ click(driver) if is_present?(driver)
139
+ end
140
+
141
+ def click_if_present_with_wait(timeout = $conf["implicit_wait"], driver = $focus_driver)
142
+ click(driver) if is_present_with_wait?(timeout, driver)
143
+ end
144
+
145
+ def to_s
146
+ return "How ===> #{@how}\nWhat ===> #{@what}\nOptions ===> #{@options}"
147
+ end
148
+
149
+ def mouse_over(index = 1, driver = $focus_driver)
150
+ element = driver.find_elements(self)[index - 1]
151
+ driver.action.move_to(element).perform
152
+ end
153
+
154
+ def move_and_click(driver = $focus_driver)
155
+ element = driver.find_element(self)
156
+ driver.action.move_to(element).click.perform
157
+ end
158
+
159
+ def get_element(driver = $focus_driver)
160
+ driver.find_element(self)
161
+ end
162
+
163
+ ################################
164
+ # Check box methods
165
+ ################################
166
+
167
+ def is_checked?(driver = $focus_driver)
168
+ self.attribute("checked") == "true"
169
+ end
170
+
171
+ def check(driver = $focus_driver)
172
+ self.click unless self.is_checked?
173
+ end
174
+
175
+ def uncheck(driver = $focus_driver)
176
+ self.click if self.is_checked?
177
+ end
178
+
179
+ ##############################
180
+ # Text box methods
181
+ ##############################
182
+
183
+ def clear(driver = $focus_driver)
184
+ driver.find_element(self).clear
185
+ end
186
+
187
+ def send_keys(*args)
188
+ $focus_driver.find_element(self).send_keys(*args)
189
+ end
190
+
191
+ def clear_and_send_keys(*args)
192
+ clear($focus_driver)
193
+ send_keys(*args)
194
+ end
195
+
196
+ def get_value(driver = $focus_driver)
197
+ driver.find_element(self).attribute("value")
198
+ end
199
+ end
200
+ end