How to handle Windows file upload in .Net core using Selenium? - selenium-webdriver

I am new to the C# world and I want to know how can a Windows file upload form be automated using Selenium in a .Net core project as it doesn't support AutoIt.

Zehra - It really depends on how precise you want to get to mimicking the user functionality. Typically you click a button that opens a windows dialog where you locate the file on your pc/phone. Since this is really not testing the application and is just a Windows function, I just use send keys. If you want to get more precise, you can look at AutoIT but I would suggest just doing send keys.
Set your location for the file.
string filePath = #"C:\MyFiles\Test.jpg";
Then find the path to the input for the file upload.
driver.FindElement(By.XPath("//div[#class='FileUploadInput']")).SendKeys(filePath);
If you have a spinner or a bar for the upload process, I would wait until that element is no longer visible and then proceed.
As an example - go here - https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_get
In the example if you look at the "choose file" element, it looks like:
<input type="file" id="myFile">
You would then just do:
string filePath = #"C:\MyFiles\Test.jpg";
driver.FindElement(By.Id("myFile")).SendKeys(filePath);

Related

.NET Windows Form - Interact with a website's filedialog opened from webview2

I'm using webview2 in a .NET Framework Windows Forms application to automatically upload files to a website. The site only supports uploading one file at a time and we often have to upload hundreds of files. I used java script in the webview2 to execute the steps for the upload.
My problem is that to specify the file to upload I have to interact with a file dialog box that's opens on a button click from the website within the webview. I'm currently populating the path of the file to upload in the filedialog using SendKeys, which I think is a poor process.
Is there a way that I can interact with the filedialog that is created from the website through my Windows Forms application? If so how would I get started in figuring out how to do that?
Here's more detail on what I'm currently doing:
In the webview, to begin an upload you have to click a few checkboxes to choose upload options, which has to be done every time, so I run these commands first:
await webview2.ExecuteScriptAsync($"document.getElementById('SelectedTypeId').click();");
await webview2.ExecuteScriptAsync($"document.getElementById('AutoAcceptChkbox').click();");
Then, there's a command to click an upload button, which opens a file dialog:
await webview2.ExecuteScriptAsync($"document.getElementById('uploadFile').click();");
From here, I get the filedialog and use SendKeys.Send("Filepath to file") and SendKeys.Send("{ENTER}") to populate the file path and select the file.
Once the filedialog closes I have an additonal command to click the import button to begin the import.
The issue I have is with the filedialog. It's obviously not a great process to use SendKeys to populate the path to the file. Also, I have to make the application "wait" around the sendkeys actions so that the filedialog has time to come up and close out before moving forward, I'm betting at some point there will be errors if the application isn't waiting long enough.

Get file system by angularjs or jquery

I'm using angularjs and spring framework to generate jasper report and save report to file system by
File newFile = new File("/home/person/report");
So the reports saved automatically to this path.
What I want to do is, make update that let user choose the path of the report by dialog box that enables user to navigate the local file system and select the desired path.
I want to get the path from angular and send it by REST service.
Accessing files directly is not supported.
If you insist doing that you can write a browser extension that does these operations or porting your web to a native-web application using frameworks like Electron
However,
You can (and should) use file uploads ...

Access Specific PDF Page in WPF WebBrowser Control

I have a webbrowser control in my application that is used to display pdf files that have been created in iTextSharp and are stored locally on the hard drive.
I would like to be able to navigate the file (next, previous, first, last, toc) from my application rather than using the inbuilt nav of the reader in the browser.
I have seen that you can navigate to specific pages by using
Browser.Navigate("filename.pdf#page=?);
This works the first time but when trying to navigate to a different page, it makes the browser disappear completely with no errors. However, I can reload the file without problem if I don't have the #page=? suffix on the file url though. Any ideas on this?
Alternatively, is there anyway in iTextSharp of adding something to the file to allow for it to be navigated from an external command?
All the official parameters that can be used to navigate through a PDF using parameters in the query string after the ? character are listed in a document published by Adobe: Parameters for Opening PDF Files
You already mentioned the page parameter. Another option could be using named destinations: nameddest=destination. In this case, you need to add the anchor with name destination to the file using iTextSharp.
Note that not all viewers implement these parameters. Adobe supports them in Adobe Reader and in the Adobe Reader plug-in, but there is no guarantee that they will work in pdf.js (Firefox), Pdfium (Google Chrome),... If your browser disappears when using an open parameter, you may have hit a bug in the browser or the viewer plug-in that causes the browser to crash. iTextSharp nor iText can crash a browser ;-)
There are no other ways you can navigate a PDF from an external application. The only thing you can do, is to add JavaScript to the PDF so that it always opens at the same page. This is done using an open action. I don't think this solves your problem as it would mean that you have to change the PDF file every time you want it to open at a different page.

