|
/** |
|
* Copyright 2016 Jorge Luis Betanourt |
|
* |
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions |
|
* are met: |
|
* |
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|
* |
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in |
|
* the documentation and/or other materials provided with the distribution. |
|
* |
|
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived |
|
* from this software without specific prior written permission. |
|
* |
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
|
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
|
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
*/ |
|
|
|
var fs = require('fs') |
|
var args = require('system').args |
|
var casper = require('casper').create({ |
|
verbose: true, |
|
pageSettings: { |
|
webSecurityEnabled: false, |
|
// resourceTimeout: 10000, |
|
} |
|
}); |
|
|
|
const URL = 'https://correo.uci.cu/owncloud/'; |
|
const USER = 'testuser'; |
|
const PASSWORD = 'TestPassw0rd'; |
|
|
|
if (args.length < 5) { |
|
casper.echo("Usage: casperjs --ignore-ssl-errors=true script.js <dir>") |
|
casper.exit(0) |
|
} |
|
|
|
BASEDIR = args[4] |
|
|
|
if (BASEDIR[BASEDIR.length - 1] != '/') { |
|
BASEDIR = BASEDIR + '/' |
|
} |
|
|
|
// wait 7 seconds before giving up |
|
casper.options.waitTimeout = 10000000 |
|
|
|
casper.onResourceTimeout = function(request) { |
|
console.log('Response (#' + request.id + '): ' + JSON.stringify(request)); |
|
}; |
|
|
|
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X); Safari'); |
|
|
|
casper.on('started', function () { |
|
this.page.customHeaders = { 'Accept': '*/*' } |
|
}); |
|
|
|
casper.on('resource.error',function (request) { |
|
this.echo('[Res ID: ' + request.id + ' | URL: ' + request.url + ' | Error Message: ' + request.errorString + ']'); |
|
}); |
|
|
|
casper.start(URL, function() { |
|
this.echo(this.getTitle()); |
|
this.fill('form[name="login"]', { |
|
user: USER, |
|
password: PASSWORD |
|
}, true); |
|
}) |
|
|
|
casper.then(function () { |
|
this.echo(this.getTitle()); |
|
// this.capture('pepe.png') |
|
// check if are any notifications |
|
var info = this.getElementInfo('#notification') |
|
|
|
if (info.visible) { |
|
this.echo("There is some error, probably your storage is full") |
|
this.exit(-1) |
|
} |
|
}) |
|
|
|
casper.then(function () { |
|
var list = fs.list(BASEDIR) |
|
|
|
for (var i = 0; i < list.length; i++) { |
|
var filename = BASEDIR + list[i] |
|
if (fs.isFile(filename)) { |
|
this.echo(filename) |
|
var info = this.getElementInfo('#notification') |
|
|
|
if (info.visible) { |
|
this.echo("There is some error, probably your storage is full") |
|
this.exit(-1) |
|
} |
|
|
|
this.page.uploadFile('input[type="file"]', filename); |
|
this.echo("Processing ..." + filename) |
|
this.waitWhileVisible('button.stop.icon-close', function () { |
|
// this.capture('pepe.png'); |
|
this.echo('Done!'); |
|
}) |
|
} |
|
} |
|
}) |
|
|
|
casper.run(); |