rstudio / webdriver

WebDriver client in R
https://rstudio.github.io/webdriver/
Other
70 stars 16 forks source link

Add session$upload_file method #44

Closed wch closed 8 years ago

wch commented 8 years ago

This adds a session$upload_file() method.

The webdriver spec says that you should be able to upload files with element.sendKeys(), but that currently doesn't work in PhantomJS: ariya/phantomjs#10993

The workaround is to POST to /session/<session_id>/file with the following:

{
  "selector": "input[name=file]",
  "filepath": ["file1", "file2"],
}

I would have preferred to add an upload_file to the element class, but as far as I can tell, the /session/<session_id>/file endpoint requires a CSS selector, and will not accept an element's webdriver ID: https://github.com/detro/ghostdriver/blob/873c9d6/src/request_handlers/session_request_handler.js#L212

gaborcsardi commented 8 years ago

I would have preferred to add an upload_file to the element class, but as far as I can tell, the /session//file endpoint requires a CSS selector, and will not accept an element's webdriver ID: https://github.com/detro/ghostdriver/blob/873c9d6/src/request_handlers/session_request_handler.js#L212

How about manually creating a selector for the id. I.e. paste0('#', self$get_id())?

gaborcsardi commented 8 years ago

How about manually creating a selector for the id. I.e. paste0('#', self$get_id())?

Oh, wait, it might not have an id, of course.

gaborcsardi commented 8 years ago

Oh, wait, it might not have an id, of course.

But we can still create a selector for the element, I suppose, and supply that.

wch commented 8 years ago

But we can still create a selector for the element, I suppose, and supply that.

How would you create a selector for an arbitrary element?

gaborcsardi commented 8 years ago

I suppose the element has to be an upload button, otherwise $upload_file() does not make sense. And then we could use the tag and attributes of the button to create a selector. Isn't that what you are kind of doing with "selector": "input[name=file]"?

wch commented 8 years ago

It does have to be a <input type=file>, so the selector input[type=file] would get part of the way there (in the example code I provided above, I assumed the input also had name=file and so I used that as the selector).

These inputs can have a name and/or id, but I don't think either of those attributes is strictly required. I think that's OK though -- I can have it try to construct a selector using the id, then name, and if neither is available, just throw an error. That should cover all realistic use cases.

wch commented 8 years ago

It should be good to go now.