I am using WebdriverIO to run a file-uploading .exe created with AutoIt.
I am running the script inside a browser.execute command. The file needs to run from the local drive and execute wd in Chrome browser.
Here is the code :
this.open("https://smallpdf.com/word-to-pdf");
this.SubmitClick("//div[#class='l0v3m7-3 hIetmU']");
this.BrowserSleep(2000);
scr.runAutoItScript('C:\\test\\desktopApp\\autoit', 'fileUpload.exe')
//scr have the child process:
const { execFile } = require('child_process').execFile;
module.exports = {
runAutoItScript(pathToScript, scriptName) {
console.info(`\n> Started execution of ${pathToScript} / ${scriptName} ...`);
execFile(`${pathToScript}/${scriptName}`, (error, stdout, stderr) => {
if (error) {
throw error;
} else {
console.info(`\n> Finished execution of ${scriptName}! | Output: ${stdout}`);
}
});
}
}
I remember doing something like this in the past and I used NodeJS's child_process.execFile command. The documentation is heavy on child_process, so read carefully.
You should end up with something along the lines of:
const execFile = require('child_process').execFile;
let runAutoItScript = function(pathToScript, scriptName) {
console.info(`\n> Started execution of ${scriptName} ...`);
execFile(`${pathToScript}/${scriptName}`, (error, stdout, stderr) => {
if (error) {
throw error;
} else {
// > do something with the script output <
console.info(`\n> Finished execution of ${scriptName}! | Output: ${stdout}`);
}
});
}
runAutoItScript('/this/is/a/valid/path', 'AwesomeScript.exe');
Next step would be to minify it and make it run inside browser.execute call.
You can find a lot of child_process examples online, just leverage the resources available to run the simplest script. Develop from there.
Related
I'm trying to download a zip file with rn-fetch-blob, then when I got this file I unzip it with React-native-zip-archive.
It often works well, but sometimes, the "unzipFile()" function I've created can't unzip the file, like if it is corrupted.
Someone already got this problem ?
Here is my code :
downloadZipFile(res => {
unzipFile(res.path(), (boolean, path) => {
if (boolean !== false) {
db = SQLite.openDatabase({
name: "addb.sqlite",
location: "default",
createFromLocation: path
}).then(DB => {
db = DB;
db.transaction(tx => {
tx.executeSql(
"SELECT * FROM sqlite_master",
[],
(tx, results) => {
console.log("Logs sqlite_master");
const rows = results.rows;
for (let i = 0; i < rows.length; i++) {
console.log(_getCurrentDate());
datas.push({
...rows.item(i)
});
}
console.log(datas);
callback(true);
},
(tx, err) => {
console.log(err)
}
);
});
});
} else {
console.log("Can't create database");
callback(false);
}
});
});
And the functions I used :
export function downloadZipFile(callback) {
RNFetchBlob.config({
fileCache: true
})
.fetch(
"GET",
"MY LINK"
)
.then(res => {
console.log("The file saved to ", res.path());
callback(res);
})
.catch((errorMessage, statusCode) => {
// error handling
console.log(
"erreur : " + errorMessage + " and statuscode : " + statusCode
);
});
}
export function unzipFile(sourcePath, callback) {
const charset = "UTF-8";
const targetPath = "/data/user/0/com.myapp/databases/";
unzip(sourcePath, targetPath, charset)
.then(path => {
console.log(`unzip completed at ${path}`);
callback(true, path);
})
.catch(error => {
console.log("there is an error" + error);
callback(false, null);
});
}
Others informations :
The file is a database that I have to put in the "databases" folder's application. I tried to put a console.log(path) everywhere in the "unzipFile()" function to see if the file is really created when I try to unzip it, and it seems he is hereā¦ And when the file is impossible to unzip, it does the same size as the others which work.
rn-fetch-blob calls an api which copy an existant distant database and zip it as an axd file. Is there any problem with this format ? Can the api be the problem ?
The axd file created by the api is used by an existant application and seems to work correctly for the existant application. Moreover, when we download the file without rn-fetch-blob (by copying the link in my navigator), it works correctly everytime I tried.
I tried to download the file directly,the api always sent me the same file (a zip file or an axd file), and it works without problem (20 try). Can the problem be the delay to download the file ? With the api, it takes 5 or 6 seconds, without it takes 2 seconds. But I think my unzipFile() function only start when the file is downloaded, no ? And as I said, when I put a console.log(path) in the unzipFile() function, the file is here, with same size as others...
I Don't know how to make it works everytime, hope someone can help me :)
Ty !
I tried to put a for(let i = 1; i < 101; i++) to do the RNFB 100 times :
it works 97 times / 100 and 96 times /100...
Then I tried to put a timer, to be sure the file is finished to download, it works 3 times / 100...
And I deleted the timer, and now it never works anymore, or 5 times / 100...
I really Don't understand what is the problem :(
I've been searching the web for more than 3 hours now looking for relevant ways to play an audio file but unfortunately I can't find anything useful. I have a CasperJS that's automating some tasks and I wanted it to play an audio file (e.g. beep.wav) after it completes all the tasks. I wonder if it's possible.
casper.run(function(casper) {
fs.write( saveDir, JSON.stringify(content, null, ' '), 'w');
// play an audio file before exiting....
casper.exit();
});
You need to use Child Process Module to run your script, to play the music.
I created pl.sh script to play the music:
#!/bin/bash
mplayer "/music/downloads2/Technoboy - Into Deep.oga"
exit 0
Then I created CasperJS script:
var casper = require('casper').create();
casper
.start('http://domu-test-2/node/12', function(){
var childProcess;
try {
childProcess = require("child_process")
} catch(e){
console.log(e, "(error)")
}
if (childProcess){
childProcess.execFile("/bin/bash", ["./pl.sh"], null, function(err, stdout, stderr){
console.log("execFileSTDOUT: "+stdout);
console.log("execFileSTDERR:",stderr);
});
console.log("Shell commands executed")
} else {
console.log("Unable to require child process (error)")
}
this.wait(10000)// need to wait to play the sound
})
.run();
And then PhantomJS script:
var page = require('webpage').create();
page.open('http://domu-test-2/node/12', function() {
var childProcess;
try {
childProcess = require("child_process")
} catch(e){
console.log(e, "(error)")
}
if (childProcess){
childProcess.execFile("/bin/bash", ["./pl.sh"], null, function(err, stdout, stderr){
console.log("execFileSTDOUT: "+stdout);
console.log("execFileSTDERR:",stderr);
});
console.log("Shell commands executed")
} else {
console.log("Unable to require child process (error)")
}
setTimeout(phantom.exit,10000)// need to wait to play the sound
});
Trying to open random pages through casperJS start method but some pages are loading properly and some of them are not, so in this scenario it is not exiting from casperjs.
It is getting stuck in console then need to manually exit from console using CTR+C.
casper.start("some url", function() {
if(this.status().currentHTTPStatus == 200) {
casper.echo("page is loading");
} else {
casper.echo("page is in error ");
this.exit();
}
});
Wrap it by a then step with a global stepTimeout option.
Sample code:
var casper = require('casper').create({
stepTimeout: 10000 //10s
})
casper.start()
casper.then(funtion(){
casper.open(url)
})
casper.run()
Try bypass() to ignore the next thens.
casper.start("some url", function() {
if(this.status().currentHTTPStatus == 200) {
casper.echo("page is loading");
} else {
casper.echo("page is in error ");
this.bypass(2); // Will not execute the then functions.
}
}).then(function() {
// The 1st then function.
}).then(function() {
// The 2nd then function.
})
casper.run(function() {
this.echo('Something');
this.exit(); // <--- Here.
});
Ref: https://github.com/balderdashy/skipper/issues/49
Adapter: skipper-gridfs
Basic controller code:
req.file('fileTest')
.upload({
// You can apply a file upload limit (in bytes)
maxBytes: maxUpload,
adapter: require('skipper-gridfs'),
uri: bucketConnect,
saveAs : function (__newFileStream,cb) {
cb(null, __newFileStream.filename);
}
}, function whenDone(err, uploadedFiles) {
if (err) {
var error = { "status": 500, "error" : err };
return res.serverError(error);
}else {
I have a jQuery-File-Upload client ( https://blueimp.github.io/jQuery-File-Upload/ ) impementing the "cancel" procedure by using jqXHR abort described here (https://github.com/blueimp/jQuery-File-Upload/wiki/API ):
$('button.cancel').click(function (e) {
jqXHR.abort();
});
After the client aborts, the server crashes with the following message:
events.js:72
throw er; // Unhandled 'error' event
^
Error: Request aborted
at IncomingMessage.onReqAborted (.../node_modules/sails/node_modules/skipper/node_modules/multiparty/index.js:175:17)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at abortIncoming (http.js:1911:11)
at Socket.serverSocketCloseListener (http.js:1923:5)
at Socket.EventEmitter.emit (events.js:117:20)
at TCP.close (net.js:466:12)
I've used try/catch but it didn't work, the server crashes anyway.
I am not sure if this is a Skipper issue or a Multiparty issue -- my knowledge stops here ( https://github.com/andrewrk/node-multiparty/blob/master/index.js ):
function onReqAborted() {
waitend = false;
self.emit('aborted');
handleError(new Error("Request aborted"));
}
function onReqEnd() {
waitend = false;
}
function handleError(err) {
var first = !self.error;
if (first) {
self.error = err;
req.removeListener('aborted', onReqAborted);
req.removeListener('end', onReqEnd);
if (self.destStream) {
self.destStream.emit('error', err);
}
}
cleanupOpenFiles(self);
if (first) {
self.emit('error', err);
}
}
At first I thought this was the way the jqXHR request was aborted, but it seems to be a generic Skipper issue on aborted uploads, since the simple act of closing the tab during an upload will crash the server (different message):
_stream_writable.js:233
cb(er);
^
TypeError: object is not a function
at onwriteError (_stream_writable.js:233:5)
at onwrite (_stream_writable.js:253:5)
at WritableState.onwrite (_stream_writable.js:97:5)
at Writable.<anonymous> (.../node_modules/skipper-gridfs/index.js:179:25)
at Writable.g (events.js:180:16)
at Writable.EventEmitter.emit (events.js:117:20)
at PassThrough.<anonymous> (.../node_modules/skipper-gridfs/index.js:194:36)
at PassThrough.g (events.js:180:16)
at PassThrough.EventEmitter.emit (events.js:117:20)
at .../node_modules/sails/node_modules/skipper/standalone/Upstream/prototype.fatalIncomingError.js:55:17
I have tried aborting the upload by closing the tab while using a simple upload controller (not Skipper) and there is no crash:
var uploadFile = req.file('fileTest');
console.log(uploadFile);
uploadFile.upload(function onUploadComplete (err, files) { // Files will be uploaded to .tmp/uploads
if (err) return res.serverError(err); // IF ERROR Return and send 500 error with error
console.log(files);
res.json({status:200,file:files});
});
So, did anybody see this happening and is there any workaround?
This issue has been solved in skipper#0.5.4 and skipper-disk#0.5.4
Ref.: https://github.com/balderdashy/skipper/issues/49
Also there is an Issue in skipper-gridfs#0.5.3
Link: https://github.com/willhuang85/skipper-gridfs/issues/20
Hi I am developing a website as my chrome extension.the extension periodically checks the output of a server file and then works according to the output of the server file.Now what I need is that presently it checks the output of the server file every 5 min.So what I need is that when the output of the server file changes at any time,at that moment I have to do some operations.How can I do this?
Here is my background.js
var myNotificationID = null;
var oldChromeVersion = !chrome.runtime;
chrome.windows.onCreated.addListener(function (){
updateIcon();
});
chrome.tabs.onCreated.addListener(function (){
updateIcon();
});
chrome.tabs.onUpdated.addListener(function (){
updateIcon();
});
function getGmailUrl() {
return "http://calpinemate.com/";
}
function isGmailUrl(url) {
return url.indexOf(getGmailUrl()) == 0;
}
chrome.windows.onCreated.addListener(function (){
updateIcon();
});
function onInit() {
updateIcon();
if (!oldChromeVersion) {
chrome.alarms.create('watchdog',{periodInMinutes:5,delayInMinutes: 0});
}
}
function onAlarm(alarm) {
if (alarm && alarm.name == 'watchdog') {
onWatchdog();
}
else {
updateIcon();
}
}
function onWatchdog() {
chrome.alarms.get('refresh', function(alarm) {
if (alarm) {
console.log('Refresh alarm exists. Yay.');
}
else {
updateIcon();
}
});
}
if (oldChromeVersion) {
updateIcon();
onInit();
}
else {
chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);
}
It is the updateIcon() which reads the server file.And when there is change in the output of server file,I have to call the updateIcon() itself.Presently only in every 5 min,the output is checked and updated in extension.But I need it to happen at the moment the status of server file changes.Anyone please help me.
In short,what I need is that when the output of API changes at any time at that time I have to call updateIcon().
Here is my updateIcon()
function updateIcon(){
var urlPrefix = 'http://www.calpinemate.com/employees/attendanceStatus/';
var urlSuffix = '/2';
var req = new XMLHttpRequest();
req.addEventListener("readystatechange", function() {
if (req.readyState == 4) {
if (req.status == 200) {
var item=req.responseText;
if(item==1){
.....//something
}
else{
// do something
}
else {
alert("ERROR: status code " + req.status);
}
}
});
var url = urlPrefix + encodeURIComponent(localStorage.username) + urlSuffix;
req.open("GET", url);
req.send(null);
}
I need to periodically monitor the API for output.
You could use a WebSocket to maintain a persistent, two-way channel between the server and your extension.
But, considering that PHP does not have native support for WebSockets (i.e. you would need to use an external library) and the fact that the interaction will be infrequent (only at log-in/-out), it might be unnecessary overhead.
I suggest you communicate the login-status directly from your web-page to your extension. For a more detailed description of the process, see my answer to one similar question of yours.
UPDATE:
Since it turned out you don't have control of the domain you need to "monitor", there is unfortunately no other option (that I know of) than polling at frequent intervals on that server-resource.
If you are using a non-persistent background-page (which is advisable due to its resource-friendliness) you must use the chrome.alarms API, which allows at most 1 triggering per minute: (in order to reduce the load on the user's machine - note: to help debug your extension, this limit is no imposed on unpacked extensions).
If you decide to use a persistent background-page, you can use setInterval() with an arbitrarily smal period (in milliseconds):
setInterval(function() {
/* Check up on the server */
...
}, 30000); // <-- trigger every 30.000 milliseconds (== 30 seconds)