0
votes

I am looking for any ideas on how best to handle file upload failures in selenium webdriver.

Recently I have been seeing a higher number of failures in my webdriver test suite, the error I am trying to fix is a failure in the Internet Explorer tests caused by a file upload failing.

In webdriver the only way I know to upload a file to an input element is to use the SendKeys() method and pass the file path to SendKeys() this works like a charm in chrome and firefox but periodically have had issues with it in Internet Explorer. What appears to happen is the file upload window gets opened but the path is not typed nor is the file uploaded. This leaves the node with a file upload window open, being this is a native window's window and not a webpage selenium cannot interact with the pop up. enter image description here

The result is the HttpWebRequest does not get a response back triggering a WebdriverTimoutException. This results in the session getting cleaned up. This causes a cascade of failures for all other tests in the suite as the session has been terminated.

Environment Info:

  • Selenium.Webdriver & Seleneium.Support version 3.7.0
  • IEDriver 3.7.0
  • Testing in IE 11
2
Any change to be related to this? Can you add the Selenium version your using to your question?Tom
So which aspect do you want to address uploading a file to an input element causing file upload window open or get rid of opened up file upload window ?undetected Selenium
@Tom I was not aware of that parameter so I will have to experiment with it, it does not feel like it was hitting that timeout as I would expect some more descriptive error messaging if I was hitting that timeout like FileUploadTimetoutException or something (not sure if that exists or not) but I will try configuring that timeout and see if that helps. Still does not address my true question of "If this situation occurs what is the best way to clean up so tests within the same session are unaffected by this failure."Buster
@DebanjanB - The crux of my question is how to get rid of file upload window I can wrap calls that may result in this situation but even if I catch this situation I do not know how to "clean up" the file upload window. So subsequent tests can run.Buster

2 Answers

1
votes

I may be late to answer this question, but someone will benefit.

The file(s) in question should be available on the machine (be it local or remote server) that your program is running on, for example, in your /resources directory

On your local machine, this should work.

chooseFileElement.waitForVisible().type("/file/path/filename.jpg");
clickButton("Attach File");

On Remote Server however, you need to associate a new instance of LocalFileDetector to the <input type=file> element.

LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile("/file/path/filename.jpg");
RemoteWebElement input = (RemoteWebElement) myDriver().findElement(By.id("fileUpload"));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
clickButton("Attach File");
0
votes

It really depends on how you've written your code. What I would do is when you get down to the end and detect that you've gotten into a bad state, e.g. the File | Open dialog has been left open, close the dialog and start that part of the script over. I'm not sure what start over in your case would mean... maybe it means reload the page and start from there... maybe it means just .sendKeys() to the INPUT again.

You should be able to dismiss the dialog by sending an ESC to the page. You should be able to use .sendKeys() on the BODY tag or whatever.

You should be able to detect the bad state by catching certain exceptions.