4

I am writing an automated UI test solution based on Selenium WebDriver. I need a browser-independent method that can achieve file downloads.

My method, which relies on the System.Net.WebClient class, can successfully download files from a site if there is an attribute (such as href or src) from which to infer the file location.

However, I am having issues when the element that would normally hold the reference has no pointer to a file. Instead, this element has a CSS class that is tied to a Javascript click event. When the element is clicked, Javascript triggers a window.open action on a dynamically-created link.

Any ideas on how can I successfully capture that file reference from my C# code?

Ameto
  • 167
  • 2
  • 9

1 Answers1

0

If I understand you correctly, you are building the client application and the server behavior is beyond your control. You want to mimic the following behavior of a real browser:

  1. The client navigates to a URL.
  2. The client clicks on a link (or other element) which triggers a JavaScript function.
  3. The JavaScript function redirects the client to the correct URL for downloading an asset.
  4. The client downloads the asset.

You cannot do this with the WebClient class, which does not expose any click behavior or any way to execute JavaScript. A WebClient instance is not a browser. It is just a class that provides a way to send data to and receive data from a given URL.

This does not answer your question directly, but there are arguments to be made that when using an automation tool you should stick to what can be done through the tool IE, you should use a browser to do whatever it is you need to do. If for some reason you just really need to download the result of that click event, but cannot do so through selenium, then you will probably need to use a headless browser. This question discusses some options you could use.

This question discusses some more options.

Community
  • 1
  • 1
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
  • Yes, that is exactly what I am trying to accomplish. I was hoping I wouldn't have to maintain a browser configuration file per browser that I am targeting, since that adds its own set of complications. I'll look at those links you posted and see if I can find an alternate solution – Ameto Jul 17 '15 at 14:23