Saturday, December 17, 2016

Password Encoder Utility - Selenium


Hi All,
 
We have password utility which generates based on your input string in QTP/UFT which is called as "Password encoder".  As selenium is freeversion :) they're not providing you any direct utility, but you've option to encrypt and decrypt your password by using org.apache.commons.codec.binary.Base64 from Selenium-Java.
 
Step 1:
Create Maven Project/Java Project
You must have below dependency in maven project or add jar to your project manually
 
 
Step 2:
Create one package and create a class "PasswordEncoder" with Main method
 
Step 3:
Copy and paste below code to your class file
 
import org.apache.commons.codec.binary.Base64;
 
public class PasswordEncoder {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println();
System. out .println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.println("Encrypted password of given string is in next line");
System.out.println(EncyptPassword(args[0]));
System.out.println();
System. out .println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
}
public static String EncyptPassword(String strOrgPwd){
byte [] encoded =Base64.encodeBase64(strOrgPwd.getBytes());
return new String(encoded);
}

 
Step 4:
Export Maven project/Java project as (executable) Runnable jar file with name "passwordEncoder.jar"
 
Step 5:
Create a Folder in your local drive
 
Step 6:
Copy runnable jar file into the created folder
 
Step 7:
Create a bat file and copy paste below code
 
@echo off
cd\
setlocal
 
 
 

call:pwd pass
call:pwd pass2
cd /d %~dp0
IF "%pass%" == "%pass2%" ( echo.Passwords entered are matched, Hurray!!!
java —jar passwordEncoder.jar %pass% ) ELSE (
echo.Passwords entered does not matched... Try with correct password again!!!
pause
EXIT
)
pause

 

GOTO:EOF
:pwd var title color —— shows a password dialog box
:$created 20060101 :$changed 20080226 :$categories Input,Password
SETLOCAL
 

 

set "tit=%~2"
set "col=%~3"
set "pwd="
if not defined col set "col=EF"
if not defined tit set "tit=Password:"
set "f=%temp%\%~nx0.tmp~0.tmp"
start "%tit%" /wait cmd /c "mode con cols=50 lines=1&color %col%&set /p "in="&call echo.%%in%%>"%f%""
for /f "usebackq tokens=*" %%a in ("%f%") do set "pwd=%%a"
del /q "%f%"
ENDLOCAL&if "%~1" NEQ "" (SET %~1=%pwd%) ELSE ECHO.%txt%
EXIT /b
 
 
Step 8:
Create a short for the bat file and change icon as you like
 
Step 9:
Copy the shortcut file of bat and paste under all programs of you windows
 
Your Utility is ready... Go ahead and enjoy your own utility
 
Advantages:
1. You can enter string even during screen share. Text you're entering won't be displayed to all
2. Utility will ask for your password confirmation twice, which avoids typo error encryptions
 
"A good threat is worth a thousand tests" - Boris Beizer

Saturday, August 20, 2016

Update TestSet Test status and upload file in ALM 11 OTA code using JACOBS (using Java)

Hi All,

Using VB, C#, VBScript to access COM is very straight forward and we can easily develop scripts/code based on Application API, you can get intellisense in IDEs, Excel Macros, etc., So, all methods are visible and documentation would be easily to find on each of them.

But, When we got situation to work on any COM using Java, it's not so easy as with VB/C#. we've multiple ways to deal with COM with Java, e.g. COM4j, JACOBS, etc., each jar has their own methods and way to access COM.

Mainly, now a days Web Automation is looking forward to Selenium - Java, But most of the projects would be running on ALM. Expectations from managements as to use Selenium, But integration with ALM for Test management.

I would like to show you guys how we can Access ALM - Testset(TestLab) Test and update test results, Attach any file in Test attachment with description.

Note: you can tweak this as per your need. 

You must have Jacob dlls (32 and 64 bit) and place as shown below.


 /**
 * PreCondition to ALM Update code - Initialize Jacobs
 * */
import com.jacob.com.LibraryLoader;

