3

In this question there is an example how to request XPCOM access from Javascript:

How to create a file using javascript in Mozilla Firefox

 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

I was hoping to find a way to enable XPCOM access in similar manner for my Selenium test scripts, so that Javascript could directly write RAW image data to a file. This actually continues my previous questions how to extract pixel data from in optimized manner:

Firefox, Selenium, toDataURL, Uint8ClampedArray and Python

What I am hoping to achieve

  • Enable XPCOM access for Javascripts run through Selenium

  • Render image on Canvas

  • Read canvas pixels as raw image data (public API should be available on the canvas itself)

  • Write RAW image data to a file using XPCOM interfaces in a known path location

Note: PNG etc. encoding is unaccetable. This must be raw data for the speed, as it will be directly feed to a video encoding,

Community
  • 1
  • 1
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435

2 Answers2

3

It seems to me that this blog post is what you are looking for:

Save to local files with XPCOM for Selenium Firefox Chrome

function saveFile(fileName, val){
   try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   } catch (e) {
      //alert("Permission to save file was denied.");
   }
   var file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
   file.initWithPath(fileName);
   if (file.exists() == false) {
     //alert("Creating file... " );
     file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
   }
   var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
            .createInstance(Components.interfaces.nsIFileOutputStream);
   outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 ); 
   //UTF-8 convert
   var uc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
     .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
   uc.charset = "UTF-8";
   var data_stream = uc.ConvertFromUnicode(val);
   var result = outputStream.write(data_stream, data_stream.length );
   outputStream.close(); 
}

You'll have to adapt it for your situation (RAW datatype) but that's basically it!

Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
0

I don't know much about Selenium, but IF you can install an extension on the version of Firefox that will be running the page, you can inject your own script into the page when it loads. See this SO answer

Community
  • 1
  • 1
rav
  • 686
  • 5
  • 7