Selenide can't download PDF - selenium

I had some trouble using Selenide's download function. This is my flow:
click on button
button opens new tab which shows pdf file and url is:
blob:https://hostname.apps.something.else/9365b3ab-f0ad-45e9-85d2-db7e2fb5fd2e
So basically the new tab is opened in Chrome PDF Viewer.
I tried to click on the hidden download button in the new tab but no luck.
My thinking got me to the idea of when I click that button, this new tab shouldn't open, the download should start. That would mean settings some Configuration preferences like I did below:
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserSize = "1920x1080";
Configuration.fileDownload = FileDownloadMode.FOLDER;
Configuration.downloadsFolder = "./src/pdfs";
Configuration.proxyHost = "30.40.34.82";
Configuration.proxyPort = 8080;
Configuration.proxyEnabled = true;
Configuration.fileDownload = FileDownloadMode.PROXY;
Configuration.browserCapabilities.setCapability("plugins.always_open_pdf_externally", true);
Configuration.browserCapabilities.setCapability("download.prompt_for_download", false);
Configuration.browserCapabilities.setCapability("pdfjs.disabled", true);
The problem is that, this still opens new tab, and I get no such element found
File file = applicationList.printButton.download(30000);

Why do you first set Configuration.fileDownload to FOLDER and then to PROXY? No need to set it twice.
I think you cannot download the file via PROXY because links of type blob: don't fetch file content from the server.
I think you can just remove all these lines:
Configuration.proxyHost = "30.40.34.82";
Configuration.proxyPort = 8080;
Configuration.proxyEnabled = true;
Configuration.fileDownload = FileDownloadMode.PROXY;
Configuration.browserCapabilities.setCapability("plugins.always_open_pdf_externally", true);
Configuration.browserCapabilities.setCapability("download.prompt_for_download", false);
Configuration.browserCapabilities.setCapability("pdfjs.disabled", true);
Selenide method FOLDER should download the file without all these tricky settings.

Try it
System.setProperty("chromeoptions.prefs", "plugins.always_open_pdf_externally=true")

Related

Allow file explorer in Microsoft.Web.WebView2.WinForms.WebView2

I have a Winforms app with a webview but the file explorer do not open when fired inside the webview (works fine on browser) i alredy tried the following :
Dim oEnvironment As Microsoft.Web.WebView2.Core.CoreWebView2Environment
Dim opts As CoreWebView2EnvironmentOptions = New CoreWebView2EnvironmentOptions()
'opts.AdditionalBrowserArguments = "--allow-file-access-from-files"
opts.AdditionalBrowserArguments = "--disable-web-security"
oEnvironment = Await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(String.Empty, System.IO.Path.GetTempPath(), opts)
Await browser.EnsureCoreWebView2Async(environment:=oEnvironment)
The page do nor return any error.
How can i give permissions to open that?
Update:
I have a file picker like this below, if i drag and drop the files it works, if i click "Browse" nothing appen in the webview (on browser all woks fine)

Reloading of Editor in Eclipse plugin development is not showing workbench status bar

I have a requirement to close/open an Editor.
To open an Editor I am using page.closeEditor(..) and to open it again I am using page.openEditor(...) but page is not loading workbench status bar.
Please provide your input.
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
final int MATCH_BOTH = IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID;
part=page.openEditor(input, input.getEditorid(), true, MATCH_BOTH);
part = page.getActiveEditor();
page.closeEditor(part, true);
part = page.openEditor(input,"com.editors.OnlyJavaEditor", true, MATCH_BOTH);
To open a specific editor on a file you just need to do:
IWorkbenchPage page = ... get workbench page ...
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(..path of file...);
// or some other way of getting an IFile
IEditorInput input = new FileEditorInput(file);
final int MATCH_BOTH = IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID;
IEditorPart part = page.openEditor(input, "com.editors.OnlyJavaEditor", true, MATCH_BOTH);

Handle download dialog box in SlimerJS

