Skip to content

Commit

Permalink
Allow to use multiple files with file detector
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Jul 26, 2018
1 parent 2be2884 commit a3d2f41
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
46 changes: 46 additions & 0 deletions common/src/web/upload_multiple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Multiple Upload Form</title>
<script>
var intervalId;
function onTick() {
var label = document.getElementById('upload_label');
label.innerHTML += '.';
}

function onUploadSubmit() {
document.getElementById('upload_target').contentWindow.document.body.
innerHTML = '';
var label = document.getElementById('upload_label');
label.innerHTML = 'Uploading "' + document.forms[0].upload.value + '"';
label.style.display = '';
intervalId = window.setInterval(onTick, 500);
return true;
}

function onUploadDone() {
var label = document.getElementById('upload_label');
label.style.display = 'none';
window.clearInterval(intervalId);
return true;
}
</script>
</head>
<body>
<form action="/common/upload" method="post" name="upload_form"
target="upload_target" enctype="multipart/form-data"
onsubmit="onUploadSubmit();">
<div>
Enter a file to upload:
<div><input id="upload" name="upload[]" type="file" multiple/></div>
<div><input id="go" type="submit" value="Go!"/></div>
</div>
<div id="upload_label" style="display:none"></div>
<iframe src="" id="upload_target" name="upload_target"
style="width:300px;height:200px">
</iframe>
</form>
</body>
</body>
</html>
7 changes: 5 additions & 2 deletions rb/lib/selenium/webdriver/remote/oss/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,11 @@ def send_keys_to_active_element(key)

def send_keys_to_element(element, keys)
if @file_detector
local_file = @file_detector.call(keys)
keys = upload(local_file) if local_file
local_files = keys.first.split("\n").map { |key| @file_detector.call(key) }.compact
if local_files.any?
keys = local_files.map { |local_file| upload(local_file) }
keys = keys.join("\n")
end
end

execute :send_keys_to_element, {id: element}, {value: Array(keys)}
Expand Down
7 changes: 5 additions & 2 deletions rb/lib/selenium/webdriver/remote/w3c/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,11 @@ def click_element(element)

def send_keys_to_element(element, keys)
if @file_detector
local_file = @file_detector.call(keys)
keys = [upload(local_file)] if local_file
local_files = keys.first.split("\n").map { |key| @file_detector.call(key) }.compact
if local_files.any?
keys = local_files.map { |local_file| upload(local_file) }
keys = Array(keys.join("\n"))
end
end

# Keep .split(//) for backward compatibility for now
Expand Down
36 changes: 26 additions & 10 deletions rb/spec/integration/selenium/webdriver/remote/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@

module Selenium
module WebDriver
describe Element, only: {driver: :remote, browser: %i[chrome ff_esr firefox]} do
describe Element, only: {driver: :remote} do
before do
driver.file_detector = ->(_str) { __FILE__ }
driver.file_detector = ->(filename) { File.join(__dir__, filename) }
end

after do
driver.file_detector = nil
end

it 'uses the file detector' do
driver.navigate.to url_for('upload.html')
context 'when uploading one file', only: {browser: %i[chrome ff_esr firefox ie]} do
it 'uses the file detector' do
driver.navigate.to url_for('upload.html')

driver.find_element(id: 'upload').send_keys('random string')
driver.find_element(id: 'go').submit
wait.until { driver.find_element(id: 'upload_label').displayed? }
driver.find_element(id: 'upload').send_keys('element_spec.rb')
driver.find_element(id: 'go').submit
wait.until { driver.find_element(id: 'upload_label').displayed? }

driver.switch_to.frame('upload_target')
body = driver.find_element(xpath: '//body')
expect(body.text).to include('uses the set file detector')
driver.switch_to.frame('upload_target')
body = driver.find_element(xpath: '//body')
expect(body.text.scan('Licensed to the Software Freedom Conservancy').count).to eq(3)
end
end

context 'when uploading multiple files', only: {browser: %i[chrome ie]} do
it 'uses the file detector' do
driver.navigate.to url_for('upload_multiple.html')

driver.find_element(id: 'upload').send_keys("driver_spec.rb\nelement_spec.rb")
driver.find_element(id: 'go').submit
wait.until { driver.find_element(id: 'upload_label').displayed? }

driver.switch_to.frame('upload_target')
body = driver.find_element(xpath: '//body')
expect(body.text.scan('Licensed to the Software Freedom Conservancy').count).to eq(4)
end
end
end
end # WebDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ def call(env)
case env['PATH_INFO']
when '/common/upload'
req = Rack::Request.new(env)
body = req['upload'][:tempfile].read
body = case req['upload']
when Array
req['upload'].map { |upload| upload[:tempfile].read }.join("\n")
when Hash
req['upload'][:tempfile].read
end

[200, {'Content-Type' => 'text/html'}, [body]]
else
Expand Down

0 comments on commit a3d2f41

Please sign in to comment.