Showing posts with label filehandling. Show all posts
Showing posts with label filehandling. Show all posts

Tuesday, July 15, 2008

Silktest - File Download window

Sometimes Silktest is not identifying FileDownload objects properly. To save it, we need to use TypeKeys. I used following code snippet to save a file from Browser.

Sample 4Test code



[+] Boolean SaveFile (String sFile)
[ ] Boolean bResult = FALSE
[+] do
[+] if (FileDownload.Exists(1))
[ ] Print ("FileDownload exists.")
[ ] FileDownload.SetActive ()
[ ] Sleep (1)
[ ] FileDownload.Save.Click (1,10,3)
[ ] Sleep (3)
[ ]
[ ] Window wActive = Desktop.GetActive ()
[ ] Print ("Window: {wActive}")
[ ] // DialogBox("File Download").DialogBox("Save As")
[ ] wActive.TypeKeys (sFile)
[ ] wActive.TypeKeys ("", 1)
[ ] wActive.TypeKeys ("", 1)
[ ] wActive.TypeKeys ("S",1)
[ ]
[ ] bResult = TRUE

[+] else
[ ] Print ("BrowserFileDownload does not exist.")
[+] except
[ ] ExceptLog ()
[ ]
[ ] return bResult
[ ]

Wednesday, June 4, 2008

Silktest - File Handling

Any automation suite must have file handling functions for various scenarios. It may be just text files or any data files. Silktest also has few methods for file handling.

SYS_FileOpen is executed by the Agent process and not by the SilkTest host process. But it is essentially the same as FileOpen. SYS_FileOpen and SYS_FileClose should be used to do file operations in other machines (while the agents are running from other machines ), not in the host machine. Below few 4test code samples are given.

4Test Sample Code: To Read a text file.

[+] public void ReadTxtFile(STRING sFile) [ ] // Purpose: Read contents of a file. [ ] HFILE hFile [ ] String sLine [ ] [ ] hFile = FileOpen (sFile, FM_READ) [ ] Print ("File: {sFile}") [ ] [-] while (FileReadLine (hFile, sLine)) [ ] Print (sLine) [ ] [ ] FileClose (hFile) [ ]

4Test Sample Code: To Create a text file.
[+] public void CreateTxtFile(STRING sFile, STRING sMsg) [ ] // Purpose: Create a txt file. All contents will be removed [ ] // from that file, if that file exists already. [ ] HFILE hFile [ ] String sLine [ ] [ ] hFile = FileOpen (sFile, FM_WRITE) [ ] FileWriteLine (hFile, sMsg) [ ] FileClose (hFile)

SilkTest Sample Code: To append a text file.
[+] public void AppendTxtFile(STRING sFile, STRING sMsg) [ ] // Purpose: Create a txt file. Given string will be appended [ ] // from last line of the file, if that file exists already. [ ] HFILE hFile [ ] String sLine [ ] [ ] hFile = FileOpen (sFile, FM_APPEND) [ ] FileWriteLine (hFile, sMsg) [ ] FileClose (hFile) [ ]

Silk Test Sample Code: To Delete a text file.
[-] void DeleteFile (String sFileName) [ ] // To delete given file, if it exists [-] if (Sys_FileExists(sFileName)) [ ] Print ("Existing file - {sFileName}") [ ] Sys_RemoveFile (sFileName) [ ] Sleep (0.1) [-] if (Sys_FileExists(sFileName)) [ ] Print ("Could not delete file - {sFileName}") [-] else [ ] Print ("Deleted file - {sFileName}") [-] else [ ] Print ("File {sFileName} does not exist.")

Silktest Sample Code: To Separate filename and path.
[+] public void GetFileNameAndPath (String sFileWithPath, inout String sFileName null optional, inout String sFilePath null optional) [ ] // Purpose: To separate filname and path. [ ] String sTmpFile, sTmpPath, sTemp [ ] Integer iPos,iCount = 0 [ ] [ ] sTemp = sFileWithPath [ ] iPos = strPos ("\",sTemp, TRUE) [-] if (iPos > 0) [ ] sFileName = Substr (sTemp,iPos + 1) [ ] sFilePath = Left (sTemp,iPos -1) [-] else [ ] Print (" File {sFileWithPath} does not have File Separator")

Tuesday, May 20, 2008

Winrunner - File Handling

Winrunner code is similar to 'C' code. Any automation suite must have file handling functions for various scenarios. It may be just text files or any data files. Winrunner also has few methods for file handling. Below few sample code snippets are given..

Code: To read a file

#--------------------------------- # Method : fileRead # Author : T.Palani Selvam # Purpose : Read all contents of a file. # Parameters: String, contains file name. # # Returns : - Nil - # Caller : # Calls : - Nil - #---------------------------------- public function fileRead(in m_sFile) { auto iLine; auto sEachLine; auto sFileContent; printf("fileRead is going to process for " & m_sFile); file_open(m_sFile,FO_MODE_READ); iLine = 0; while(file_getline(m_sFile, sEachLine) == 0) { sFileContent = sFileContent & sEachLine; iLine++; printf(sEachLine); } #report_msg(sFileContent); #Line is too length file_close(m_sFile); } #end of fileRead(in m_sFile)


Code: To write info to a file
#------------------------------ # Method : fileWrite # Author : T.Palani Selvam # Purpose : Write Given inputs to a file. # Parameters: String, contains file name. # # Returns : - Nil - # Caller : # Calls : - Nil - #----------------------------- public function fileWrite(inout m_sFile) { auto iLine; auto sEachLine; auto iBool; auto null=""; printf("fileWrite is going to process for " & m_sFile); file_open(m_sFile, FO_MODE_WRITE); iLine = 0; do { sEachLine = create_input_dialog("Enter Input to write in " & m_sFile); if (sEachLine == null ) { #equals to NULL iBool = 0; } else { iBool = 1; iLine = iLine + 1; file_printf(m_sFile,"%s",sEachLine & "\r\n"); printf(sEachLine); } } while(iBool == 1); file_close(m_sFile); #Closing file } #end of fileWrite(in m_sFile)