I have written a script that clicks on a link which can download a mp3 file. The problem I am facing is when the script simulates the click on that link, a download dialog box pops up like this:
Download Dialog Box
Now, I want to save this file to some path of my choice and automate this whole process. I am clueless on how to handle this dialog box.
Here's a script adapted from this blog post to download a file.
In SlimerJS it is possible to use response.body inside the onResourceReceived handler. However to prevent using too much memory it does not get anything by default. You have to first set page.captureContent to say what you want. You assign an array of regexes to page.captureContent to say which files to receive. The regex is applied to the mime-type. In the example code below I use /.*/ to mean "get everything". Using [/^image/.+$/] should just get images, etc.
var fs=require('fs');
var page = require('webpage').create();
fs.makeTree('contents');
page.captureContent = [ /.*/ ];
page.onResourceReceived = function(response) {
if(response.stage!="end" || !response.bodySize)
{
return;
}
var matches = response.url.match(/[/]([^/]+)$/);
var fname = "contents/"+matches[1];
console.log("Saving "+response.bodySize+" bytes to "+fname);
fs.write(fname,response.body);
phantom.exit();
};
page.onResourceRequested = function(requestData, networkRequest) {
//console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
page.open("http://....mp3", function(){
});
You can't control a dialog box. SlimerJS doesn't have API for this action.
Firefox generates a temp "downloadfile.extension.part" file which contains the content. Just simply rename the file ex. myfile.csv.part > myfile.csv
locally if working on a mac you should find the .part file in the downloads directory, on linux /temp/ folder
Not the most elegant solution but should do the trick

c# on button click open pdf in new tab insted of downloading

I have a .aspx page and it has a button on click of which a radwindow opens .The radwindow has a new page with content including a button onclick of which a pdf is created and downloaded.I want the pdf to open in a new tab or window , so i made changes to my code -changed attachment to inline
Hashtable deviceInfo = new Hashtable();
deviceInfo.Add("FontEmbedding", "Subset");
deviceInfo.Add("DocumentTitle", String.Format("TrackitManager - {0}", reportToExport.DocumentName));
deviceInfo.Add("DocumentAuthor", "Trackit Systems Pty Ltd");
ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult result = reportProcessor.RenderReport("PDF", reportToExport, deviceInfo);
if (String.IsNullOrEmpty(filename))
filename = result.DocumentName;
context.Response.Clear();
context.Response.ContentType = result.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Expires = -1;
context.Response.Buffer = true;
//context.Response.TransmitFile("~/Handlers/EnrollmentPDF.ashx");
context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename=\"{0}.pdf\"", filename));
context.Response.BinaryWrite(result.DocumentBytes);
context.Response.End();
But this is opening the pdf inside the radwindow itself.I want it to open in a new window but outside the radwindow.
you have to give the correct header and mimeType to tell the browser to download the file
for that you just have to add this line to replace :
context.Response.ContentType = result.MimeType;
by this line :
Response.ContentType = "application/octectstream";

Print PDF in Website

I have been searching for days for a solution to this problem.
Description : I have a website which loads a PDF dynamically via an iFrame. The PDF is saved on the server and the user of the website can view the pdf on the website.
Problem : Introduce a Print button on website which prints the PDF which was created dynamically and saved on the server.
Is this even possible ? I am looking at a cross-browser implementation as well to make things worse. I have tried n number of JS options from the web but none of them seem to work. I can not seem to get the PDF printed in the same way as it looks. To put it short, I am trying to emulate the print button which appears on the PDF when it is loaded. Is there an option to pass the pdf document from the server to the print dialog box ?
Description : I have a website which loads a PDF dynamically via an iFrame. The PDF is saved on the server and the user of the website can view the pdf on the website.
Problem : Introduce a Print button on website which prints the PDF which was created dynamically and saved on the server.
Solution : I could not find an exact solution to this problem, but here is how I solved the problem -
Create the 'Print' as per req and redirect that to another page which has only the PDF.
Copy the previous PDF & Create new PDF with JS - this.print() such that when it opens up, the print dialog pops up directly to the user.
In the new page -
if ("Location of PDF " != null)
{
sPdf = "Location of PDF ";
PdfReader pReader = new PdfReader(sPdf);
Document document = new Document
(pReader.GetPageSizeWithRotation(ApplicationConstants.INDEX_ONE));
int n = pReader.NumberOfPages;
FileStream fs = new FileStream
("New PDF location",
FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, fs);
// Write to pdf
document.Open();
for (int i = ApplicationConstants.INDEX_ONE; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(pReader, i);
copy.AddPage(page);
}
copy.AddJavaScript("this.print(true);", true);
document.Close();
pReader.Close();
inStr = File.OpenRead("New PDF location");
while ((bytecnt = inStr.Read
(buffer, ApplicationConstants.INDEX_ZERO, buffer.Length))
> ApplicationConstants.INDEX_ZERO)
{
if (Context.Response.IsClientConnected)
{
Context.Response.ContentType = "application/PDF";
Context.Response.OutputStream.Write(buffer,
ApplicationConstants.INDEX_ZERO, buffer.Length);
Context.Response.Flush();
}
}
}
Please note that I am using itextsharp to inject the JS script into the new PDF. Hope this helps someone else. I am trying to find another solution without the usage of itextsharp or any other dll but this will have to do for now.
I am not sure if this will work, but you could try launching a popup window with a special version of your PDF file that opens the print dialog when opened. Then close the popup afterwards. This last part might be tricky since I think there is no clean way to know if the print dialog has been closed.