Automation Beyond

Testing and Test Automation for Software Quality Assurance. Above Requirements. Beyond Expectations. Methodologies and concepts. Framework Design. Programming and Scripting. Created by Albert Gareev.

GP/QTP Automation: Execute Dexterity Macro

Posted by Albert Gareev on 2009/12/23

All related posts: Reference Page – GP/QTP Automation 

Dexterity Macro Limitation and Suggested Workaround

One of the major limitations identified in Dexterity Macro System is totally hard-coded data with no functionalities to read them from any external source. To overcome this issue we can dynamically generate Dexterity macro text file from a pre-built XML template. After the file is generated controller script (driven by QTP) may invoke it through COM interface.

VBA/VBScript functions

VBA function – put in your Excel VBA macro file

Note. Stores error code and error description (if) returned.


Public Function RunDexterityMacro()
    Dim intRC As Integer
    Dim sCode, sErrMsg As String
    Dim sPath

    sPath = Workbooks.Item(1).Sheets.Item(1).Cells(2, 1)
    sCode = "run macro " & """" & sPath & """" & ";"
   
    Call GPApp.Activate
    intRC = GPApp.ExecuteSanScript(sCode, sErrMsg)
    If intRC <> 0 Then
        'Store result
        Workbooks.Item(1).Sheets.Item(1).Cells(2, 5) = intRC
        Workbooks.Item(1).Sheets.Item(1).Cells(2, 4) = sErrMsg
        Exit Function
    End If
   
    'Clear result
    Workbooks.Item(1).Sheets.Item(1).Cells(2, 5) = 0
   
End Function

VBScript function – put as Dexterity Interface Class method

Note. If you use it as an embedded function don’t forget to call COM initialization procedures first.


Public Function RunDexterityMacro(ByVal sMacroName)
Dim boolRC, intRC
Dim objUsedRange, objRecord

Set objUsedRange = XLBook.Worksheets(1).UsedRange()
objUsedRange.Cells(2,1) = sMacroName

XLHandle.Run("RunDexterityMacro")

Set objRecord = RetrieveParameters()
intRC = objRecord.Item("Var5")
If intRC <> 0 Then
RunDexterityMacro = FALSE
End If

RunDexterityMacro = TRUE
End Function

A function to execute Dexterity Macro
sTemplateName – full path and name of the XML template file
sMacroName – Dexterity Macro file to create

Uses Service Functions – System (QTP, VBScript)


Public Function GP_RunMacro(ByVal sTemplateName, ByVal sMacroName)
Dim boolRC, intRC
Dim sFullPath, sSubfolder
Dim sLogFile
Dim sCallName, sDexCallName, sDrive
Dim FSO

'Check file exists
Set FSO = CreateObject("Scripting.FileSystemObject")
boolRC = FSO.FileExists(sTemplateName)
If Not boolRC Then
Set FSO = Nothing
' Put your reporting function here "Failed to locate template file " & sTemplateName
Exit Function
End If

'Create subfolder
sFullPath = SubfolderCreate(sMacroName, sSubfolder)

