Thursday, 20 August 2015

Get Microsoft Office/ Excel Name Function in Java

Retrieve Microsoft Excel Name and Office Path In JAVA


I have implemented an office name parsing function to reterieve which Office is installed in our machine. This code is written in Java and can be used to Automate Excel .. 

This function Performs Following Steps :

1) Find Office Excel Path Dynamically.
2) Open Excels
3) Parse the path to find which Excel is installed.

Note:- I have used parsing for Excel 2013, Excel 2010, Excel 2007 you can extend it for other office Names

public static String getofficeName()
    {
    Process p;
try {
p = Runtime.getRuntime().exec
     (new String [] { "cmd.exe", "/c", "assoc", ".xls"});

           BufferedReader input =
             new BufferedReader
               (new InputStreamReader(p.getInputStream()));
           String extensionType = input.readLine();
           input.close();
           // extract type
           if (extensionType == null) {
             System.out.println("no office installed ?");
             System.exit(1);
           }
           String fileType[] = extensionType.split("=");

           p = Runtime.getRuntime().exec
             (new String [] { "cmd.exe", "/c", "ftype", fileType[1]});
           input =
             new BufferedReader
               (new InputStreamReader(p.getInputStream()));
           String fileAssociation = input.readLine();
           // extract path
           String officePath = fileAssociation.split("=")[1];
           officePath = officePath.split("/")[0];
           System.out.println(officePath);
   
           if( officePath.contains("Root"))
  {
            String[] officePath1 = officePath.split("Root\\\\", 2);
            a = officePath1[1];
            a  = a.split("\\\\")[0];

            if (a.equalsIgnoreCase("Office15"))
            {
            OfficeName = "Excel 2013";
           
            else if (a.equalsIgnoreCase("Office14"))

    {

    OfficeName = "Excel 2010";
   
            
        else if (a.equalsIgnoreCase("Office12"))

    {

    OfficeName = "Excel 2007";
    }
    }
    else
    {
    String[] officePath2 = officePath.split("Office\\\\", 2);
    a = officePath2[1];
    a  = a.split("\\\\")[0];

    if (a.equalsIgnoreCase("Office14"))

    {

    OfficeName = "Excel 2010";
    }
   
    else if (a.equalsIgnoreCase("Office15"))
        {
        OfficeName = "Excel 2013";
       
    else if (a.equalsIgnoreCase("Office12"))

    {

    OfficeName = "Excel 2007";
    }
   
    }

      }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
return OfficeName;

 }
    

Wednesday, 12 August 2015

Sikuli With Java For Testers

Sikuli With Java For Testers

Introduction:

Sikuli is an open source tool to automate and test graphical user interfaces (GUI) using images (screenshots).

As, Sikuli is based on image recognition, it waits for anything to appear on screen and, click, type etc. according to the specified identifiers. The identifiers are specified by images. Sikuli triggers GUI interactions based on the image visual match, the image which we have passed as the parameter along with all methods.

For example, if there is a Flash component on the web page with a "Next" button, an image of the "Next" button could be captured using the Sikuli, and stored. Then with the Java code the "Next" button could be identified and clicked. In-addition, the core of Sikuli Script is written in Java, which means you can use Sikuli Script as a standard JAVA library in your program.

Sikuli Can Be Useful When:

  1. When we are testing Legacy apps which are designed without a thought towards testability and thus ids were not specified by the developer
  2. When we have to be particular about UI which has lot of images and flash content on the webpage
  3. When we have to deal with an application like Google maps, Excel
  4. It can also be used to test non-browser GUI applications

    Common Functions of Java I Used In Sikuli Framework

1)Absolute Path



2) CSV Read Functions...




3) Send Email Function




Prerequisite

1. Download and install sikuli from for more information visit link "http://www.sikuli.org/download.html 
2. Download and install java 1.6
3. Download and setup eclipse with Test NG

I have created a sikuli script in which I open notepad, type some text and save file into system.

Step by Step working

1. Create a java project in to your eclipse.
2. Add "testng.jar" and "sikuli-script.jar" to your eclipse library.
3. Create a images folder into your java project as below.  
4.  Get images of using Sikuli IDE and put into images folder.
5. Create a java class into src folder.
6. Import sikuli classed and create Screen class object. as below java code:

package com.test;

import org.sikuli.script.App;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;
import org.testng.annotations.Test;

public class SikuliTest {

    public Screen screen;
    
    @Test
    public void tesSikuli() throws InterruptedException, FindFailed
    {
        //open note pad application
        screen =new  Screen();
        App.open("notepad.exe");
        Thread.sleep(2000);
        
        //type text notepad
        screen.type("images/InputFiled.png", "Testing sikuli with Test NG");
        Thread.sleep(2000);
        
        //click on file menu
        screen.click("images/FileMenu.png", 20);
        Thread.sleep(2000);
        
        //click on save submenu
        screen.click("images/SavesubMenu.png", 20);
        Thread.sleep(2000);
        
        //select file input field and type path 
       screen.click("images/FilePathInput.png", 20);
        screen.type("images/FilePathInput.png", "C:\\test.txt");
        Thread.sleep(2000);
        
        //click save button
        screen.click("images/SaveButton.png", 20);
        Thread.sleep(2000);    
        
    }
}

7. Right click on eclipse test scripts and chose Run As > TestNG Test and click.
8. After successfully run you can see eclipse console log as below screen.


Get Microsoft Office/ Excel Name Function in Java

Retrieve Microsoft Excel Name and Office Path In JAVA I have implemented an office name parsing function to reterieve which Office is in...

Get Counted