Skip to content

Instantly share code, notes, and snippets.

@benalavi
Created July 25, 2018 01:58
Show Gist options
  • Save benalavi/615df90c18d1474d94876fced68e44b4 to your computer and use it in GitHub Desktop.
Save benalavi/615df90c18d1474d94876fced68e44b4 to your computer and use it in GitHub Desktop.
Tests that clicking inside an iframe with a padding does not work in headless chrome (issue is actually in chromedriver: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2069)
require "minitest/autorun"
require "capybara/dsl"
require "selenium-webdriver"
Capybara.javascript_driver = :selenium_chrome_headless
Capybara.default_driver = Capybara.javascript_driver
Capybara.app = lambda do |env|
req = Rack::Request.new(env)
if env["PATH_INFO"] == "/frame1"
# src for frame1 (which contains the link we're trying to click)
body = <<-HTML
<!DOCTYPE html>
<html>
<head></head>
<body style="background-color:#afa">
<p>Frame 1 Content</p>
<a href="/frame2">Frame 2</a>
</body>
</html>
HTML
elsif env["PATH_INFO"] == "/frame2"
# src for frame2 (which shows we clicked the link)
body = <<-HTML
<!DOCTYPE html>
<html>
<head></head>
<body style="background-color:#aaf">
<p>Frame 2 Content</p>
</body>
</html>
HTML
elsif env["PATH_INFO"] == "/frame_with_padding"
# src for container which uses an iframe w/ padding
body = <<-HTML
<!DOCTYPE html>
<html>
<head></head>
<body style="background-color:#fff">
<p style="height:200px;">Outer Content</p>
<iframe name="main" src="/frame1" style="height:300px;padding-top:100px;"></iframe>
</body>
</html>
HTML
elsif env["PATH_INFO"] == "/frame_without_padding"
# src for container which uses an iframe w/o padding
body = <<-HTML
<!DOCTYPE html>
<html>
<head></head>
<body style="background-color:#fff">
<p style="height:200px;">Outer Content</p>
<iframe name="main" src="/frame1" style="height:300px;"></iframe>
</body>
</html>
HTML
end
Rack::Response.new body, 200, {}
end
class PoltergeistTest < MiniTest::Test
include Capybara::DSL
def test_click_element_inside_iframe_with_padding
visit "/frame_with_padding"
within_frame "main" do
assert has_content?("Frame 1 Content")
assert has_no_content?("Frame 2 Content")
click_link "Frame 2"
assert has_no_content?("Frame 1 Content")
assert has_content?("Frame 2 Content")
end
end
def test_click_element_inside_iframe_without_padding
visit "/frame_without_padding"
within_frame "main" do
assert has_content?("Frame 1 Content")
assert has_no_content?("Frame 2 Content")
click_link "Frame 2"
assert has_no_content?("Frame 1 Content")
assert has_content?("Frame 2 Content")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment