by admin | Mar 10, 2016 | 2016 |
When we capture screenshots, we might need to draw some shape over the screenshot to highlight some errors or some object. In such cases, we can use Dot .Net factory wherein we can draw different shapes with different colors. The below code draws rectangle in an image.
Code
Function DrawRectangle(ByVal strImagePath,ByVal inX,ByVal inY,ByVal inHeight,ByVal inWidth,ByVal strColor)
Dim oPens,oGraphics,oImage,oBitmap,oRect,oFile
Set oImage=DotNetFactory.CreateInstance("System.Drawing.Image","System.Drawing")
Set oGraphics=DotNetFactory.CreateInstance("System.Drawing.Graphics","System.Drawing")
Set oRect=DotNetFactory.CreateInstance("System.Drawing.Rectangle","System.Drawing",inX,inY,inHeight,inWidth)
Set oPens=DotNetFactory.CreateInstance("System.Drawing.Pens","System.Drawing")
Set oFile=DotNetFactory.CreateInstance("System.IO.File","")
Set oImage=oImage.FromFile(strImagePath)
Set oBitmap=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",oImage)
Set oGraphics = oGraphics.FromImage(oBitmap)
Execute "oGraphics.DrawRectangle oPens."&strColor&", oRect"
oImage.Dispose
oFile.Delete(strImagePath)
oBitmap.Save(strImagePath)
Set oPens=Nothing
Set oGraphics=Nothing
Set oImage=Nothing
Set oFile=Nothing
Set oBitmap=Nothing
Set oRect=Nothing
End Function
DrawRectangle "C:img.jpg",20,20,40,40,"RED"
Synopsis
The DrawRectangle function requires the following six argument values to be passed.
Argument 1: Image path
Argument 2: X point
Argument 3: Y point
Argument 4: Rectangle Height
Argument 5: Rectangle Width
Argument 6: Color
Note: We can use multiple colors to draw shapes. Refer the below link for different colors.
http://msdn.microsoft.com/en-US/library/system.drawing.pens_members(v=vs.80).aspx
by admin | Mar 10, 2016 | 2016 |
In this article, we will see how to select WPF Checkbox using TogglePattern class which is available in UI Automation pattern.
Code
Function SelectCheckbox(ByVal inWindowHwnd,ByVal strCheckBoxName)
Dim oTreeScope, oAutomationElement,oControlType
Dim oTogglePattern, oInptr, oPropertyCondition
Set oControlType=DotNetFactory.CreateInstance("System.Windows.Automation.ControlType","UIAutomationTypes")
Set oTreeScope=DotNetFactory.CreateInstance("System.Windows.Automation.TreeScope","UIAutomationTypes")
Set oAutomationElement=DotNetFactory.CreateInstance("System.Windows.Automation.AutomationElement","UIAutomationClient")
Set oPropertyCondition=DotNetFactory.CreateInstance("System.Windows.Automation.PropertyCondition","UIAutomationClient",oAutomationElement.NameProperty,strCheckBoxName)
Set oTogglePattern=DotNetFactory.CreateInstance("System.Windows.Automation.TogglePattern","UIAutomationClient")
Set oInptr=DotNetFactory.CreateInstance("System.IntPtr","",inWindowHwnd)
Set oAutomationElement=oAutomationElement.FromHandle(oInptr)
Set oTogglePattern=oAutomationElement.FindAll(oTreeScope.Children,oPropertyCondition)(0).GetCurrentPattern(oTogglePattern.Pattern)
oTogglePattern.Toggle
Set oTreeScope=Nothing
Set oAutomationElement=Nothing
Set oControlType=Nothing
Set oTogglePattern=Nothing
Set oInptr=Nothing
Set oPropertyCondition=Nothing
End Function
Dim inWindowHandle
inWindowHandle=Window("text:=List of Products").GetRoProperty("hwnd")
SelectCheckbox inWindowHandle,"Show only bargains"
Synopsis
Once the above code is executed, “Show only bargains” checkbox will be checked. This “SelectCheckbox” function will check the checkbox if it is already unchecked and vice versa. So it is always recommended to make sure whether the checkbox is checked or not using the “ToggleState” property before calling the function. The ToggleState property for the object can be retrieved as given below.
Msgbox oTogglePattern.current.ToggleState
This implementation for the checkbox is as same as selecting a radio button but the only difference is Toggle Pattern is used for Checkbox.
by admin | Mar 10, 2016 | 2016 |
Sometimes we may need to generate a random file name. In VBScript we can use Scriptlet.TypeLib to generate GUID for random data, but length of the GUID is too long.
If we need a shorter random file name, it can be generated using GetRandomFileName method.
Code
Dim oPath,strFileName
'Creating instance of System.IO.Path class which contains GetRandomFileName method
Set oPath=DotNetFactory.CreateInstance("System.IO.Path")
'Assigning random filename in a variable using GetRandomFileName method
strFileName=oPath.GetRandomFileName
Msgbox strFileName
Set oPath=Nothing
Although there are several ways to generate random file name, this technique can be used where we require a shorter random data to be used as file name.
by admin | Mar 10, 2016 | 2016 |
As a continuation to the previous WPF automation article. In this topic, we will see how to set value in WPF Textbox.
Let’s see how it can be implemented using System.Windows.Automation.ValuePattern class.
To explain this implementation, we have used the below application to set the value in Textbox.
Code
'WPF_SetValue function with three arguments
'Argument 1: inWindowHwnd - Window Handle
'Argument 2: inTextBoxIndex - Index of TextBox
'Argument 3: strValue - Value to be set
Function WPF_SetValue(ByVal inWindowHwnd,ByVal inTextBoxIndex,ByVal strValue)
Dim oTreeScope, oAutomationElement,oControlType
Dim oInptr, oPropertyCondition, oValuePattern
'Creating object for ControlType class since the textbox will be identified with Edit Control Type.
Set oControlType=DotNetFactory.CreateInstance("System.Windows.Automation.ControlType","UIAutomationTypes")
'TreeScope object to specify scope of elements
Set oTreeScope=DotNetFactory.CreateInstance("System.Windows.Automation.TreeScope","UIAutomationTypes")
'AutomationElement object is used to retrieve the element
Set oAutomationElement=DotNetFactory.CreateInstance("System.Windows.Automation.AutomationElement","UIAutomationClient")
'PropertyCondition object is used to filter the textbox with ControlType property.
Set oPropertyCondition=DotNetFactory.CreateInstance(" System.Windows.Automation.PropertyCondition","UIAutomationClient",oAutomationElement.ControlTypeProperty,oControlType.Edit)
'Since TextBox control comes under ValuePattern, we need this object to set value in textbox.
Set oValuePattern=DotNetFactory.CreateInstance("System.Windows.Automation.ValuePattern","UIAutomationClient")
'In .Net, Window Handle cannot be taken as Integer or String, it should be passed as IntPtr structure. That is why here inWindowHwnd argument is supplied as constructor for IntPtr object.
Set oInptr=DotNetFactory.CreateInstance("System.IntPtr","",inWindowHwnd)
'Getting the WPF Form using FromHandle method.
Set oAutomationElement=oAutomationElement.FromHandle(oInptr)
'Finding the textbox with Control Type and assigning into oValuePattern object
Set oValuePattern=oAutomationElement.FindAll(oTreeScope.Children,oPropertyCondition)(inTextBoxIndex-1).GetCurrentPattern(oValuePattern.Pattern)
'Setting value in the textbox
oValuePattern.SetValue(strValue)
Set oTreeScope=Nothing
Set oControlType=Nothing
Set oAutomationElement=Nothing
Set oPropertyCondition=Nothing
Set oValuePattern=Nothing
Set oInptr=Nothing
End Function
Now to set the value in the WPF Textbox, We need to get the window handle for the WPF Application and then call the above function. We pass three arguments to the function:
1.Window Handle
2.Index of the Textbox : Since ‘Email’ is the first textbox available in the window, we pass the index argument value as 1
3.Value to be set in the textbox. Eg. “[email protected]”
Dim inWindowHandle
inWindowHandle=Window("text:= ExpenseIt Standalone").GetRoProperty("hwnd")
'Call WPF_SetValue function with Window Handle, textBox Index and value
WPF_SetValue inWindowHandle,1,"[email protected]"
Synopsis
Once the above code is run, the value specified will be set in the textbox. In the above function, the textbox is identified in the Window with the help of ControlTypeProperty.
by admin | Mar 10, 2016 | 2016 |
Downloading a file is very simple in .NET and only few lines are required.
Code
Dim oNetwork,strServerLocation,strClientLocation
'URL of the file which you want to download
strServerLocation="http://ist.psu.edu/current-students/pdf/resume-objective-
statements.pdf"
'Specify where to save it along with the file name including file extension.
strClientLocation="C:DownloadHere.pdf"
'Creating Network object
Set oNetwork=
DotNetFactory.CreateInstance("Microsoft.VisualBasic.Devices.Network","Microsoft.Vi
sualBasic")
'Downloading the file into specified client location
oNetwork.DownloadFile strServerLocation,strClientLocation
Set oNetwork =Nothing
Synopsis
Once the above code is run, the file will be downloaded at the specified client location.
We can also ping a server and upload a file using Network class.
by admin | Mar 10, 2016 | 2016 |
ArrayList is one of the collections in .Net which implements the IList interface; it has
more advantages than traditional array.
- . ArrayList size is increased dynamically whenever new item is added
- . If required, Array can be resized using TrimToSize method or by changing
Capacity property
- . It is capable to store anything as an item
- . Array items can be accessed using an Integer Index
- . It has more properties and methods than normal Array
Let us see how to create an ArrayList instance using DotNetFactory and sort the
elements in an ArrayList using the Sort method.
Code
Dim oArrayList
Set oArrayList=DotNetFactory.CreateInstance("System.Collections.ArrayList")
oArrayList.Add "Paris"
oArrayList.Add "London"
oArrayList.Add "New York"
oArrayList.Sort
Msgbox oArrayList(0)
Set oArrayList=Nothing
Synopsis
On running the above code, the three items will be added to the ArrayList and gets
sorted. We are just getting the 1st element in the ArrayList after sorting as the output
i.e. oArrayList(0). Here the index starts with 0. Before sorting, the 1st item in the
ArrayList will be “Paris”. Once the array is sorted, “London” will be the 1st item in the
ArrayList and will be displayed as the output.