Upload action using Nightwatch.js - node.js

I am trying to create an action for nightwatch that will upload files to a given upload form. Is it possible to create that kind of action in nightwatch? The nightwatch is integrated into a nodejs application and is running in a selenium webdriver.

Use AutoIT to create an executable for the upload action. Execute the executable using the exec function of the Node. Upload.exe is the executable file compililed from the AutoIt script.
exec('START "" .\\src\\Resources\\Upload.exe', function(err) {
console.log('err: ' + err);})

This question is already asked here.
The approved answer gives this solution:
.setValue('input#fileUpload', require('path').resolve(__dirname + '/testfile.txt'))
You can take a look here at the discussion that led to this fix.

Related

NodeJS open local file using the default preview

I am trying to create a desktop app using electron which includes a function where a user can open a desired file saved in the local storage. Sine I am using MacOS, I want to use nodejs to be able to open the file (e.g. PDF doc) in the default preview software. is there any way to do this?
Thank you in advance.
A portable way is to make use of the Electron's shell API, specifically the shell.openPath method:
const { shell } = require('electron');
shell.openPath("/fullpath/to/file");
It is available both in the main process and in the renderer process, and it can also be used to open a folder in the Finder.
if anyone is wondering I found a way of using
const { exec } = require('child_process')
exec('open ~/path/to/file')
this used command shell (terminal) to open it. If there is any other better way to do it please let us know!

Uploading a file with Nightwatch and Browser Stack / Selenium Grid

I'm trying to upload a file in a nightwatch test that is being run on Browserstack in IE11. Here is what I have:
browser.execute(function (data) {
const el = document.querySelector('input#fileUpload');
el.style.display = "block";
el.removeAttribute('multiple');
}, []);
browser.setValue('input#fileUpload', require('path').resolve(__dirname + '/upload-files/test-doc.pdf'))
The error is a file not found issue, it works fine when being run on Chrome locally. Searching around, it seems like I need to upload the file to BS first, I found this gist and this PR but neither worked for me.
Since the file is not present on the machine of BrowserStack you might be encountering this error. You will need to use the 'setFileDetector' method provided by Selenium to upload a local file to remote grid. More details can be found on their documentation here - https://www.browserstack.com/automate/node#enhancements-uploads-downloads

Trying to write to a json file using Node fs.writeFile

I hope I'm saying this correctly. What I'm trying to do is write to a json file using fs.writeFile.
I can get it to work using the command line but what I want to do is call a function maybe a button click to update the json file.
I figure I would need some type of call to the node server which is local port 8080. I was researching and seen somebody mention using .post but still can't wrap my head around how to write the logic.
$(".button").on("click", function(event) {
fs.writeFile("./updateme.json", "{test: 1}", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
Using jQuery along with fs? Wow that could be great! Unfortunately that is not as simple as that!
Let me introduce you to server-side VS client-side JavaScript. Well actually there are a lot of resources on the net about that - just google it, or check the answers to this other StackOverflow question. Basically JavaScript can run either on a browser (Chrome, Mozilla...) or as a program (usually a server written in NodeJS), and while the language is (almost) the same, both platforms don't have the same features.
The script that you're showing should run in a browser, because it's using jQuery and interacting with buttons and stuff (aka the DOM). Can you imagine what a mess it would be if that script could interact with the file system? Any page you'll visit will be able to crawl around in your holiday pictures and other personal stuff you keep on your computer. Bad idea! That is why some libraries like fs are not available in the browser.
Similarly, some libraries like jQuery are not available (or simply useless) in the server, because there is no HTML and user interaction, only headless programs running.
So, what can I do to write a JSON file after a user clicks on a button?
You can set up:
A NodeJS server that will write a JSON file
Make jQuery call this server with the data to be written after the user clicks on a button
If you want further guidelines on this, tell me in the comments! I'll be ready to edit my question so as to include instructions on setting up such an environment.

How to download and write a jar file in Node.js?

So I'm working on a Minecraft launcher (because why not, good experience), and I'm stuck when it comes to downloading the libraries.
I have a valid jar URL here. When you download it in the browser, it works fine. But, when you download it with Node.js, 7-zip gives this error when trying to open it:
An attempt was made to move the file pointer before the beginning of the file.
I'm using a module called snekfetch, but I've also tried it with request. Both items gave the same issue. Here's my current test code:
request.get('https://libraries.minecraft.net/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar').then(r => {
fs.writeFileSync('./mything.jar', r.body);
});
Am I doing something wrong to download the jar file?
Okay, so now that I've seen this answer, I need to modify the question. I've gotten it to work using pipes, but I need inline-code because this is a for loop that's downloading (hence my usage of writeFileSync, and in my actual code I use await for the request). Is it even possible to download and write without piping?
It turns out this is an issue with the snekfetch library. Switching to snekfetch v3 fixed it.
You can check out the status of the issue here.

How to automatically run .sh after a file is uploaded

I am new to Sails/nodeJS. I am trying to create a system that will automatically run a .sh after a video is uploaded to the server (local file system). I understand from Sails documentation that file uploading is done in Controller file, and what I want to do is to trigger a .sh after that. I need to constantly read the output from .sh and return it to the backend.
Anyone has any idea how to implement this?
Thanks!
You always can use https://www.npmjs.com/package/exec
Simple implementation:
var exec = require('exec');
exec('PATH TO YOUR .SH FILE', function(err, out, code) {
// Process to the next action within the callback function.
});

Resources