Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Last active March 27, 2026 09:12
Show Gist options
  • Select an option

  • Save alpaca-tc/12b479ae6f9fa514df5514c077ddb52c to your computer and use it in GitHub Desktop.

Select an option

Save alpaca-tc/12b479ae6f9fa514df5514c077ddb52c to your computer and use it in GitHub Desktop.
> ruby issue.rb
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "capybara"
gem "selenium-devtools"
gem "selenium-webdriver"
gem "puma"
gem "rackup"
end
require 'capybara'
require 'capybara/dsl'
require 'rack'
require 'rack/static'
require 'websocket'
html = <<~HTML
<!doctype html>
<html lang="en">
<body>
<form>
<input type="file" id="file" name="file" />
<img id="preview" src="" alt="Image preview" style="display: none; max-width: 200px; margin-top: 10px;" />
</form>
</body>
<script>
const form = document.querySelector('form');
const file = document.querySelector('#file');
const preview = document.querySelector('#preview');
file.addEventListener('change', () => {
const selectedFile = file.files[0];
if (selectedFile) {
const reader = new FileReader();
reader.onload = (e) => {
preview.src = e.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(selectedFile);
} else {
preview.src = '';
preview.style.display = 'none';
}
});
</script>
</html>
HTML
Capybara.default_driver = :selenium_chrome_headless
Capybara.app = Rack::ContentLength.new(
proc { [200, { "content-type" => "text/html" }, [html]] }
)
class Test
include Capybara::DSL
def run
# The following code causes the issue, but if you comment it out, it works fine.
page.driver.browser.intercept do |request, &continue|
continue.call(request)
end
WebSocket.max_frame_size = 1024
visit '/'
image = Tempfile.new(["avatar", ".png"]) do |f|
f.binmode
f.write OpenURI.open_uri("https://github.com/alpaca-tc.png").read
f.flush
end
attach_file("file", image.path)
end
end
Test.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment