0

I have a website where user can send his photo to a contest. He clicks to choose file from disk and sends it using Firefox.

I want to test my form with Selenium in C#. I know there is a way to send local file path to input file, but i want to test it including Windows 7 file open dialog.

I've written test, click a button to open file dialog and execute this code to set path to local file inside the File Upload Dialog:

System.Windows.Forms.SendKeys.SendWait(localFile);
Thread.Sleep(1500);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");

It works properly when i test it on my profile on Windows. But i have another profile on Windows where website test is running. So i use my personal profile to use Windows, and tests run on the second profile. When i set the test to be executed on my second profile than it is not executed properly.

When i switch to this profile i see a dialog window and the localFile url is typed into the File Dialog at the very moment when i switched to the profile. It seems like SendWait does not work when i am not active in the profile where test is executed.

So my question is: what to do to make the test handle File Dialog properly on a profile i am not active when test is executed?

Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236

2 Answers2

0

Assuming that you mean that those tests run on another account with another desktop on the same machine, then there is a high chance that the issue is caused by the way SendInput (it is used internally by SendKeys) works - it seems to work only on the current active desktop.

I can't find any reliable documentation from MS on that part, but that's logical and this answer(on a bit different question) says

it's heavily reliant on the underlying Win32 SendInput API. This won't work at all for keyboard input when a desktop is inactive.

Or it may be because of how your testing infrastructure is set (perhaps it is a non-interactive service - https://blogs.msdn.microsoft.com/patricka/2010/04/27/what-is-interactive-services-detection-and-why-is-it-blinking-at-me/ that is not bound to any desktop).

If current active desktop is the reason, then the alternatives are:

  1. Separate testing machine, probably virtual (it is the best solution from this and other points of view - one should strive to not test anything on its own machine).
  2. Try PostMessage - that should be independent of active desktop issues - you have window handle - you post message to it, though other issues can surface with it.
  3. Try System.Windows.Automation to directly change the controls (AFAIK it shouldn't use SendInput or the likes).
  4. Keyboard driver - perhaps it can overcome such issues, or perhaps not - that should be researched.
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
0

Basically upload works in simpler way with native Selenium support:

  • You should find element "input[type='file']
  • Call inputFileElement.SendKeys(fullPathToFile)

fullfilePath could be any valid windows path or Network Share

Vitalii
  • 86
  • 2