Using selenium drivers to Print to PDF - selenium

I am struggling with a problem.
I need to download PDF file when the Print dialog appears.
Here is an example of the dialog
I have tried both the chrome and firefox drivers. But unfortunately still cannot get it working.
I have found the following preference for the firefox
fp.setPreference("print.print_to_file", true);
But it seems that it doesn't work.
It doesn't even get applied into the configuration variables set.
Please suggest any way to automate some actions with selenium and then save to PDF file when this print dialog appears.

Firstly, you need to know selenium is a web automation tool. so it can only operate things belongs to browser. Even that, it can't operate all things belongs to browser, like address bar, favior bar, menu bar of browser.
For most case, the print dialog is not belongs to browser, it supplyed by OS or other software installed on OS. so selenium can't operate it is reasonable.
If your script code by java, you can use java.awt.robot to capture the dialog
and click 'print to file'.
If java.awt.robot can worked as your expect, I think you aslo need to set browser options in your script to give a path for browser to save the printed file, just like when you download file, browse will ask you where to save.
if you not to set that options, i guess it will pop-up another dialog to ask you where to save and this dialog is also not belongs to browser.
it's not easy to use java.awt.robot to set the save path.
Because you need to consider the possbility of script run on different OS, use abosulte path is not workable on different OS. Besides if your script run with seleniu grid, it will make you craze. So if testing print to file feature is not important, i recommend you test it manuall.

You can automate Print to PDF using Testmate as below
http://thetestmate.com/save-as-pdf-in-chrome-testmate-selenium/

Related

How to handle window open prompt(native) in webdriver.io

I am using webdriver.io framework. At my application, I have a button that opens a Windows Open prompt (Native), and there I need to choose a folder/file and click Open.
Then returning to my application and continue.
Is there a way to move focus on to this window and control it (Select a path and click Open)?
If not- Is there any other solution someone can offer?
The short answer is probably not, you can't control native dialogs from webdriver.
If it's a file input type for which you want to set a file, you might possibly use webdriver to evaluate a piece of javascript to set the HTMLInputElement.files value of the input element.
This works in modern browsers as mentioned in the mozilla docs

Selenium select folder windows dialog

]1
There are many examples on what to do with file upload, however I could not find anything in selecting folder. Any ideas how to handle this ?
UPDATE:
I have tried 3 different ways but none worked :
the first is using sendkeys(). This does not work because there is no input field. This dialog opens when you click a button.
The second is using AutoIt, which is used in the uploading files dialog. It causes an error when i use it because I am not uploading a file but just selecting a folder.
the third is robot, where I copy the path I want to the clipboard and try to paste it with robot. It did not work for me.
Update 2:
If you open chrome://extensions/ on chrome browser and tick developer mode you will see a load unpacked extensions buttons. This is the button that when you click you get this windows dialog:
There are different ways by which you can handle File Upload :
Way 1:
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
Way 2:
By using Robot Class API:
driver.findElement(By.By.name("uploadfile")).click();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_KP_DOWN);
r.keyRelease(KeyEvent.VK_KP_DOWN);
r.keyPress(KeyEvent.VK_KP_RIGHT);
r.keyRelease(KeyEvent.VK_KP_RIGHT);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Hope it will help you
Selenium support is for Web browser and not for windows dialogs.
the only option for you is to write automation using coded UI or Autoit.
Selenium only works on Web browser, You can use AutoIT but I see you already tried it. In that case I would suggest you to use Sikuli.

How to handle external applications in a selenium-test?

I am writing a test that verifies that a warning to the user before the user opens a document. If the user says that they still would like to open the document, the document opens in an external application (pdf or word).
But now I have an external application over the browser window, and it messes up for other tests.
So, what are the best practices around this kind of issue? Rewrite of the appliction to allow for not opening documents in test?
Added description:
The problem is twofold.
1) It starts processes (word and acrobat) that fills the desctop and requires resources from the test-slave
2) The external process seems to interfer with other tests since (guessing here) it is located over the browser window.
what i understood from your post is, the document(word/pdf) is opening in the browser window hence you are not able to proceed with further steps. If so, you can verify the Title to make sure the document is opened in browser window and can navigate back using below snippet.
driver.navigate().back();
Hope this helps.
What I understand from the line
'But now I have an external application over the browser window, and it messes up for other tests.'
is that once user clicks on open button a new window opens up (Window based application) since you have mentioned PDF or Word.
You can use robot class in such cases, below code snippet will close the current active window:
Robot rbt = new Robot();
rbt.keyPress(KeyEvent.VK_ALT);
rbt.keyPress(KeyEvent.VK_F4);
rbt.keyRelease(KeyEvent.VK_ALT);
rbt.keyRelease(KeyEvent.VK_F4);
Make sure you deal with sync issues properly so that intended window is closed instead of AUT.

Modifying the file upload dialog of firefox

Is there any way to modify the file upload dialog in firefox with an extension (XUL or Javascript) ?
I'd like to give the user the possibility to encrypt the file(s) before uploading them to whereever (facebook, gmail, gmx, ...) and I thought the easiest way is to add a checkbox to the file upload dialog and check for that.
There is no native support for something like that.
So you have to find a workaround to provide this possiblity.
The easiest way would most likely be to simply add the checkbox near the file upload button instead of directly in the dialog that shows up. Most likely it would even be noticed better like this.
An other way would be to create your own file dialog using other technologies like a java applet or some flash content. The advantage of this way is clearly, that you can handle all operation that have to be done for the encryption directly inside that applet. Then your firefox plugin would only have to trigger the loading of your applet for each file upload dialog.
No. Internally nsIFilePicker interface is used to create this dialog - as you probably see there isn't a whole lot of configuration options. That's because Gecko will use the system's file selection dialog where available (Windows, Mac OS X) and changing this dialog is non-trivial to say the least. So even if you were creating this dialog yourself - you wouldn't be able to add a checkbox to it. You need to find some other solution.

How to test File Download dialog box using cucumber/capybara

I searched around and couldn't find how to test a file download box using capybara/cucumber?
The following image asks the question much clearer.
This was similar to another question I just answered, hope it helps Anybody have idea how to test file download using cucumber?
#Millisami Capybara::NotSupportedByDriverError Fixed for me!
What i had to do is removing the #javascript tag from my cucumber test, which was included. I mean:
#search
Scenario: Recieving a file
...
instead of
#search
#javascript
Scenario: Recieving a file
...
Hope it helps :-)
The download box is a function of the browser. Capybara simulates a browser but without all the UI etc.. (e.g. it looks like a browser to your application, so using it you'd mostly skip over the whole file download UI stuff. It would look to the browser like someone did whatever they needed to in order to tell the browser where to put the file and start the download)
If you are trying to test a download box, (beyond clicks needed to start the download) you are now testing the browser, not your application. As yourself if that's part of your charter and worth your time.
To actually test the download box you are going to have to have a browser instance going, and use a tool like Firewatir/Watir or Selenium, to actually 'drive' the browser, and some other gem to actually automate up at the OS UI level (on windows we usually use autoit) in order to click things and fill in values of the browser's file download UI.