Automate PDF contents using nightwatch/Cucumber - node.js

I am using nightwatch/NodeJs using cucumber for Automation. I have a lot of areas where PDF's are generated and needs to be verified. Is there any way by which I can verify PDF contents which are downloaded including text/Images etc??
Thanks in advance.

You can use the pdfjs-dist package to read the contents of a PDF. In my experience it has worked most of the time. I have had a few instances where it returned no content even though I could visibly see that the file had text in it. I haven't tried to dig in to see why that was but the majority of the time it has worked for me without issue. Also, I have only used it to verify text. I haven't tried to use it for images so I'm not sure if it can do that. You can find more details here.
This will get you as far as getting all the text into an array. What you do with it from there is up to you.
pdfjs.getDocument('path/to/your/file.pdf').then(function(document) {
document.getPage(1).then(function(page) {
page.getTextContent().then(function(text) {
const allText = text.items.map(function(s) { return s.str }).concat('');
//console.log(allText);
});
});
});

Related

JODIT WYSIWYG editor 3.24.1 - how to call a function and get source filename after a base64 encoded image has been inserted

I've got images inserting into the editor as base64 encoded images (uploader option insertImageAsBase64URI is set to true). However, I'd like to call a function after the image has been inserted and also read the source filename for the inserted image.
I'm new to the JODIT editor, it seems great so far, but I need to tweak it a bit and am not sure how to register an event callback for this, or if there is another/better way. Any help is appreciated!
I think the best solution is to fork JODIT in github and edit the code. For some reason, however, I have been unable to build the code on my mac laptop for at least a couple of reasons (missing file in node module, fixed, and a build error "TypeError: require(...) is not a function" that may indicate circular dependencies in node modules?). Anyway, I found a complete and limited "HACK" for my needs and that is to actually capture the filename when the file is added by attaching an "onchange" handler function to the JODIT instance's file input element. This works roughly as follows (I'm using jQuery):
var selectedFile = null;
function setSelectedFile(){
$('.jodit').find('input[type="file"]').removeProp('multiple');
$('.jodit').find('input[type="file"]').on('change', function(){
var files = $(this).prop('files');
selectedFile = files[0].name;
});
});
$('.jodit').find('button[aria-label="Insert file"]').on('click',
function(){
setSelectedFile();
}
);
I run something like this after the page has loaded. This works only for the "change" event (where you select a file directly) and I could not figure out how to read the filename after a file is "dropped". Dropping a file does not seem to trigger the "change" event in the file input element. If anyone knows how to get the filename of a dropped file for the JODIT editor I'd appreciate sharing. I will update this if I get around to fixing that.

Save a tensorflow.js model in node

I am trying to save a tensorflow.js model I have as a json file but it's not working.
var exampleModel = {input:1,name:'model'}
fs.writeFileSync('model.json', JSON.stringify(model,null,2))
//=> '"{\"input\":1, \"name\": \"model\"}"'
It's not in json format. I wasted a lot of time trying to manually create a json but that didn't work either and I will have to redo it by copying it to a normal object or something.
Seems noone knows anything about this. The problem was that I was using node and I needed to require "#tensorflow/tfjs-node" underneath my tfjs require line. I had to search for this page specifically as there wasn't anything about it in their documentation: https://js.tensorflow.org/tutorials/model-save-load.html
Here's another helpful link: https://github.com/tensorflow/tfjs-node

ElectronJS - adding folder to favorites [macOS]

I am wondering if there is a way to add a folder to Favorites using Node?
I've found this issue, but the solution does not work anymore.
Basically the file that I'm interested in is located in:
/Users/USER_NAME/Library/Application\ Support/com.apple.sharedfilelist
The thing is I do not know how to modify it...
You can use try os module. Very easy to use.
Here is an example:
const home = require("os").homedir(); // This will get your OS based Home directory
const dirToSave = `${home}/Desktop/output.csv`; // Now you can add file or folder to any tree directory of home directory.
fs.writeFile(`${dirToSave}`, {csvData}, function(err, stat) {
if (err) throw err;
console.log("file saved");
});
There is a file that you need to edit
You can edit the sidebar on Mac using the com.apple.sidebarlists.plist preference file. The items will be in the favoriteitems dictionary.
The items you see are all set to being AlwaysVisible. You need to edit that file to add your own file.
I will give some links that might help you.
About the com.apple.sidebarlists.plist file, http://www.thexlab.com/faqs/finder.html
Finding the com.apple.finder.plist, https://discussions.apple.com/thread/4122582
Another post, https://apple.stackexchange.com/questions/139305/how-can-i-add-new-folders-to-the-favorites-in-the-finder-sidebar
About editing a file in Electron
There are tutorials about how to edit files with Electron, you can start from this example,
https://ourcodeworld.com/articles/read/106/how-to-choose-read-save-delete-or-create-a-file-with-electron-framework
What I've done is I wrote a python script based on the resource that #Royson posted in this question's comments (click).
It's available under a gist:
https://gist.github.com/Ancinek/6d6e34791c5a8674275560ae118848c7
Currently possible to add/remove one file at a time.
I will be creating a binary for this so it can be run without the need to installing the pyobjc on the user's machine - will post the update soon after I find out how to actually do this.
Hope this helps somebody!

Use node `fs` module on webpack compile

A bit about what I'm trying to achieve.
I'm building dev library that shows the list of files. And I want to set file color depending on when file was changed.
So, as a result of generation, I want an array like this:
[
{
lastChange: '2009-06-29T11:11:55Z',
fileContents: {name: 'VmSome'},
},
// ...
]
This is meant to work in browser environment. Meaning all file related information should be included into bundle.
Current progress
At the moment I'm not quite sure whether that's even possible.
I'm getting a list of files via webpack require.context:
require.context('./tree', true, /.js$/)
This gives me access to file content and path. But not to anything else.
Thanks for your attention.
I would first try modifying webpack-file-list-plugin. As of now, it creates a JSON that gives all files packed, their name and some more info... You could definitely add your code to fetch more information to the JSON.
https://github.com/object88/webpack-file-list-plugin/blob/master/src/index.js

SAP, how to checkIn document files?

I want to create a new document in SAP. Additional I have some files which belongs to this document, these files I want to upload to the SAP knwolegde base.
I'm using BAPI_DOCUMENT_CREATE2 to create or BAPI_DOCUMENT_CHECKIN2 to add files to a document info data. Every thing works fine, except file upload or checkin.
I'm using the DOCUMENTFILES table. I add a row for each file, currently I set only three fields:
row["STORAGECATEGORY"] = "DMS_C1_ST";
row["DOCFILE"] = "c:\temp\bom.pdf";
row["WASAPPLICATION"] = "PDF";
BAPI erro message:
"Error while checking in and storing c:/temp/bom.pdf"
I set the parameter
PF_FTP_DEST = "SAPFTPA";
PF_HTTP_DEST = "SAPHTTPA";
I have looked in the log data (slg1). I found following entry:
ERRMSG: Error in opening file "..." for reading (No such file or directrory)
V1: SCMS_DOC_CREATE_FILES
V2: 13
It would be nice if anybody has an idea and could bring some light in this issue.
Thanks in advance
Thomas
Remember that BAPIS run inside the application server and are not allowed to make any assumptions about the client side. This also means that they can't call back to the SAP GUI and upload a file from there. C:\temp\bom.pdf has to be a file on the application server, not your local machine!
Have you considered using
row["DOCFILE"] = "bom.pdf";
row["DOCPATH"] = "c:\temp\";
Let me know how it goes, or if you have already done with it then please paste your solution.

Resources