Use Firefox 'print' or 'save as' features using Selenium WebDriver - firefox

I would like to programatically instruct Firefox to visit a list of URLs (defined in a text file, for instance) and for each of them save the page to disk or print it.
I know Selenium provides a feature to capture a screenshot of the page, but I would like to know if it's possible to use browser's native saving and printing features.
If Selenium does not provide such features, would any other tool allow me to define a script to be executed by Firefox and achieve similar results?

It is possible to enable silent printing in firefox to print to the default printer, bypassing the print dialog.
The required firefox preference is print.always_print_silent, and can be setup with selenium like so:
import org.openqa.selenium.JavascriptExecutor;
/* ... */
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("print.always_print_silent", true);
WebDriver driver = new FirefoxDriver(profile);
Now simply navigate to a web page and call print using javascript:
driver.get("http://www.google.com");
((JavascriptExecutor)driver).executeScript("window.print();");
Additionally, couple this with a free PDF printer such as novaPDF to print without displaying the Save as dialog and automatically save a PDF to a predefined location.

If you are fine with png format, you can take full page screenshot.
import selenium.webdriver
import selenium.common
options = selenium.webdriver.firefox.options.Options()
# options.headless = True
with selenium.webdriver.Firefox(options=options) as driver:
driver.get('http://google.com')
time.sleep(2)
root=driver.find_element_by_tag_name('html')
root.screenshot('full page screenshot.png')

Normally you would do this using Sikuli API. The open source community (a.k.a. Mozilla foundation) is working on a project called Marionette that supposedly will enable you do do these things without using screenshot matching but it's still alpha and they are still working on it and Chrome and IE haven't signed onto it yet.
It should be noted that on native file downloads, you don't really need to test the browser functionality of the already well tested save-as dialog. What Selenium testers usually do is just download the file with Apache HttpUtils or something similar and just bypass the browser on the download step. Then you don't need to use the Save-As dialog and it will work cross-browser. Just use selenium to get the download URL and download it with Java code instead.

Maybe this can helps you...
https://stackoverflow.com/a/64987078/6003328
I made this using python...
Basically, I change the about:config settings for each print, adjusting the filename of the pdf to be created, and setting the printer as always_print_silent: true...

Related

Firefox command line print

Have a requirement to print html or html from vb6 application using only firefox as we have already for IE. So, need command to print using firefox.exe.
Something like this firefox.exe -print http://www.google.com which is not working. Is there anyway to do this? Thanks for your help.
There is no built-in support for a -print command line switch. See this document for the different command line switches supported for the Firefox browser. Support for such functionality would most probably have to be implemented through an extension.
If no extension exists that currently offers what your looking for, a Google search yielded this mozillaZine forum thread: "Printing to file from Commandline?" You could have a look at the discussion, or download and modify the referenced add-on from the source link.
Or else, you could always create your own application (probably not in VB6) by embedding the Gecko layout, but since the Mozilla killed the embedding API a while back, you'll have a lot of integration work to do (or else you could go with another layout engine like Webkit).
You can use the built in webbrowser control to print html via IE: http://www.vbforums.com/showthread.php?384076-Webbrowser-Control-Tip-and-Examples
If you must use firefox, there was a firefox active x control but I have not used it and dont know the status:
mozilla firefox activeX
or
http://www.iol.ie/~locka/mozilla/control.htm
or
https://bitbucket.org/geckofx
It also looks like there is a webkit floating around online (google it)

Firefox - How to enable an HTML (with Javascript) file to save itself locally?

