watir 7.0.0 → 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aaa0e3a2614b0cdae93ebbae36c1f1c4ae58f55fd7d79a1b333144969354346f
4
- data.tar.gz: 349abd59394dcd77d9f4aaf2050ab75dd613872c7aff3de96f8acc13cd797213
3
+ metadata.gz: e8a3f59dcd9f56e63cf532cc2ba8c9605e8ffad78bd59d3f104e6677c9f5e20c
4
+ data.tar.gz: 1f1cb55a4270ebdce957f4a8dc33e76403eef391ed59e3179f4f6772595d3f59
5
5
  SHA512:
6
- metadata.gz: 2abac0e5c8e81acf27b5edefd507c9056bca728f2ccab2aa858176287cf7e544a6961ab7bcc0edf7a01d78735038fedf77cbb70798aacdd490be4fc987f88602
7
- data.tar.gz: a32076a9af54abfbcc470bb96095aa1441f812640029fa492233f7d7cbc5f9fb327de0397417103519cf674e7c03a91de85b749bf87e6cc8483cbf16bcefa3e7
6
+ metadata.gz: 488c6b2f0a93a20390350da8b95025f9e33d2be9bb7bd84b130808906bca067860b3db6908c59961ef3914ccbb682d2d15d2418656749f8916a1416fa8c6cbb5
7
+ data.tar.gz: 64bc4a7b36c294a75ceb86c3fea21809dbefc158cbca08ba72dc6bf7b3b64d76f04b5c02130a8cce15e78d5156659cb317ee397e27d93d06e537e804695e425b
data/CHANGES.md CHANGED
@@ -1,3 +1,12 @@
1
+ ### 7.1.0 (2021-11-15)
2
+
3
+ * Add automatic scroll to `Element#click` with method parameters to ensure element is in viewport
4
+ * Provide `:scroll_to` parameter for element methods implemented with `Actions` class (thanks osadasami #948, #949)
5
+ * Allow `:scroll_to` parameter to accept `nil` argument to avoid automatic scrolling
6
+ * Implement `Element#in_viewport?`
7
+ * Fix but where `Scroll#to` for `Browser` with `:center` argument did not scroll to center of page
8
+ * Change `Element#obscured?` to only scroll when element out of viewport and then scroll to bottom instead of top
9
+
1
10
  ### 7.0.0 (2021-10-18)
2
11
 
3
12
  * Requires Selenium 4.0.0+
@@ -146,9 +146,10 @@ module Watir
146
146
  # @param [:shift, :alt, :control, :command, :meta] modifiers to press while clicking.
147
147
  #
148
148
 
149
- def click(*modifiers)
149
+ def click(*modifiers, scroll_to: :center)
150
150
  element_call(:wait_for_enabled) do
151
151
  if modifiers.any?
152
+ scroll.to(scroll_to) if scroll_to
152
153
  action = driver.action
153
154
  modifiers.each { |mod| action.key_down mod }
154
155
  action.click @element
@@ -202,10 +203,13 @@ module Watir
202
203
  # @example
203
204
  # browser.element(name: "new_user_button").double_click
204
205
  #
206
+ # @example
207
+ # browser.element(name: "new_user_button").double_click(scroll_to: :center)
208
+ #
205
209
 
206
- def double_click
210
+ def double_click(scroll_to: :center)
207
211
  element_call(:wait_for_present) do
208
- scroll.to
212
+ scroll.to(scroll_to) if scroll_to
209
213
  driver.action.double_click(@element).perform
210
214
  end
211
215
  browser.after_hooks.run
@@ -237,12 +241,16 @@ module Watir
237
241
  # @example Click an element with several modifier keys pressed
238
242
  # browser.element(name: "new_user_button").right_click(:shift, :alt)
239
243
  #
240
- # @param [:shift, :alt, :control, :command, :meta] modifiers to press while right clicking.
244
+ # @example Click an element with several modifier keys pressed and scroll position
245
+ # browser.element(name: "new_user_button").right_click(:shift, :alt, scroll_to: :center)
246
+ #
247
+ # @param [:shift, :alt, :control, :command, :meta, scroll_to: :center]
248
+ # modifiers to press while right clicking and scroll position.
241
249
  #
242
250
 
243
- def right_click(*modifiers)
251
+ def right_click(*modifiers, scroll_to: :center)
244
252
  element_call(:wait_for_present) do
245
- scroll.to
253
+ scroll.to(scroll_to) if scroll_to
246
254
  action = driver.action
247
255
  if modifiers.any?
248
256
  modifiers.each { |mod| action.key_down mod }
@@ -264,10 +272,13 @@ module Watir
264
272
  # @example
265
273
  # browser.element(name: "new_user_button").hover
266
274
  #
275
+ # @example
276
+ # browser.element(name: "new_user_button").hover(scroll_to: :center)
277
+ #
267
278
 
268
- def hover
279
+ def hover(scroll_to: :center)
269
280
  element_call(:wait_for_present) do
270
- scroll.to
281
+ scroll.to(scroll_to) if scroll_to
271
282
  driver.action.move_to(@element).perform
272
283
  end
273
284
  end
@@ -280,13 +291,14 @@ module Watir
280
291
  # a = browser.div(id: "draggable")
281
292
  # b = browser.div(id: "droppable")
282
293
  # a.drag_and_drop_on b
294
+ # a.drag_and_drop_on b, scroll_to: :center
283
295
  #
284
296
 
285
- def drag_and_drop_on(other)
297
+ def drag_and_drop_on(other, scroll_to: :center)
286
298
  assert_is_element other
287
299
 
288
300
  value = element_call(:wait_for_present) do
289
- scroll.to
301
+ scroll.to(scroll_to) if scroll_to
290
302
  driver.action
291
303
  .drag_and_drop(@element, other.wd)
292
304
  .perform
@@ -302,13 +314,17 @@ module Watir
302
314
  # @example
303
315
  # browser.div(id: "draggable").drag_and_drop_by 100, 25
304
316
  #
317
+ # @example
318
+ # browser.div(id: "draggable").drag_and_drop_by 100, 25, scroll_to: :center
319
+ #
305
320
  # @param [Integer] right_by
306
321
  # @param [Integer] down_by
322
+ # @param [Symbol] scroll_to
307
323
  #
308
324
 
309
- def drag_and_drop_by(right_by, down_by)
325
+ def drag_and_drop_by(right_by, down_by, scroll_to: :center)
310
326
  element_call(:wait_for_present) do
311
- scroll.to
327
+ scroll.to(scroll_to) if scroll_to
312
328
  driver.action
313
329
  .drag_and_drop_by(@element, right_by, down_by)
314
330
  .perform
@@ -530,11 +546,21 @@ module Watir
530
546
  element_call do
531
547
  return true unless present?
532
548
 
533
- scroll.to
549
+ scroll.to :bottom unless in_viewport?
534
550
  execute_js(:elementObscured, self)
535
551
  end
536
552
  end
537
553
 
554
+ #
555
+ # Returns true if the top of the element is visible in the viewport.
556
+ #
557
+ # @return [Boolean]
558
+ #
559
+
560
+ def in_viewport?
561
+ element_call { execute_js(:isElementInViewport, @element) }
562
+ end
563
+
538
564
  #
539
565
  # Returns given style property of this element.
540
566
  #
@@ -0,0 +1,20 @@
1
+ function(){
2
+ var el = arguments[0];
3
+ var top = el.offsetTop;
4
+ var left = el.offsetLeft;
5
+ var width = el.offsetWidth;
6
+ var height = el.offsetHeight;
7
+
8
+ while (el.offsetParent) {
9
+ el = el.offsetParent;
10
+ top += el.offsetTop;
11
+ left += el.offsetLeft;
12
+ }
13
+
14
+ return (
15
+ top < (window.pageYOffset + window.innerHeight) &&
16
+ left < (window.pageXOffset + window.innerWidth) &&
17
+ (top + height) > window.pageYOffset &&
18
+ (left + width) > window.pageXOffset
19
+ );
20
+ }
data/lib/watir/scroll.rb CHANGED
@@ -58,7 +58,8 @@ module Watir
58
58
  when :top, :start
59
59
  'window.scrollTo(0, 0);'
60
60
  when :center
61
- 'window.scrollTo(window.outerWidth / 2, window.outerHeight / 2);'
61
+ y = '(document.body.scrollHeight - window.innerHeight) / 2 + document.body.getBoundingClientRect().top'
62
+ "window.scrollTo(window.outerWidth / 2, #{y});"
62
63
  when :bottom, :end
63
64
  'window.scrollTo(0, document.body.scrollHeight);'
64
65
  when Array
data/lib/watir/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Watir
2
- VERSION = '7.0.0'.freeze
2
+ VERSION = '7.1.0'.freeze
3
3
  end
@@ -98,7 +98,6 @@ describe 'Browser::AfterHooks' do
98
98
  @page_after_hook = proc { @yield = browser.title == 'Non-control elements' }
99
99
  browser.after_hooks.add @page_after_hook
100
100
  div = browser.div(id: 'html_test')
101
- div.scroll.to
102
101
  div.double_click
103
102
  expect(@yield).to be true
104
103
  end
@@ -108,7 +107,6 @@ describe 'Browser::AfterHooks' do
108
107
  @page_after_hook = proc { @yield = browser.title == 'Right Click Test' }
109
108
  browser.after_hooks.add @page_after_hook
110
109
  div = browser.div(id: 'click')
111
- div.scroll.to
112
110
  div.right_click
113
111
  expect(@yield).to be true
114
112
  end
@@ -13,10 +13,22 @@ describe 'Element' do
13
13
  expect(droppable.text).to include 'Dropped!'
14
14
  end
15
15
 
16
+ it 'can drag and drop an element onto another with specified scroll position' do
17
+ expect(droppable.text).to include 'Drop here'
18
+ draggable.drag_and_drop_on droppable, scroll_to: :center
19
+ expect(droppable.text).to include 'Dropped!'
20
+ end
21
+
16
22
  it 'can drag an element by the given offset' do
17
23
  expect(droppable.text).to include 'Drop here'
18
24
  draggable.drag_and_drop_by 200, 50
19
25
  expect(droppable.text).to include 'Dropped!'
20
26
  end
27
+
28
+ it 'can drag an element by the given offset with specified scroll position' do
29
+ expect(droppable.text).to include 'Drop here'
30
+ draggable.drag_and_drop_by 200, 50, scroll_to: :center
31
+ expect(droppable.text).to include 'Dropped!'
32
+ end
21
33
  end
22
34
  end
@@ -170,10 +170,15 @@ describe 'Div' do
170
170
  except: {browser: :safari, reason: 'command correctly received, but action not taken'} do
171
171
  it 'fires the ondblclick event' do
172
172
  div = browser.div(id: 'html_test')
173
- div.scroll.to
174
173
  div.double_click
175
174
  expect(messages).to include('double clicked')
176
175
  end
176
+
177
+ it 'fires the ondblclick event with specified scroll position' do
178
+ div = browser.div(id: 'html_test')
179
+ div.double_click(scroll_to: :center)
180
+ expect(messages).to include('double clicked')
181
+ end
177
182
  end
178
183
 
179
184
  describe '#double_click!' do
@@ -196,6 +201,12 @@ describe 'Div' do
196
201
  expect(event_log.first).to eq('control=true alt=true')
197
202
  end
198
203
 
204
+ it 'accepts modifiers with scroll position', except: {browser: :ie} do
205
+ browser.goto(WatirSpec.url_for('right_click.html'))
206
+ browser.div(id: 'click-logger').right_click(:control, :alt, scroll_to: :center)
207
+ expect(event_log.first).to eq('control=true alt=true')
208
+ end
209
+
199
210
  it 'scrolls' do
200
211
  browser.del(class: 'footer').double_click
201
212
  puts 'yes?'
@@ -623,15 +623,65 @@ describe 'Element' do
623
623
  end
624
624
 
625
625
  describe '#hover' do
626
- it 'should hover over the element', except: {browser: :ie} do
627
- browser.goto WatirSpec.url_for('hover.html')
628
- link = browser.a
626
+ def element_color(element)
627
+ case element.style('color')
628
+ when 'rgba(0, 0, 255, 1)'
629
+ :blue
630
+ when 'rgba(255, 165, 0, 1)', 'rgb(255, 165, 0)'
631
+ :orange
632
+ else
633
+ raise rgba
634
+ end
635
+ end
636
+
637
+ it 'allows scrolling to top', except: {browser: :ie,
638
+ reason: 'needs require_window_focus'} do
639
+ browser.goto(WatirSpec.url_for('scroll.html'))
640
+ element = browser.div(id: 'center')
641
+
642
+ element.hover(scroll_to: :top)
643
+ expect(element_color(element)).to eq :orange
644
+
645
+ element_top = browser.execute_script('return arguments[0].getBoundingClientRect().top', element)
646
+ expect(element_top).to be_within(1).of(0)
647
+ end
648
+
649
+ it 'scrolls to center by default', except: {browser: :ie,
650
+ reason: 'needs require_window_focus'} do
651
+ browser.goto(WatirSpec.url_for('scroll.html'))
652
+ element = browser.div(id: 'center')
653
+
654
+ element.hover
655
+ expect(element_color(element)).to eq :orange
656
+
657
+ element_rect = browser.execute_script('return arguments[0].getBoundingClientRect()', element)
658
+
659
+ expect(element_rect['top']).to eq(element_rect['bottom'] - element_rect['height'])
660
+ end
661
+
662
+ it 'allows scrolling to bottom', except: {browser: :ie,
663
+ reason: 'needs require_window_focus'} do
664
+ browser.goto(WatirSpec.url_for('scroll.html'))
665
+ element = browser.div(id: 'center')
666
+
667
+ element.hover(scroll_to: :bottom)
668
+ expect(element_color(element)).to eq :orange
669
+
670
+ element_bottom = browser.execute_script('return arguments[0].getBoundingClientRect().bottom', element)
671
+ window_height = browser.execute_script('return window.innerHeight')
672
+
673
+ expect(element_bottom).to be_within(1).of(window_height)
674
+ end
675
+
676
+ it 'allows not scrolling', except: {browser: %i[chrome edge],
677
+ reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3955'} do
678
+ browser.goto(WatirSpec.url_for('scroll.html'))
679
+ element = browser.div(id: 'center')
680
+
681
+ browser.execute_script('return window.pageYOffset;')
682
+ browser.execute_script('return window.innerHeight;')
629
683
 
630
- expect(link.style('font-size')).to eq '10px'
631
- link.scroll.to
632
- link.hover
633
- link.wait_until { |l| l.style('font-size') == '20px' }
634
- expect(link.style('font-size')).to eq '20px'
684
+ expect { element.hover(scroll_to: nil) }.to raise_exception Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
635
685
  end
636
686
  end
637
687
 
@@ -914,40 +964,63 @@ describe 'Element' do
914
964
 
915
965
  it 'returns false if element center is not covered' do
916
966
  btn = browser.button(id: 'not_obscured')
917
- expect(btn).not_to be_obscured
967
+ btn.scroll.to :center
918
968
  expect { btn.click }.not_to raise_exception
969
+ expect(btn).not_to be_obscured
919
970
  end
920
971
 
921
972
  it 'returns false if element center is covered by its descendant' do
922
973
  btn = browser.button(id: 'has_descendant')
923
- expect(btn).not_to be_obscured
974
+ btn.scroll.to :center
924
975
  expect { btn.click }.not_to raise_exception
976
+ expect(btn).not_to be_obscured
925
977
  end
926
978
 
927
979
  it 'returns true if element center is covered by a non-descendant',
928
980
  except: {browser: :safari, reason: 'not getting element click intercepted'} do
929
981
  btn = browser.button(id: 'obscured')
930
- expect(btn).to be_obscured
931
-
982
+ btn.scroll.to :center
932
983
  expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::ElementClickInterceptedError)
984
+ expect(btn).to be_obscured
933
985
  end
934
986
 
935
987
  it 'returns false if element center is surrounded by non-descendants' do
936
988
  btn = browser.button(id: 'surrounded')
937
- expect(btn).not_to be_obscured
989
+ btn.scroll.to :center
938
990
  expect { btn.click }.not_to raise_exception
991
+ expect(btn).not_to be_obscured
939
992
  end
940
993
 
941
- it 'scrolls interactive element into view before checking if obscured' do
942
- btn = browser.button(id: 'requires_scrolling')
943
- expect(btn).not_to be_obscured
944
- expect { btn.click }.not_to raise_exception
994
+ it 'correctly scrolls element below viewport' do
995
+ browser.goto WatirSpec.url_for('sticky_elements.html')
996
+
997
+ element = browser.div(id: 'center')
998
+
999
+ browser.scroll.to :top
1000
+ expect { element.click }.not_to raise_exception
1001
+
1002
+ browser.scroll.to :top
1003
+ expect(element).not_to be_obscured
1004
+ end
1005
+
1006
+ it 'correctly scrolls element above viewport',
1007
+ except: {browser: %i[chrome edge],
1008
+ reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=3954'} do
1009
+ browser.goto WatirSpec.url_for('sticky_elements.html')
1010
+ element = browser.div(id: 'center')
1011
+
1012
+ browser.scroll.to :bottom
1013
+ expect { element.click }.not_to raise_exception
1014
+
1015
+ browser.scroll.to :bottom
1016
+ expect(element).not_to be_obscured
945
1017
  end
946
1018
 
947
1019
  it 'scrolls non-interactive element into view before checking if obscured' do
948
1020
  div = browser.div(id: 'requires_scrolling_container')
949
- expect(div).not_to be_obscured
950
1021
  expect { div.click }.not_to raise_exception
1022
+ browser.scroll.to :top
1023
+ expect(div).not_to be_obscured
951
1024
  end
952
1025
 
953
1026
  it 'returns true if element cannot be scrolled into view' do
@@ -1,32 +1,15 @@
1
1
  <html>
2
2
  <head>
3
- <script>
4
- function isElementInViewport(el) {
5
- var top = el.offsetTop;
6
- var left = el.offsetLeft;
7
- var width = el.offsetWidth;
8
- var height = el.offsetHeight;
9
-
10
- while (el.offsetParent) {
11
- el = el.offsetParent;
12
- top += el.offsetTop;
13
- left += el.offsetLeft;
14
- }
15
-
16
- return (
17
- top < (window.pageYOffset + window.innerHeight) &&
18
- left < (window.pageXOffset + window.innerWidth) &&
19
- (top + height) > window.pageYOffset &&
20
- (left + width) > window.pageXOffset
21
- );
22
- }
23
- </script>
3
+ <style media="screen">
4
+ div { color: blue; }
5
+ div:hover { color: orange; }
6
+ </style>
24
7
  </head>
25
8
  <body>
26
- <div style="height: 180%; position: relative;">
27
- <button style="position: absolute;">Top</button>
28
- <button style="position: absolute; top: 50%;">Center</button>
29
- <button style="position: absolute; bottom: 0;">Bottom</button>
9
+ <div style="height: 300%; position: relative;">
10
+ <div id="top" style="position: absolute; top: 0;">top</div>
11
+ <div id="center" style="position: absolute; top: 50%;">Center</div>
12
+ <div id="bottom" style="position: absolute; top: 100%;">bottom</div>
30
13
  </div>
31
14
  </body>
32
15
  </html>
@@ -0,0 +1,10 @@
1
+ <html>
2
+ <body>
3
+ <div style="height: 300%; position: relative;">
4
+ <header style="position: sticky; z-index: 10; top: 0;"><h1>Sticky Header</h1></header>
5
+ <div id="top" style="position: absolute; top: 0;">top</div>
6
+ <div id="center" style="position: absolute; top: 50%;">Center</div>
7
+ <div id="bottom" style="position: absolute; top: 100%;">bottom</div>
8
+ </div>
9
+ </body>
10
+ </html>
@@ -5,101 +5,131 @@ describe Watir::Scrolling do
5
5
  browser.goto(WatirSpec.url_for('scroll.html'))
6
6
  end
7
7
 
8
- def visible?(element)
9
- browser.execute_script('return isElementInViewport(arguments[0]);', element)
8
+ def top_centered_viewport
9
+ browser.execute_script('return (window.innerHeight - document.body.scrollHeight)/2;')
10
+ end
11
+
12
+ def current_top
13
+ browser.execute_script('return document.body.getBoundingClientRect().top;')
10
14
  end
11
15
 
12
16
  context 'when scrolling Browser' do
13
17
  describe '#to' do
14
18
  it 'scrolls to the top of the page' do
15
19
  browser.scroll.to :bottom
20
+ expect(browser.div(id: 'top')).not_to be_in_viewport
16
21
  browser.scroll.to :top
17
- expect(visible?(browser.button(text: 'Top'))).to eq(true)
18
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
19
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
22
+ expect(browser.div(id: 'top')).to be_in_viewport
20
23
  end
21
24
 
22
25
  it 'scrolls to the center of the page' do
23
26
  browser.scroll.to :center
24
- expect(visible?(browser.button(text: 'Top'))).to eq(false)
25
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
26
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
27
+ viewport_top = browser.execute_script('return document.body.getBoundingClientRect().top;')
28
+
29
+ expect(viewport_top).to be_within(1).of(top_centered_viewport)
27
30
  end
28
31
 
29
32
  it 'scrolls to the bottom of the page' do
33
+ expect(browser.div(id: 'bottom')).not_to be_in_viewport
30
34
  browser.scroll.to :bottom
31
- expect(visible?(browser.button(text: 'Top'))).to eq(false)
32
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
33
- expect(visible?(browser.button(text: 'Bottom'))).to eq(true)
35
+ expect(browser.div(id: 'bottom')).to be_in_viewport
34
36
  end
35
37
 
36
38
  it 'scrolls to coordinates' do
37
- button = browser.button(text: 'Bottom')
38
- browser.scroll.to [button.wd.location.x, button.wd.location.y]
39
- expect(visible?(button)).to eq(true)
39
+ element = browser.div(id: 'center')
40
+ location = element.location
41
+ browser.scroll.to [location.x, location.y]
42
+ expect(element).to be_in_viewport
40
43
  end
41
44
 
42
- it 'raises error when scroll point is not vaild' do
45
+ it 'raises error when scroll point is not valid' do
43
46
  expect { browser.scroll.to(:blah) }.to raise_error(ArgumentError)
44
47
  end
45
48
  end
46
49
 
47
50
  describe '#by' do
48
- it 'offset' do
51
+ before do
52
+ sleep(0.1)
53
+ browser.scroll.to :top
54
+ end
55
+
56
+ it 'offset from top' do
57
+ initial_top = current_top
58
+ scroll_by = top_centered_viewport
59
+ browser.scroll.by(0, -scroll_by)
60
+
61
+ expect(current_top).to be_within(1).of(scroll_by + initial_top)
62
+ end
63
+
64
+ it 'offset from bottom' do
65
+ browser.scroll.to :top
66
+ initial_top = current_top
49
67
  browser.scroll.to :bottom
50
- browser.scroll.by(-10_000, -10_000)
68
+ scroll_by = top_centered_viewport
69
+ browser.scroll.by(0, scroll_by)
51
70
 
52
- expect(visible?(browser.button(text: 'Top'))).to eq(true)
53
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
54
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
71
+ expect(current_top).to be_within(1).of(scroll_by + initial_top)
55
72
  end
56
73
  end
57
74
  end
58
75
 
59
76
  context 'when scrolling Element' do
60
77
  describe '#to' do
61
- it 'scrolls to element (top)' do
62
- browser.button(text: 'Center').scroll.to
63
- expect(visible?(browser.button(text: 'Top'))).to eq(false)
64
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
65
- expect(visible?(browser.button(text: 'Bottom'))).to eq(true)
78
+ it 'scrolls to element (top) by default' do
79
+ element = browser.div(id: 'center')
80
+ element.scroll.to
81
+
82
+ element_top = browser.execute_script('return arguments[0].getBoundingClientRect().top', element)
83
+ expect(element_top).to be_within(1).of(0)
66
84
  end
67
85
 
68
86
  it 'scrolls to element (center)' do
69
- browser.button(text: 'Center').scroll.to :center
70
- expect(visible?(browser.button(text: 'Top'))).to eq(false)
71
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
72
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
87
+ element = browser.div(id: 'center')
88
+ element.scroll.to :center
89
+
90
+ element_rect = browser.execute_script('return arguments[0].getBoundingClientRect()', element)
91
+
92
+ expect(element_rect['top']).to eq(element_rect['bottom'] - element_rect['height'])
73
93
  end
74
94
 
75
95
  it 'scrolls to element (bottom)' do
76
- browser.button(text: 'Center').scroll.to :bottom
77
- expect(visible?(browser.button(text: 'Top'))).to eq(true)
78
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
79
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
96
+ element = browser.div(id: 'center')
97
+ element.scroll.to :bottom
98
+
99
+ element_bottom = browser.execute_script('return arguments[0].getBoundingClientRect().bottom', element)
100
+ window_height = browser.execute_script('return window.innerHeight')
101
+
102
+ expect(element_bottom).to be_within(1).of(window_height)
80
103
  end
81
104
 
82
105
  it 'scrolls to element multiple times' do
83
106
  2.times do
84
- browser.button(text: 'Center').scroll.to(:center)
85
- expect(visible?(browser.button(text: 'Top'))).to eq(false)
107
+ element = browser.div(id: 'center')
108
+ element.scroll.to(:center)
109
+
110
+ element_rect = browser.execute_script('return arguments[0].getBoundingClientRect()', element)
111
+
112
+ expect(element_rect['top']).to eq(element_rect['bottom'] - element_rect['height'])
86
113
  end
87
114
  end
88
115
 
89
- it 'raises error when scroll param is not vaild' do
90
- expect { browser.button(text: 'Top').scroll.to(:blah) }.to raise_error(ArgumentError)
116
+ it 'raises error when scroll param is not valid' do
117
+ expect { browser.div(id: 'top').scroll.to(:blah) }.to raise_error(ArgumentError)
91
118
  end
92
119
  end
93
120
 
94
121
  describe '#by' do
95
122
  it 'offset' do
123
+ sleep 0.1
124
+ browser.scroll.to :top
125
+ initial_top = current_top
96
126
  browser.scroll.to :bottom
97
- button = browser.button(text: 'Bottom')
98
- button.scroll.by(-10_000, -10_000) # simulate scrolling to top
127
+ scroll_by = top_centered_viewport
128
+
129
+ element = browser.div(id: 'bottom')
130
+ element.scroll.by(0, scroll_by)
99
131
 
100
- expect(visible?(browser.button(text: 'Top'))).to eq(true)
101
- expect(visible?(browser.button(text: 'Center'))).to eq(true)
102
- expect(visible?(browser.button(text: 'Bottom'))).to eq(false)
132
+ expect(current_top).to be_within(1).of(scroll_by + initial_top)
103
133
  end
104
134
  end
105
135
  end
@@ -39,7 +39,6 @@ class LocalConfig
39
39
  Webdrivers::Geckodriver.update
40
40
  "geckodriver version: #{Webdrivers::Geckodriver.current_version.version}"
41
41
  when :ie
42
- Webdrivers::IEdriver.required_version = '3.150.1'
43
42
  Webdrivers::IEdriver.update
44
43
  "iedriver version: #{Webdrivers::IEdriver.current_version.version}"
45
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rodionov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-10-18 00:00:00.000000000 Z
13
+ date: 2021-11-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: selenium-webdriver
@@ -364,6 +364,7 @@ files:
364
364
  - lib/watir/js_snippets/getInnerText.js
365
365
  - lib/watir/js_snippets/getOuterHtml.js
366
366
  - lib/watir/js_snippets/getTextContent.js
367
+ - lib/watir/js_snippets/isElementInViewport.js
367
368
  - lib/watir/js_snippets/isImageLoaded.js
368
369
  - lib/watir/js_snippets/selectOptionsLabel.js
369
370
  - lib/watir/js_snippets/selectOptionsText.js
@@ -543,7 +544,6 @@ files:
543
544
  - spec/watirspec/html/frame_1.html
544
545
  - spec/watirspec/html/frame_2.html
545
546
  - spec/watirspec/html/frames.html
546
- - spec/watirspec/html/hover.html
547
547
  - spec/watirspec/html/iframe_1.html
548
548
  - spec/watirspec/html/iframes.html
549
549
  - spec/watirspec/html/images.html
@@ -581,6 +581,7 @@ files:
581
581
  - spec/watirspec/html/right_click.html
582
582
  - spec/watirspec/html/scroll.html
583
583
  - spec/watirspec/html/special_chars.html
584
+ - spec/watirspec/html/sticky_elements.html
584
585
  - spec/watirspec/html/tables.html
585
586
  - spec/watirspec/html/timeout_window_location.html
586
587
  - spec/watirspec/html/uneven_table.html
@@ -751,7 +752,6 @@ test_files:
751
752
  - spec/watirspec/html/frame_1.html
752
753
  - spec/watirspec/html/frame_2.html
753
754
  - spec/watirspec/html/frames.html
754
- - spec/watirspec/html/hover.html
755
755
  - spec/watirspec/html/iframe_1.html
756
756
  - spec/watirspec/html/iframes.html
757
757
  - spec/watirspec/html/images.html
@@ -789,6 +789,7 @@ test_files:
789
789
  - spec/watirspec/html/right_click.html
790
790
  - spec/watirspec/html/scroll.html
791
791
  - spec/watirspec/html/special_chars.html
792
+ - spec/watirspec/html/sticky_elements.html
792
793
  - spec/watirspec/html/tables.html
793
794
  - spec/watirspec/html/timeout_window_location.html
794
795
  - spec/watirspec/html/uneven_table.html
@@ -1,12 +0,0 @@
1
- <html>
2
- <head>
3
- <style type="text/css" media="screen">
4
- a { font-size: 10px; }
5
- a:hover { font-size: 20px; }
6
- </style>
7
- </head>
8
-
9
- <body>
10
- <a href="#">hello</a>
11
- </body>
12
- </html>