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")

1 comment:

Sat said...

This is a very nice blog with all the code snippets and upto the point unlike some other blog where they write stories and make it boring. Keep up the good work. I am going to bookmark this. Thanks