Sunday 14 June 2015

Node.js - Reverse of words in a string.

Prerequisites:
Node.js and NPM is already installed, click below link for installing Node.js and NPM.
http://automation-home.blogspot.in/2015/05/installing-nodejs-and-npm-on-windows.html

Procedure-1:
FileName: script1.js
var str = "Welcome to Automation-Home";
var result = "";
var wordArr = str.split(" ");
for(var i=wordArr.length-1; i>=0;i--){
   result = result+wordArr[i];
   if(i>0) result = result + " ";
}
console.log(result);

Procedure-2:
FileName: script2.js
var str = "Automation-Home welcomes you";
var result = str.split(" ").reverse().join(" ");
console.log(result);

Procedure-3:
FileName: script3.js
var rev = function(str){
  return str.split(" ").reverse().join(" ");
}
console.log(rev("NodeJS javascript"));

Watch Demo

Friday 12 June 2015

Node.js - Reading data from JSON file or JSON object.

Prerequisites:
Node.js and NPM is already installed, click below link for installing Node.js and NPM.
http://automation-home.blogspot.in/2015/05/installing-nodejs-and-npm-on-windows.html

Below steps demonstrates reading the data from JSON file.
File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonData.js
var fs = require('fs');
fs.readFile('BookInfo.json', 'utf8', function (err, data) {
  if (err) throw err;
  var jsonData = JSON.parse(data);
  console.log("--------------Books Information --------");
  for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id"+jsonData[i].book_id);
    console.log("Book Name Id"+jsonData[i].book_name);
    console.log("Book Cost Id"+jsonData[i].book_cost);
  console.log("----------------------------------------");
  }
});


Open the command prompt.

Open the folder path in command prompt where the 'BooksInfo.json' and 'PrintJsonData.js' are placed.

Type the command 'node PrintJsonData.js' and press enter key.



Below steps demonstrates reading data from JSON object.
File Name: PrintJsonData.js
var response = '[{"Mobile":iOS , "Cost":55000}]';
var jsonObject = JSON.parse(response);
console.log(jsonObject[0].Mobile);
console.log(jsonObject[0].Cost);

Type the command 'node PrintJsonData.js' and press enter key.

--------------------------------------------------------------------------------------------------------------------------

Below steps demonstrates reading the data from JSON file using 'jsonfile' package.

Import the 'jsonfile' package using the npm command 'npm install jsonfile --save'.
Note: if 'jsonfile' package is already installed, you can ignore this step(i.e. installing 'jsonfile' package).


File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonData1.js
var jf = require('jsonfile')
var file = 'BookInfo.json'
jf.readFile(file, function(err, jsonData) {
  if (err) throw err;
  console.log("--------------Books Information --------");
  for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id : "+jsonData[i].book_id);
    console.log("Book Name : "+jsonData[i].book_name);
    console.log("Book Cost : "+jsonData[i].book_cost);
    console.log("----------------------------------------");
  }

});

Type the command 'node PrintJsonData1.js' and press enter key.

--------------------------------------------------------------------------------------------------------------------------

Below steps demonstrates reading data from JSON file using 'jsonfile' package - 'readFileSync' function.

Import the 'jsonfile' package using the npm command 'npm install jsonfile --save'.
Note: if 'jsonfile' package is already installed, you can ignore this step(i.e. installing 'jsonfile' package).


File Name: BooksInfo.json
[
  {
    "book_id" : "1125",
    "book_name" : "Head First Java",
  "book_cost" : "620"
  },
  {
    "book_id" : "8876",
    "book_name" : "Node.js in Action",
  "book_cost" : "2097"
  },
  {
    "book_id" : "5484",
    "book_name" : "Angular JS Testing Cookbook",
  "book_cost" : "1519"
    }
]

File Name: PrintJsonDataSync.js
var jf = require('jsonfile')
var file = 'BookInfo.json'

var jsonData = jf.readFileSync(file);
for (var i = 0; i < jsonData.length; ++i) {
    console.log("Book Id : "+jsonData[i].book_id);
    console.log("Book Name : "+jsonData[i].book_name);
    console.log("Book Cost : "+jsonData[i].book_cost);
    console.log("----------------------------------------");
}

Type the command 'node PrintJsonDataSync.js' and press enter key.

Monday 8 June 2015

Java and AutoIt - Automating the calculator application using autoitx4java

autoitx4java is useful for testing the Windows applications using the AutoIt.

For more information on 'autoitx4java' check the url https://code.google.com/p/autoitx4java/

1. Download 'AutoItX4Java.jar'.
https://code.google.com/p/autoitx4java/downloads/list