Silverlight and XtraReports - opening generated PDF report in new tab in browser

I'm trying to send a PDF file from a WCF to silverlight client. PDF is generated by DevExpress XtraReports (in method XtraReport CreateReport(string reportTypeName, RootGenericReportParameterContainer reportInformation)).
Acually PDF is saved somewhere on clients computer after choosing save path in file save dialog - DevExpress takes care of everything - but I don't have a clue how to open the PDF in new tab in browser.
And here is another problem. Silverlight 4 has no access to local file system right? So information about local PDF location is useless. Maybe it would be better to save the PDF in WCF and send a link to it to the client - but how?
I would first question why you need to send the file to the Silverlight client. Get rid of that requirement and the solution becomes much simpler. Silverlight can provide a link that opens a new browser tab. That link would be handled by the web domain, processing it as an HttpHandler, generating the PDF file for the browser. Your PDF url doesn't have to reference a physical file, you can still generate it on request, handle querystring values, etc... Lots of different ways to do this.
Seems that the question isn't really about DevExpress or Silverlight - you're just looking to open a [document of some kind] in a new tab. Each browser natively handles things differently, and users can change tab handling to whatever they want. And (as you mentioned) once the user has downloaded the file, you no longer control it.
Your best bet (and the way I do it) is probably to have a link pointing to a handler/file using "target='_blank' " in the anchor tag on the webpage. From the server side, you would want to set the "Content-Disposition" header to "Inline" to indicate to the browser that the document should be displayed in place instead of downloaded ("Attachment").

Silverlight 4: Read in file from local system without being OOB?

Any tips/tricks on how to read a file from the local system dynamically in silverlight 4 without having to be out of browser?
Impersonation? Toggling app elevated trust on/off programmatically?
Or is this simply impossible to do without being out of browser?
As it stands I have a Pegasus ImageGear PDF viewer that I feed a "LoadDocument" method a stream of a PDF file.
This of course works fine if the file is an application resource and compiled with the application.
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/TestRIA;component/SampleData/test.pdf", UriKind.Relative));
docViewer.LoadDocument(resource.Stream);
This silverlight application will be hosted through a website deployed on a server. This server has a partition specifically for repositories of files. These files in the "D:" partition are currently accessed by an ASPX web application and displayed in a PDF viewer. We're moving to silverlight, so as the user selects the grid row representation of that file in the repository, I know the "NAME" of the file. The repository's location is a string held in the database configured in another application. I simply concatenate the file name to that repository path and have the filepath.
Again, the 3rd party viewer's "LoadDocument" method has two overloads. One that accepts a stream of the PDF and one that accepts the filename of the PDF.
For example I have a click event that feeds the name of the document, and I already have the root path to concatenate it to:
void testButton_Click(object sender, EventArgs e)
{
string docName = myListBox.SelectedItem.Content.ToString();
docViewer.LoadDocument(repositoryPath + docName);
//OR using stream
Stream s = new FileStream(repositoryPath + docName, FileMode.Open);
docViewer.LoadDocument(s);
}
You cannot programatically interact with an arbitrary file in SL4. Period. There's your section of isolated storage you can read from and write to files, but that isn't what you're looking for. You can read and write files through the file open dialog, but again I think that's not what you want.
The only way out of the Silverlight sandbox is the network. You have to have to talk to a non-sandboxed service to do this. SL has OK support for Web Services, Http, and even sockets. This seems doable for you since you're talking about the file being somewhere on a "server".
I remember reading that this will work without elevated trust only if the code is initiated with a user action such as button click.
http://msdn.microsoft.com/en-us/library/ff382752%28v=vs.95%29.aspx
For security purposes, if a Silverlight application is a sandboxed
application, file and print dialog boxes must be user-initiated. This
means you must show them from a user-initiated action, such as the
click event handler for a button.
As a possible workaround, if your Silverlight app is backed by a service, you could make the reading/writing of the file be handled by the service, assuming it has access to the location(s) and sufficient rights in the destination folder(s).
Create an OpenFileDialog box and you can return stream(s) to the selected file(s).

Resources