Skip to content

Commit

Permalink
Merge pull request #3 from willnet/hover-take2
Browse files Browse the repository at this point in the history
wait before hover
  • Loading branch information
willnet committed Jan 18, 2023
2 parents 16b421c + 9598ea3 commit 510516a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Style/StringLiteralsInInterpolation:

Layout/LineLength:
Max: 120

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
5 changes: 5 additions & 0 deletions lib/capybara/wait_before_click.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def click(*keys, **options)
super
end

def hover
_wait_for_image_loading
super
end

def _logger
@_logger ||= if defined?(Rails)
Rails.logger
Expand Down
4 changes: 3 additions & 1 deletion spec/app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "sinatra"
require "securerandom"

class App < Sinatra::Base
get "/next" do
"<html><body><h1>Next Page</h1></body></html>"
Expand All @@ -14,7 +16,7 @@ class App < Sinatra::Base
%(<html><body><a href='/next'>Next Page</a></body></html>)
end

get "/heavy_image.png" do
get "/heavy_image/*.png" do
sleep 2
send_file("spec/heavy_image.png")
end
Expand Down
16 changes: 16 additions & 0 deletions spec/capybara/wait_before_click_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@

expect(without_images_duration + 2).to be_within(0.2).of(with_a_image_duration)
end

it "wait a image before hover" do
visit "/without_images"
without_images_before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
find_link("Next Page").hover
without_images_after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
visit "/with_a_image"
with_a_image_before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
find_link("Next Page").hover
with_a_image_after = Process.clock_gettime(Process::CLOCK_MONOTONIC)

without_images_duration = without_images_after - without_images_before
with_a_image_duration = with_a_image_after - with_a_image_before

expect(without_images_duration + 2).to be_within(0.2).of(with_a_image_duration)
end
end
2 changes: 1 addition & 1 deletion spec/views/with_a_image.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<script type="text/javascript">
setTimeout(() => {
const img = document.createElement('img');
img.setAttribute('src', '/heavy_image.png');
img.setAttribute('src', '/heavy_image/<%= SecureRandom.hex(10) %>.png');
document.body.prepend(img);
}, 1)
</script>
Expand Down

0 comments on commit 510516a

Please sign in to comment.