2. Copy the 'AutoItX4Java.jar' file to the 'lib' folder of the project(i.e. Eclipse IDE Project).

3. Download JACOB.


4. Extract the 'jacob-1.18-M2.zip' file.

5. Copy the below file in the 'lib' folder of the project.





7. Extract the 'autoit-v3.zip' file.

8. AutoIt Inspector - for inspecting the UI elements.


9.Set the java build path for 
- AutoItX4Java.jar
- jacob.jar



10. Create a java class.


11. Execute the program(Sample Program is added in the end of the page).

Note: Unable to execute the program, since AutoIt is not registered in the windows registry

Below steps guides you in adding AutoIt to the windows registry.
12. Open the 'CommandPrompt' as 'Administrator'



13. After extracting the 'autoit-v3.zip' file, navigate to the folder 'autoit-v3\install\AutoItX' in the extracted zip file.

Navigate to the 'autoit-v3\install\AutoItX'(i.e. extracted 'autoit-v3.zip') in the command prompt.

14. Add the 'AutoIt' to the registry.
If you are using 'Java 32bit', run the below command in the 'Command Prompt':
regsvr32.exe AutoItX3.dll


If you are using 'Java 64bit', run the below command in the 'Command Prompt':
regsvr32.exe AutoItX3_x64.dll


15.Execute the program again(Sample Program is added in the end of the page), after setting the AutoIt in windows registry.



you can download the below sample code from the repository, click link AutoIt Proj.zip to download.

Sample Program:
package autoit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import autoitx4java.AutoItX;

import com.jacob.com.LibraryLoader;
public class CalculatorTest {
//Choose the 'JACOB' dll based on the JVM bit version.
final String JACOB_DLL_TO_USE = System.getProperty("sun.arch.data.model").contains("32") ?
 "jacob-1.18-M2-x86.dll" : "jacob-1.18-M2-x64.dll";

final String APPLICATION_TITLE = "Calculator";
final String APPLICATION = "calc.exe";
private AutoItX control;

private Map<Integer,String> calcNumPad_ObjectRepository;
private Map<String,String> calcOperPad_ObjectRepository;
{
//Object Repository for Calculator Numbers
calcNumPad_ObjectRepository = new HashMap<Integer,String>();
calcNumPad_ObjectRepository.put(0, "130");
calcNumPad_ObjectRepository.put(1, "131");
calcNumPad_ObjectRepository.put(2, "132");
calcNumPad_ObjectRepository.put(3, "133");
calcNumPad_ObjectRepository.put(4, "134");
calcNumPad_ObjectRepository.put(5, "135");
calcNumPad_ObjectRepository.put(6, "136");
calcNumPad_ObjectRepository.put(7, "137");
calcNumPad_ObjectRepository.put(8, "138");
calcNumPad_ObjectRepository.put(9, "139");

//Object Repository for Calculator Operators
calcOperPad_ObjectRepository = new HashMap<String,String>();
calcOperPad_ObjectRepository.put("+", "93");
calcOperPad_ObjectRepository.put("-", "94");
calcOperPad_ObjectRepository.put("*", "92");
calcOperPad_ObjectRepository.put("/", "91");
calcOperPad_ObjectRepository.put("=", "121");
calcOperPad_ObjectRepository.put("clear", "81");
//Load the jacob dll.
File file = new File(System.getProperty("user.dir")+"\\lib", JACOB_DLL_TO_USE);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
control = new AutoItX();
}
public static void main(String[] args) throws InterruptedException {
CalculatorTest ct = new CalculatorTest();
//Launch 'Calculator' application.
ct.control.run("calc.exe");
ct.control.winActivate("Calculator");
ct.control.winWaitActive("Calculator");

//Perform Addition
System.out.println("Addition of 1,3 - Actual Results: "+ct.add(1,3)+",  Expected Results: 4");
System.out.println("Addition of 17,3 - Actual Results: "+ct.add(17,3)+",  Expected Results: 20");

//Perform Subtraction
System.out.println("Subtraction of 5,1 - Actual Results: "+ct.subtraction(5,1)+",  Expected Results: 4");
System.out.println("Subtraction of 90,7 - Actual Results: "+ct.subtraction(90,7)+",  Expected Results: 83");
//Perform Multiplication
System.out.println("Multiplication of 8,2 - Actual Results: "+ct.multiplication(8,2)+",  Expected Results: 16");
System.out.println("Multiplication of 15,4 - Actual Results: "+ct.multiplication(15,4)+",  Expected Results: 60");
//Perform Division
System.out.println("Division of 9,3 - Actual Results: "+ct.division(9,3)+",  Expected Results: 3");
System.out.println("Division of 100,2 - Actual Results: "+ct.division(100,2)+",  Expected Results: 50");
//Close 'Calculator' application.
ct.control.winClose("Calculator");
}
//Perform 'Addition'.
public int add(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("+");
clickNumber(b);
performOperation("=");
return Integer.parseInt(getResults().trim());
}
//Perform 'Subtraction'.
public int subtraction(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("-");
clickNumber(b);
performOperation("=");

return Integer.parseInt(getResults().trim());
}
//Perform 'Multiplication'.
public int multiplication(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("*");
clickNumber(b);
performOperation("=");

return Integer.parseInt(getResults().trim());
}

//Perform 'Division'.
public int division(int a,int b) throws NumberFormatException, InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("/");
clickNumber(b);
performOperation("=");
return Integer.parseInt(getResults().trim());
}
//Fetch the results after performing the operations
private String getResults() throws InterruptedException{
Thread.sleep(1000);
return control.winGetText(APPLICATION_TITLE);
}
//Click the Number in the calculator application.
private void clickNumber(int number) throws InterruptedException{

String sNumber = String.valueOf(number);
for(int i = 0; i < sNumber.length(); i++) {
   control.controlClick(APPLICATION_TITLE, "", calcNumPad_ObjectRepository.get(Character.digit(sNumber.charAt(i), 10)));
   Thread.sleep(1000);
}
}
//Perform operations.
private void performOperation(String controlID) throws InterruptedException{
control.controlClick(APPLICATION_TITLE, "", calcOperPad_ObjectRepository.get(controlID));
Thread.sleep(1000);
}
}