Firefox - How to enable an HTML file to save itself locally?
I use Firefox to open and edit TiddlyWiki.html files.
https://github.com/Jermolene/TiddlyWiki5
These are HTML with a Javascript app packaged together in one file.
They also have this Firefox extension called TiddlyFox that can enable the TiddlyWiki.html file to save itself locally in the file you just opened. It first asks if you want the this ability to be enabled on that particular file and if you click Yes it works.
I was wondering how this behavior is achieved via the Firefox Extension? (ex source code here: https://github.com/TiddlyWiki/TiddlyFox )
Google Chrome has the filesystem api, i dont know if this is how Tiddly does it but this topic solution shows how to do it in chrome:
http://stackoverflow.com/a/13779352/1828637
Noitidart comment Feb 10 '15 at 3:38

Export all http requests on a specific page to txt/csv

I use SIEGE to test my web server performance. For a more realistic test the best way to go would be to have SIEGE hit the web page (website.com/our-company) and all static assets (.css, .js, .png, .jpg). Everything that you see on the firefox / chrome debbuing tools, except of course from resources loaded from external servers (cdn.facebook, apis.google.com).
I am running several tests so it is a pain to manually collect all asset urls. Is there a tool that I can use to load a web page and export the url for everything that was loaded?
This is firefox debugging. If I could export this to txt or csv, it would be perfect.
I tried CURL on debian CLI but I am no experct. Any tool will help, it does't have to be a plugin of Firefox / Chrome.
Best regards.
In Chrome you can export these data to a HAR file (it's JSON based) in one click. Go to "Network", right click and choose "Save as HAR with content".
Here's a free command line application to convert HAR files to CSV. Hope it helps.
http://www.yamamoto.com.ar/blog/?p=201
EDIT: added the project to GitHub:
https://github.com/spcgh0st/HarTools
On Windows you could use HttpWatch to do this with the free Basic Edition in IE or Firefox:
http://www.httpwatch.com/download/
The CSV export function will export the URLs and other fields to a CSV file.
** Disclaimer: This was posted by Simtec Limited the makers of HttpWatch **
Had the same requirement of exporting HAR files from Chrome DevTools or Firebug to do load testing with siege. Additionally, I wanted to replay POST requests too.
Choose one of these solutions:
hardy # https://github.com/nbibler/hardy - ruby script
har2siege # https://gist.github.com/photopresentr/7974747 - node.js (my script)
Nevermind.
Just fond out the very nive LiveHtttpHeaders extension for firefox.
Best regards.
As you guys know, the HAR file format is a JSON file. So... I looked for a JSON to CSV converter and found this:
https://json-csv.com/
This worked for my HAR file that I got from GTmetrix.com. Enjoy!
You can Export all Http requests from Chrome Developer console by going to the Network tab
select one of the requests in Network Tab
press Right Mouse Button
from PopUp menu select Copy -> Copy all as Har (Curl/Har/etc)
paste into file

Using Headless FireFox to Save All HTML files using command line in Linux

Using shell_exec with Xvfb and FireFox currently to capture screen shots. However, need to download the entire html (e.g. Save Page As --> Web Page complete.) to a directory using shell_exec. Have looked at all the different option available in the Mozilla Developers Forums but have not been able to figure out how to do this.
This code appears to be what I might need but where and how is this implemented so it can be accessible in shell_exec?
var file = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\filename.html");
var wbp = Components.classes['#mozilla.org/embedding/browser/nsWebBrowserPersist;1']
.createInstance(Components.interfaces.nsIWebBrowserPersist);
wbp.saveDocument(content.document, file, null, null, null, null);
The Above Code Source
void saveDocument(
in nsIDOMDocument aDocument,
in nsISupports aFile,
in nsISupports aDataPath,
in string aOutputContentType,
in unsigned long aEncodingFlags,
in unsigned long aWrapColumn
);
The Above Code Source
There is a Stackoverflow manual solution here but it does not address shell_exec:
How to save a webpage locally including pictures,etc
There are few options that I know of, but none that I know are fitting your question exactly..
Open firefox http://yoursite.com from shell, then send keystrokes to firefox using xte or similar method. (This is not headless mode though.)
Download using wget. It can work in recursive manner. Or alternately you can parse the HTML, if it is quite simple web page. If you need to submit form, use curl instead of wget.
Use greasemonkey addon & write a script, which would get loaded on http://some-fake-page.com/?download=http://yoursite.com & then open firefox with that fake-page url.
Develop your own firefox addon to do above work.
There may be other better options for this as well, but I don't know them.

Turn off built-in rss reader in Firefox 13

A couple of years ago, I designed a bunch of automatic tests for a webapplication using Molybdenum. Some of these checked the data showed in a rss feed through an xml parser. The test required the browser to show the rss as a simple xml file.
At that time I managed to turn off firefox built-in rss reader changing a parameter in the feedconverter.js file. With the major updates in Firefox (4.0 to now) this file is missing and the developers seems to have left no room for this kind of trick.
Do you know a native way to turn off the built-in rss reader?
P.S. = Solutions using the view-source: URL suffix does not work in this case, because the open command answers Failure: Access to restricted URI denied
Things that don't work:
setting "Web feed" content handler to "Preview in Firefox" (shows formatted HTML version, not plaintext XML)
setting browser.feeds.handler to "Reader" (this is an about:config alternative to above) or anything else. There's no plaintext option.
editing mimeTypes.rdf in one's profile folder - Firefox seems not to care about feed settings there
adding a new web content handler with URI view-source:%s - I think it was pretty close, but Firefox escapes the forwarded URL and refuses to open it
storeHtmlSource as it is too smart and does return the code of the formatted page, not the XML source

Resources