Passing multiple files to input node with Watir (u

2019-02-28 03:05发布

问题:

So I have hit a bit of a snag. I am trying to automate a test case where I need to pass multiple files to an input node and I cannot figure out how to do so. I can use either Mechanize or Watir, but have found very little information on a topic that seems relatively major in automation. In the snippets below, I'm using Watir with Ruby. The main issue I'm having is that it seems when multiple files are selected, the input node is no longer visible. The input node does accept multiple files, and passing in a single path does result in a successful upload, like so.

path1 = "/path/to/file.json"
file_field.set path1

I would think that passing in multiple files would be as simple as passing in a string with multiple paths separated by some sort of delimiter. I'm not particularly savvy with web dev however, and am struggling to grasp where I should even start. When I attempt to pass in multiple files like so:

multiple_paths = ("/path/to/file1.json"; "/path/to/file2.json")
file_field.set multiple_paths

it gives uploads the second file but not the first (making me think maybe it's uploading them in sequence, and the second is overwriting).

Do you think this is even possible using Watir? I know that Chrome has a workaround for uploading multiple files using \n as the delimiter, is there a similar workaround for Firefox?

回答1:

Currently there doesn't seem to be a workaround for Firefox. If anybody knows of one, please post the answer as I couldn't find a solution anywhere. I figured I'd post the solution for Chrome here because resources are scarce on this.

If you need to test for multiple file uploads, have that particular instance load the Chrome driver with:

@browser = Watir::Browser.new :chrome, :prefs => profile

Then you're going to want to pass it a string that looks something like this:

paths = "path/to/first/file.json\npath/to/second/file.json\n...etc
file_field.send_keys paths