Uploading a file with WebdriverIO and Cucumber - cucumber

I'm doing some simple automation scripts using WDIO and Cucumber. I'm using test website for "uploading a file" example. I was looking for some solutions but no help was satisfactory.
I tried to follow these steps:
https://webdriver.io/docs/api/browser/uploadFile.html
https://webdriver.io/blog/2019/06/25/file-upload.html
My final "When" step looks like:
When('I pick example file from path', () => {
const input = Upload.fileUpload
const submitBtn = Upload.fileSubmit
const filePath = path.join(__dirname, '/file_path_on_desktop/');
const remoteFilePath = browser.uploadFile(filePath);
input.setValue(remoteFilePath);
submitBtn.click()
});
And I have received error
ENOENT: no such file or directory, open '/path_to_project_folder/file_path_on_desktop/'
which is weird.
Maybe I just did smth wrong due to Cucumber, or it's just not supported with Cucumber? However I tried without it and I also had same issue.
Error shown at this line
const remoteFilePath = browser.uploadFile(filePath);

I've got a solution.
Maybe I missed it somewhere, but in that case, __dirname shouldn't be here.
Or move the file from desktop to repository folder, then __dirname may stay as it is.

Related

How can I read a pdf file with express and node

I'm trying to read a pdf... and my idea is to convert it to a text. I have read the pdf-parser documentation and I don't understand why it is giving me this error, has anyone ever used pdf-parser?
Has anyone had this error?
It would be very helpful, I have never worked with it and the videos I watched use it very easily, but it breaks the code for me.
const url = require("./prueba.pdf");
const pdf = require("pdf-parse");
const fs = require("fs");
const pdffile = fs.readFileSync(url);
console.log(pdffile);
pdf(pdffile).then(function (data) {
console.log(data.text);
});
The path ./prueba.pdf is not correct, unless you run the node command from the ..\api\src\routes subdirectory, which I doubt.
Unlike paths in a require command, paths in an fs.readFileSync command are interpreted relative to the directory where you started the node process.

Jest test doesn't create the files the normal program would

I'm using node and puppeteer to load a page, get its content and then create a screenshot if it. At the end of the run function I have the following lines
var content = fs.writeFileSync(outputFilePath, processedContent);
var screenshot = page.screenshot({path: '../output/whatever.png', fullPage:true})
browser.close();
This works when running the node app. For testing I am using JEST
And when trying to run the JEST test that checks for the screenshot:
it('Run should take a screenshot', async() => {
const runResult = await run();
const screenshot = fs.readFileSync('/app/output/whatever.png');
expect(screenshot).toBeTruthy();
})
I get the following error ENOENT: no such file or directory, open '/app/output/whatever.png'
I'm having a hard time understanding why in the normal app flow the program creates the files when running but in the tests it doesn't. As additional info the entire thing runs in a Docker container
It is most likely because you are using an absolute path instead of a relative path in your jest test.
So instead of
const screenshot = fs.readFileSync('/app/output/whatever.png');
write
const screenshot = fs.readFileSync('./app/output/whatever.png');
to use a relative path
Also keep in mind your relative path should be from the the project root

My cli engine(npm package) cant find path to read file when install on another folder

I am building an app that auto completes some type of file. When i run node index.js in the program folder i get the correct i results.
Although i want to make it an npm package that can work as a cli engine. For example i want to write the command generate and the code to produce the results.
In order to auto complete the file i have to read some data that i have stored in a .csv file that comes along with my program.
When i try to run generate command under another folder it can read that file.
I am very new to cli and i don't understand yet quite well how thing work.
Thanks in advance for the help.
Here is the code of my cli conversion.
#!/usr/bin/env node
const program = require("commander");
const {
predict
} = require("./classifier");
program.version("1.0.0").description("ESLint Rules Generator");
program
.command("generate")
.description("Generate ESLint Rules")
.action(() => {
predict();
});
program.parse(process.argv);
Here is the problematic line:
let str = fs.readFileSync("data_files/rules.csv", "utf-8");
This is the error i get: ENOENT: no such file or directory, open 'data_files/rules.csv'

Working when run, but not when built

Im on macOS. I am creating a simple electron app. When I run the app with electron . everything works perfectly with no errors. Now that my app is finished, I wanted to build and distribute it. So I setup electron-builder and I got that to work just fine. However, when I run the MyApp.app in the build folder, I get an error saying:
Uncaught Error: ENOENT: no such file or directory, scandir './img/'
I call scandir here:
const fs = require('fs');
var files = [];
fs.readdirSync("./img/").forEach(file => {
files.push(file);
})
Why is this working when I run it with node, but is not working in the build? How can I fix this issue?
Why is this working when I run it with node, but is not working in the
build? How can I fix this issue?
It's difficult to tell without having more information about the whole app's structure, it may depend on how your code is actually called or required from the html file.
Anyway, using the __dirname global variable to build the directory path usually solves this kind of problem. Please try:
const fs = require('fs');
const path = require('path');
var files = [];
fs.readdirSync(path.join(__dirname, 'img')).forEach(file => {
files.push(file);
});

Electron open file/directory in specific application

I'm building a sort of File explorer / Finder using Electron.
I want to open some file types with a specific application.
I've tried the approach from this answer:
Open external file with Electron
import { spawn } from 'child_process'
spawn('/path/to/app/superApp.app', ['/path/to/file'])
But when I do that, I get a EACCES error as follows.
Is this the right approach? If yes, how can I fix the issue? If not, what is the right approach?
You can open a file or folder through shell commands from the electron module. The commands work on both main and renderer process.
const {shell} = require('electron') // deconstructing assignment
shell.showItemInFolder('filepath') // Show the given file in a file manager. If possible, select the file.
shell.openPath('folderpath') // Open the given file in the desktop's default manner.
More info on https://github.com/electron/electron/blob/master/docs/api/shell.md
While the accepted answer does say how to open the folder in file explorer, it doesn't answer the question on how to open the folder WITH an external program like VSCode. It can be done like so:
import { spawn, SpawnOptions } from "child_process";
import { pathExists } from "fs-extra";
const editorPath = "C:/Program Files/Microsoft VS Code/Code.exe";
export const launchExternalEditor = async (
folderPath: string
): Promise<void> => {
const exists = await pathExists(editorPath);
if (!exists) {
console.error("editor not found");
}
const opts: SpawnOptions = {
// Make sure the editor processes are detached from the Desktop app.
// Otherwise, some editors (like Notepad++) will be killed when the
// Desktop app is closed.
detached: true,
};
spawn(editorPath, [folderPath], opts);
};
This will launch the folder in VSCode as it's root directory. Code is taken from this repository.
Note: this may require some tinkering depending on what program you are trying to use, but so far it worked properly with: VSCode, Visual Studio 2019, Intellij IDEA, NetBeans.
For display native system dialogs for opening and saving files, alerting, etc. you can use dialog module from electron package.
const electron = require('electron');
var filePath = __dirname;
console.log(electron.dialog.showOpenDialog)({
properties:['openFile'],
filters:[
{name:'Log', extentions:['csv', 'log']}
]
});
A very prompt explanation is provided at Electron Docs.

Resources