public class Core extends AppVariables{
    public void initializeJacobs(){
        String jacobDllVersionToUse;
        if (jvmBitVersion().contains("32")){
            jacobDllVersionToUse = "jacob-1.18-x86.dll";
        }
        else {
            jacobDllVersionToUse = "jacob-1.18-x64.dll";
        }
        File file = new File("Lib", jacobDllVersionToUse);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
    }
    public String jvmBitVersion(){
        return System.getProperty("sun.arch.data.model");
    }
}


/**
 * Actual Class which has Code implementation
 * */
import java.text.DateFormat;
import java.util.Calendar;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
//import com.sun.jna.platform.win32.COM.TypeInfoUtil.Invoke;

import frameworkComponents.Core;

public class ALMTesting extends Core{

    /**
     * @param args
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Core cfw = new Core();
        cfw.initializeJacobs();

        uploadFileToTestSetTest("http://win-2008server:8080/qcbin/", "DEFAULT", "Testing", "Baba", "baba", "Root\\Testing", "TestingForJACOBS", "Sample-SeleniumTest", "C:\\Users\\Baba\\Desktop\\SQL_SA_pwd.txt");
    }
  
    @SuppressWarnings("deprecation")
    static void uploadFileToTestSetTest(String almURL, String domain, String project, String almUserName, String almEncryptedPassword, String testFolder, String testSetName, String testName, String uploadFilePath){
        ActiveXComponent almConnection=new ActiveXComponent("TDAPIOLE80.TDConnection");
        Dispatch.call(almConnection, "InitConnectionEx", almURL);      
        Dispatch.call(almConnection, "login", almUserName,almEncryptedPassword);
        Dispatch.call(almConnection, "connect",domain,project);

        /**By me on Test set factory*/
        //        Dispatch testLab = Dispatch.get(almConnection, "TestSetFactory").toDispatch();
        //        Dispatch testSetFilter = Dispatch.get(testLab, "Filter").toDispatch();

//        String testFolder = testFolder;
//        String testSetName = testSetName;
//        String testName = testName;
//        String uploadFilePath = uploadFilePath;

        Dispatch testSetTreeManager = Dispatch.get(almConnection, "TestSetTreeManager").toDispatch();
        Dispatch tsFolder = Dispatch.call(testSetTreeManager, "NodeByPath", testFolder).toDispatch();
        //Dispatch subFolderNode = Dispatch.get(tsFolder, "NewList").toDispatch();
        Dispatch testSetNode = Dispatch.get(tsFolder, "TestSetFactory").toDispatch();
        Dispatch testSetList = Dispatch.call(testSetNode, "NewList", "").toDispatch();
        //        Dispatch tsList = Dispatch.call(tsFolder, "FindTestSets", testSetName).toDispatch();

