-->

attach_file isn't working the right way with c

2019-08-14 14:47发布

问题:

I'm trying to attach some files to this input:

<input type="file" name="image" multiple="" accept="image/*">

My code, that is working when I use selenium driver, is:

attach_file('image', File.absolute_path('../pictures/pic1.JPG'))
attach_file('image', File.absolute_path('../pictures/pic2.JPG'))
attach_file('image', File.absolute_path('../pictures/pic3.JPG'))

The problems come when I use capybara-webkit: as the input is hidden and some elements overlaps it, I need to change some css properties:

page.execute_script("$('input[name=image]').css('opacity','1')")
page.execute_script("$('input[name=image]').css('position','fixed')")
page.execute_script("$('input[name=image]').css('top','620px')")

And even if the test passes, the pictures aren't uploading the right way. If I use page.save_screenshot('after_upload.png') to see what's happening:

Expected result (and result when using selenium driver) ->

Actual result when using capybara-webkit ->

回答1:

I just found a "fix": put a sleep(3) after each image upload!