'Substitute GP Macro Log filename
sLogFile = sFullPath & sMacroName & "_log.txt"
sLogFile = Replace(sLogFile, "\\", "\")
sDrive = ":" & Left(sLogFile, 2)
sLogFile = sDrive & Replace(Mid(sLogFile, 4), "\", "/")

'You should store sLogFile in the data source object according to your data model

'Generate macro file
sCallName = sFullPath & sMacroName & ".mac"
sCallName = Replace(sCallName, "\\", "\")
boolRC = GenerateTextFile(sTemplateName, sCallName)

If Not boolRC Then
Set FSO = Nothing
' Put your reporting function here "Failed to generate macro" & sCallName
Exit Function
End If

sDrive = ":" & Left(sCallName, 2)
sDexCallName = sDrive & Replace(Mid(sCallName, 4), "\", "/")

'Execute
boolRC = GPHandle.RunDexterityMacro(sDexCallName)

'reporting
If boolRC Then
'PASS
' Put your reporting function here "Dexterity Macro execution successful"
Else
'FAIL
' Put your reporting function here "Dexterity Macro execution failed"
End If

End Function

Log file, created by Great Plains Dexterity, will contain all response messages and screen dump data (if you used the according macro commands).

Posted in Automation, Code Examples, MS Dynamics Great Plains | Tagged: , , , , , , , , , , , , , , | 1 Comment »

How to use loops in Dexterity Macro

Posted by Albert Gareev on 2009/12/21

All related posts: Reference Page – GP/QTP Automation

Answer

According to the documentation, there are no loop operators defined in Microsoft Dynamics Great Plains Dexterity Macros.

However, the macro language provides other commands to workaround the issues in the most required cases.

Workarounds

1. Capture values of GUI fields (in a loop)

Use DumpFieldBeginDumpFieldEnd commands.

2. Window synchronization

Use ActivateWindow command, repeated 5-10 times

3. GUI Control synchronization and triggering

Use MoveTo command, repeated a few times.

Move focus in or away to trigger event handlers.

Reference

MS Dynamics Great Plains: Built-in Macro System

Posted in Automation, How to, MS Dynamics Great Plains | Tagged: , , , , , , , , , , , , , | 1 Comment »

How to create a unique subfolder (QTP, VBScript)

Posted by Albert Gareev on 2009/12/16

Parent page: Service Functions – System (QTP, VBScript)

How to create a unique subfolder

Description

A subfolder is created based on the name pattern and a date-time stamp, e.g. “AccountSearch 2009-10-14 17-18-03″.  You can add extra steps and form the code as a function that returns full path of the newly created subfolder.

sName – name pattern

sFullPath - full path of the newly created subfolder


Dim date_now, dd, dt
Dim FSO
Dim sFullPath

date_now = Now

dd = Year(date_now) & "-" & AlignNumber(Month(date_now), 2, "0") & "-" & AlignNumber(Day(date_now), 2, "0")
dt = AlignNumber(Hour(date_now), 2, "0") & "-" & AlignNumber(Minute(date_now), 2, "0") & "-" & AlignNumber(Second(date_now), 2, "0")

If sName = "" Then
sSubfolder = "Data"&" " & dd & " " & dt
Else
sSubfolder = sName & " " & dd & " " & dt
End If
sFullPath = sLogPath & "\" & sSubfolder & "\"

Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CreateFolder(sFullPath)
Set FSO = Nothing

Posted in Automation, Code Examples, How to | Tagged: , , , , , , , , , , , , | 1 Comment »

How to duplicate an existing file (QTP, VBScript)

Posted by Albert Gareev on 2009/12/15

Parent page: Service Functions – System (QTP, VBScript)

How to duplicate an existing file

(If you want to access the file but it’s locked)

Description

sFileName – name of the file to duplicate
sNewName – name of created file (generated automatically), pass-back argument.

Uses UniqueFilename function to generate a name for the duplicate file.

Public Function DuplicateFile(ByVal sFileName, ByRef sNewName)
Dim boolRC
Dim FSO, objFile
Dim sName, sPath</pre>
Set FSO = CreateObject("Scripting.FileSystemObject")

'Verify source file exists
boolRC = FSO.FileExists(sFileName)
If Not boolRC Then
Set FSO = Nothing
DuplicateFile = FALSE
Exit Function
End If

'Separate filename and filepath
Set objFile = FSO.GetFile(sFileName)
sName = objFile.Name
sPath = objFile.Path
sPath = Left(sPath, Len(sPath)-Len(sName))
Set objFile = Nothing

'Generate unique name for the dup file
sNewName = sPath & "\" _
_ & UniqueFilename(sName, sPath, AssociateParameters("prefix = dup, index = 100"))

'Copy file
FSO.CopyFile sFileName, sNewName

Set FSO = Nothing
DuplicateFile = TRUE
End Function

Posted in Automation, Code Examples, How to | Tagged: , , , , , , , , , , , | 1 Comment »

How to generate unique file name (QTP, VBScript)

Posted by Albert Gareev on 2009/12/14

Parent page: Service Functions – System (QTP, VBScript)

How to generate a unique file name
if the file name already exists in the given folder

(That often happens, if you need to create a series of files or duplicate an existing file)

Description

sFileName – the “original” file name or file name template
sFolderName – parent folder (full path required)
objParameter – Dictionary object containing optional parameters.

  • Prefix. If you want the file name to be preceded with some text (e.g. copy_userdata.xls) define a prefix.
  • Index. If you want to define starting number in the series (e.g. userdata_100.xls) use index.

Note. The function does not generate a file. It generates a file name!

Example. Duplicate an existing file


Public Function UniqueFilename(ByVal sFileName, ByVal sFolderName, ByVal objParameter)

Dim FSO, boolRC, sTypeName
Dim index,prefix
Dim sNewName, BaseName, Extension

Set FSO = CreateObject("Scripting.FileSystemObject")
'to check if the objParameter passed is a dictionary object
sTypeName = TypeName (objParameter)
If sTypeName <> "Dictionary" Then
Set objParameter = CreateObject("Scripting.Dictionary")
End If

'retrieving and defaulting the parameters from objParameter
prefix = objParameter.item("prefix")
If prefix <> "" Then prefix = prefix & "_"

index = objParameter.item("index")
If Not IsNumeric(index) Then index = 1

'Separate filename and extension
BaseName = FSO.GetBaseName(sFileName)
Extension = FSO.GetExtensionName(sFileName)

'Generate filename and verify it doesn't exist

Do
sNewName = prefix & BaseName & "_" & Cstr(index) & "." & Extension
boolRC = FSO.FileExists(sFolderName & "\" & sNewName)
index = index+1
Loop Until boolRC = FALSE

Set FSO = Nothing
UniqueFilename = sNewName
End Function

Posted in Automation, Code Examples, How to | Tagged: , , , , , , , , , | 2 Comments »