Troubleshooting: 
Make sure you are using the correct version of Jacob and AutoItX3 dlls. Since both come in x86 and x64 versions.

Related Links

Automating Calculator application using Java and Sikuli.
http://automation-home.blogspot.in/2014/09/AutomateCalcAppWithJavaAndSikuli.html

Java and Twin Automation Tool - Automating windows calculator application.
http://automation-home.blogspot.in/2015/12/java-twin-automation-tool-automate-windows-calc-application.html

Sunday 7 June 2015

Selenium Webdriver 2.46 released

Note:
Below post is older one, there is new version of Selenium-Webdriver released,
you can check it at http://www.seleniumhq.org/download/

Selenium-Webdriver latest version 2.46 is released recently for the language bindings
- Java
- C#
- Ruby
- Python
- JavaScript(WebdriverJS)

Selenium-Webdriver Client
Java- http://selenium-release.storage.googleapis.com/2.46/selenium-java-2.46.0.zip
C# - http://selenium-release.storage.googleapis.com/2.46/selenium-dotnet-2.46.0.zip
Check out more information at http://www.seleniumhq.org/download/

Webdriver-Java Client Change log
https://raw.githubusercontent.com/SeleniumHQ/selenium/master/java/CHANGELOG

Selenium-Webdriver Standalone server
http://selenium-release.storage.googleapis.com/2.46/selenium-server-standalone-2.46.0.jar

Internet Explorer Driver Server - 2.46
32 Bit - http://selenium-release.storage.googleapis.com/2.46/IEDriverServer_Win32_2.46.0.zip
64 Bit - http://selenium-release.storage.googleapis.com/2.46/IEDriverServer_x64_2.46.0.zip

Internet Explorer Driver Server - 2.46 Change log

https://raw.githubusercontent.com/SeleniumHQ/selenium/master/cpp/iedriverserver/CHANGELOG

Maven-Dependency
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.46.0</version>
</dependency>

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.46.0</version>
</dependency> 

Maven repository 
Check out more information at http://www.seleniumhq.org/download/maven.jsp

Wednesday 3 June 2015

C# MBUnit - MBUnit configuration and basic attributes

MBUnit is a 'Unit Testing' framework like Nunit and MSTest.

1. MBUnit can be downloaded from http://code.google.com/p/mb-unit/downloads/list



2. Extract the zip file, which is downloaded


3. Open the IDE, create the new project.


4. Add the references to the project.


5. Add the MBUnit attributes and methods in the class.
 


6. Build the project.

7. Once the project is build, dll file is created in the debug folder of the project.

8. Launch the 'Gallio Icarus'(Gallio.Icarus.exe), which is present in the 'bin' folder of the extracted the zip file.

9. Add the dll to the 'Gallio Icarus'.



10. Click on 'Start' button to executes the test methods.


11. View the Test Report.