Code: To Append info to a file
#----------------------------------- # Method : fileAppend # Author : T.Palani Selvam # Purpose : Write Given inputs to a file. # Parameters: String, contains file name. # # Returns : - Nil - # Caller : # Calls : - Nil - #---------------------------------- public function fileAppend(inout m_sFile) { auto iLine; auto sEachLine; auto iBool; auto null=""; printf("fileAppend is going to process for " & m_sFile); file_open(m_sFile, FO_MODE_APPEND); iLine = 0; do { sEachLine = create_input_dialog("Enter value to append in " & m_sFile); if (sEachLine == null ) { #equals to NULL iBool =0; } else { iBool = 1; iLine = iLine + 1; file_printf(m_sFile,"%s",sEachLine & "\r\n"); printf(sEachLine); } } while(iBool == 1); file_close(m_sFile); } #end of fileAppend(in m_sFile)

Friday, May 2, 2008

VBScript - File Read and Write

I have developed few projects using Visual Basic. But I do not have much scripting in Visual Basic Script (VBS). In VB, the file handling functions are different than VBScript. We need to use FileSystemObject for file handling. Below I have given two code snippets to read and write into text files. This code can be used into Quick Test Professional also.

Reading a Text file



Const ForReading = 1, ForWriting = 2

FileName = "D:\VBs\Mysamples\1create.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(FileName) Then

Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)

Do while Not objTextFile.AtEndOfStream
sLine = objTextFile.ReadLine
Wscript.Echo sLine
loop

End If

objTextFile.Close

Set objFile = Nothing
Set objFSO = Nothing


Writing to a Text file


Const ForAppending = 8
FileName = "D:\VB\Mysamples\1create.txt"

Set objFSO = CreateObject("scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (FileName, ForAppending, True)

For iIndex=1 to 10
objTextFile.WriteLine(iIndex)
Next

objTextFile.Close

Set objTextFile = Nothing
Set objFSO = Nothing

Saturday, April 5, 2008

VisualTest - File Handling

Any automation suite must have file handling functions for various scenarios. It may be just text files or any data files. VisualTest also has few methods for file handling. VisualTest code is similar to Visual Basic code. Below I put some of my visual test functions.

Code: Check the given file exists or not



'----------------------------------
' Method : FileExists
' Author : T.Palani Selvam
' Purpose : Check the given file exists or not. PhaseII
' Parameters: sFile, String - Contains a file name.
' Returns : Returns Integer data type(True or False.)
' Caller : SwitchSpecialTags
' Calls : - Nil
'-----------------------------
Function FileExists(sFile As String)As Integer
Dim iPosition As Integer
Dim sDir As String, sTFile As String, sTemp As String

FileExists = False
sTFile = sFile
iPosition = Instr(sFile,"::")
If (iPosition <> 0) Then
sDir = Mid$(sFile,iPosition + 2)
sTemp = Left(sFile, iPosition - 1)
If (Instr(sTemp,":\") <> 0 ) Then
sTFile = sTemp
Else
If (Right$(Trim(sDir),1) <> "\") Then
sDir = Trim(sDir) + "\"
End If
sTFile = sDir + sTemp
End IF
End If
If Exists(sTFile,"-d") Then
FileExists = True
Logwrite( "Successful - Existing file is " + sTFile , 1)
Else
Logwrite( sTFile + " - File doesn't exist.", 1)
End IF

End Function


Code: Check whether the given folder exists or not


'----------------------------
' Method : FolderExists
' Author : T.Palani Selvam
' Purpose : Check the given folder exists or not.(PhaseII)
' Parameters: sFolder, String - Contains a folder name.
' Returns : Returns Integer data type(True or False.)
' Caller : SwitchSpecialTags
' Calls : - Nil
'---------------------------
Function FolderExists(sFolder As String)As Integer
FolderExists = False
If (Right$(Trim(sFolder),1) = "\") Then
sFolder = Trim(sFolder)
sFolder = Left$(sFolder, Len(sFolder) -1 )
End If

If Exists(sFolder,"+d") Then
FolderExists = True
Logwrite( "Successful - Existing Directory is " + sFolder , 1)
Else
Logwrite( sFolder + " - Directory doesn't exist.", 1)
End IF
End Function


Code: Check whether the given string exists in the given file or not.


'-----------------------------
' Method : StringExists
' Author : T.Palani Selvam
' Purpose : 'Check whether the given string exists in the given file or not.
' Parameters: sFile as String, Contains a file name with full path.
' sWord - String , Consists of string to check into the file.
' Returns : Returns Integer value
' Caller : OperationIdentifier()
' Calls : - Nil
'-------------------------
Function StringExists(sFile As String,sWord As String)As Integer
Dim fNumber As Long
Dim sLine As String
Dim iInt As Integer

fNumber=FREEFILE
StringExists=False

iInt =0

OPEN sFile For Input As #fNumber

While not eof(fNumber)
Line Input #fNumber,sLine
sLine = Trim (sLine)

IF (Instr(sWord,sLine)<>0 AND Len(sLine) > 1) Then
PRINT sLine, "String Exists!"
iInt = iInt + 1
If (iInt > 3) Then
StringExists=True
Exit Function
End IF
End If
Wend

CLOSE #fNumber

End Function