        System.out.println("till here good");
        Variant count = Dispatch.get(testSetList, "Count");
        System.out.println(count);
        int toInt = count.toInt();
        //        String[] testNames = new String[toInt];
        boolean blnFlag = false;
        for (int testSetIndex = 1; testSetIndex <= toInt; testSetIndex++ ) {
            Dispatch testSetItem = Dispatch.call(testSetList, "Item", new Variant(testSetIndex)).toDispatch();
            Variant testSetActName = Dispatch.get(testSetItem, "Name");
            System.out.println(testSetActName);
            if (testSetActName.toString().equalsIgnoreCase(testSetName)){
                Dispatch testSetTestFactory = Dispatch.get(testSetItem, "TSTestFactory").toDispatch();
                Dispatch testList = Dispatch.call(testSetTestFactory, "NewList", "").toDispatch();
                Variant testsCount = Dispatch.get(testList, "Count");
                System.out.println(testsCount);
                int testsCountInt = testsCount.toInt();
                for(int testIndex=1; testIndex<=testsCountInt; testIndex++){
                    Dispatch testItem = Dispatch.call(testList, "Item", new Variant(testIndex)).toDispatch();
                    Variant testActName = Dispatch.get(testItem, "TestName");
                    System.out.println(testActName);
                    if(testActName.toString().equalsIgnoreCase(testName)){
                      
                        try{
                            Dispatch nowTest = testItem;
                            Dispatch attachmentFactory = Dispatch.get(testItem, "Attachments").toDispatch();
                            //                          Dispatch attachList = Dispatch.call(attachmentFactory, "NewList", "").toDispatch();

//                            Dispatch.put(nowTest, "Status", "Passed");
//                            Dispatch.call(nowTest, "Post");

                            //System.DBNull.Value
                            Variant paramVal = new Variant();
                            paramVal.putNull();
                            Dispatch nowAttachment = Dispatch.call(attachmentFactory, "AddItem", paramVal).toDispatch();
                            Dispatch.put(nowAttachment, "Description", Calendar.getInstance().getTime());
                            Dispatch.put(nowAttachment, "Filename", uploadFilePath);
                            Dispatch.put(nowAttachment, "Type", 1);
                            Dispatch.call(nowAttachment, "Post");
                          
                            /** Updating steps in this test*/
                            // Create run factory for this test
                            Dispatch runF = Dispatch.get(testItem, "RunFactory").toDispatch();
                            // Add a name to this runfactory test
                            Dispatch runA = Dispatch.call(runF, "AddItem", "Automated").toDispatch();
                            Dispatch runAA = Dispatch.call(runF, "NewList","").toDispatch();
                          
                            Variant runCount = Dispatch.get(runAA, "Count");
                            System.out.println("Run Count : "+ runCount);
                            int runsCountInt = runCount.toInt();
                          
                            for(int run=1; run<=runsCountInt; run++){
                                Dispatch runItem = Dispatch.call(runAA, "Item", new Variant(run)).toDispatch();
                                Variant runName = Dispatch.get(runItem, "Name");
                                System.out.println(runName);
                              
                                // Create Stepfactory for this Run
                                Dispatch oStep = Dispatch.get(runItem, "StepFactory").toDispatch();
                                //Create a step
                                Dispatch.call(oStep, "AddItem", "Test case result");
                                Dispatch oStepDetails = Dispatch.call(oStep, "NewList", "").toDispatch();
                              
                                Dispatch.put(runItem, "Status", "Passed");
                                Dispatch.call(runItem, "Post");
                                // Create Stepfactory for this Run
//                                Dispatch oStep = Dispatch.get(runA, "StepFactory").toDispatch();
                              
//                                Dispatch oStepDetails = Dispatch.call(oStep, "NewList", "").toDispatch();
                              
                                // Update all steps to pass
                                Variant stepsCount=Dispatch.get(oStepDetails, "Count");
                                System.out.println("No of Step is - "+stepsCount);
                                int stepsCountInt = stepsCount.toInt();
                                for(int stepIndex=1; stepIndex<=stepsCountInt; stepIndex++){
                                    Dispatch stepItem = Dispatch.call(oStepDetails,"Item",new Variant(stepIndex)).toDispatch();
                                    Variant stepActName = Dispatch.get(stepItem, "Name");
                                    System.out.println("Step Name is : "+stepActName);
                                    try{
                                        Dispatch.put(stepItem, "Status", "Passed");
                                        Dispatch.call(stepItem, "Post");
                                    }catch(ComFailException i){
                                        System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                                        Dispatch.call(almConnection, "Disconnect");
                                        Dispatch.call(almConnection, "Logout");
                                        Dispatch.call(almConnection, "ReleaseConnection");
                                        System.exit(-1);
                                    }
                                }
                                break;
                            }
                          
                          
                            blnFlag=true;
                            break;
                        }catch(ComFailException i){
                            System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                            Dispatch.call(almConnection, "Disconnect");
                            Dispatch.call(almConnection, "Logout");
                            Dispatch.call(almConnection, "ReleaseConnection");
                            System.exit(-1);
                        }
                    }
                }
                if(blnFlag) break;
            }
        }

        Dispatch.call(almConnection, "Disconnect");
        Dispatch.call(almConnection, "Logout");
        Dispatch.call(almConnection, "ReleaseConnection");
        System.out.println("updates are done");
    }
}

"A good threat is worth a thousand tests" - Boris Beizer
There was an error in this gadget