My Blog List

Thursday, October 24, 2013

Control the text file pointer

We could have faced a problem like getting back the reading location of a opened text file to re-read or re-search the file from beginning ...
We don't have a right approach to do this in VBScript using FileSystemObject without close and reopening the file. But we could do using STREAM object without reopen ...

Dim myFileStream
Set myFileStream = CreateObject("ADODB.Stream")
myFileStream.CharSet = "UTF-8"
myFileStream.LineSeparator = 13
myFileStream.Open
myFileStream.LoadFromFile("C:\MyFile.txt")

msgbox myFileStream.ReadText(-2) 'Returns first line

myFileStream.Position = 0
msgbox myFileStream.ReadText(-2) 'returns first line again as expected
myFileStream.Close

posted from Bloggeroid

Tuesday, August 6, 2013

Convert Image Formats

You could face situations like changing the format of the image file or reduce the file size by changing the formats. Here is the way to get it done in simple way.


Function ConvertImage(strFromFile, strToFile)
 Dim oImage, oFileImage, oImageFormats, oFSO, strToExtn

 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oImage = DotNetFactory.CreateInstance("System.Drawing.Image")
 Set oFileImage =  oImage.FromFile(strFromFile)
 Set oImageFormats = DotNetFactory.CreateInstance("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing)

 strToExtn = oFSO.GetExtensionName(strToFile)
 Select Case UCase(Trim(strToExtn))
  Case "JPEG"
   oFileImage.Save strToFile, oImageFormats.Jpeg
  Case "JPG"
   oFileImage.Save strToFile, oImageFormats.Jpeg
  Case "BMP"
   oFileImage.Save strToFile, oImageFormats.Bmp
  Case "PNG"
   oFileImage.Save strToFile, oImageFormats.Png
  Case "GIF"
   oFileImage.Save strToFile, oImageFormats.Gif
 End Select

 'clear
 Set oFSO = Nothing
 Set oImage = Nothing
 Set oFileImage = Nothing
 Set oImageFormats = Nothing
End Function

Call ConvertImage("C:\sample.bmp", "C:\sample.jpeg")

Wednesday, January 2, 2013

Change IE Browser ZOOM settings

Lot of UI automation testers could have faced this problem as we could change the zoom settings while operating manually for our convenience and forgot to reset to 100%.
But our QTP and some other related tools would operate the browser perfectly if browser zoom is 100%. So wee need to change the zoom before start to run the scripts. Its better to have a code snippet in our framework to change this zoom setting right?
Here we go...

1. We can simply change the Registry values before Invoking IE
  1. Function ChangeRegistry
  2.  Dim objShell
  3.  Set objShell = CreateObject("WScript.Shell")
  4.  objShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom\ZoomFactor""100000""REG_DWORD"
  5.  Set objShell = Nothing
  6. End Function
This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try other options.

2. Use IE COM object and Change Settings
  1. Function ChangeIEZoom
  2.  Dim intZoomLevel, objIE
  3.  intZoomLevel = 100
  4.  Const OLECMDID_OPTICAL_ZOOM = 63
  5.  Const OLECMDEXECOPT_DONTPROMPTUSER = 2
  6.  Set objIE = CreateObject("InternetExplorer.Application")
  7.  objIE.Visible = True
  8.  objIE.Navigate "http://www.google.com"
  9.  While objIE.Busy = True
  10.   WScript.Sleep 100
  11.  Wend
  12.  objIE.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(intZoomLevel), vbNull
  13. End Function
Warnings:
1. Don't use Top, Left, Width and Height properties
objIE.Left = 0
objIE.Top = 0
objIE.Width = 1304
objIE.Height = 768

These things will not change zoom settings but sets the IE window size. You can use to maximize the IE window.
2. Don't use zoom property of page
document.body.style.zoom=1
This will not change the IE browser zoom setting but changes the currently displayed page size by keeping the same IE zoom.