Techno Logica


Thursday, August 28, 2014

Automation of Web-based Apps using Groovy

What is Groovy?

Groovy is a modern, dynamic, general-purpose scripting language that runs on the Java Virtual Machine (JVM). Its based on Java and is considered a superset of Java, however it has some differences. It inter-operates with other Java code and libraries.

The below link give a kick start to the language:
http://www.oracle.com/technetwork/articles/java/groovy-1695411.html

Download Groovy

There are many ways to test Web Applications with Groovy:

use Groovy (potentially in conjunction with a specialist HTML parser) to parse HTML pages as if they were XML
use Groovy to simplify the code required to drive a Java API browser simulator, e.g. HtmlUnit or HttpUnit
use Groovy to simplify the code required to drive a Java API for manually driving a real browser, e.g. IE or Firefox
use Groovy to interact with a higher-level testing library which uses one of the above two approaches, e.g. Watij (for the equivalent of Watir in the Ruby world) or WebTest (to open up the possibility of testing more than just web applications)

Monday, August 25, 2014

Use PHP and SSH to execute command on remote machine


I had a requirement of executing commands on remote machine. I needed to execute commands in both Windows and Linux platforms. There are a wide variety of communication softwares out there who can help you with this. But, I choose SSH as a communication protocol for my requirement.

Linux comes loaded with ssh enabled and for windows we can have cygwin ssh installed. This was perfect, I will have to only configure the windows based OS. 

My requirement was to trigger it using a web page, so i used php for my server processing. After some trial and error - phpseclib worked out great!!

The official link to the tutorial : http://phpseclib.sourceforge.net/ 

Below code will give you a head start:

set_include_path(get_include_path().'\phpseclib');
include('Net/SSH2.php');
include('Net/SFTP.php');

define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

echo "\r\n========= Open Communication ==========\r\n";
//Open SSH Communication
$ssh = new Net_SSH2($server);
//$key = new Crypt_RSA();
//$key->loadKey(file_get_contents('privatekey'));

if (!$ssh->login($username, $password)) {
//if (!$ssh->login($username, $key)) {
    echo "\r\n Error Occured while logging in...\r\n";
    //echo "Username:$username\r\n";
    //echo "Password:$password\r\n";
exit(1);
}
echo "\r\n Successfully Opened SSH Communication...";

$cmd = "your_command";
$arg1 = "your_arg_1";
$command = 'perl '.$cmd.' '.$arg1;

//Adding the below code to get proper command return code
$command .= ';echo "[return_code:$?]"';
echo "\r\n". $command;

//Executing the command
$output = $ssh->exec($command);

//Extract Return code
preg_match( '/\[return_code:(.*?)\]/', $output, $match );
$return_code = $match[1];
echo "\r\n". $output;
echo "\r\n".$return_code;

if(($return_code != 0))
{
echo "\r\n Command Failed";
}
else
{
echo "\r\n Command Pass";
}
?>