Reading Code inside Action in QTP - vbscript

I want to read the code written inside an action in QTP,just as we read text file using FileSystem Object.
Is there any way to read code written in action line by line ?

So basically an action is attached to a QTP-Testcase. When you save this test case at a certain location, the code you have written is saved inside the directory named action1 or whatever action you were editing. In this folder you will find a file named script.AVCHD file. Open this in notepad and you will find your code inside it. Now all you got to do is write a simple visual basic, (or just any script you like) that will open this file and readall of the content into a variable. see if this helps. thanks.

For each test there will script.mts inside the Action1 folder.
get the script text line
filename = "C:\YourUFTTest\Action1\script.mts"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
msgbox f.ReadLine
Loop
f.Close

Related

Open a file with notepad and save it, using vbscript

My problem is that I want to open a file in notepad and then save it from notepad, not manually but using vbscript (the reason I want this is to overcome some encoding issues). Until now I have found a relevant article which should solve my problem, but it doesn't work. It has this code:
Dim notepad, wndNotepad, strDesktop
Set notepad = Sys.Process("notepad")
Set wndNotepad = notepad.Window("Notepad")
' Open a file in Notepad
wndNotepad.MainMenu.Click("File|Open...")
notepad.Window("#32770", "Open").OpenFile "C:\Program Files\SmartBear\TestComplete 12\install.txt"
' Save the file to the desktop
strDesktop = WshShell.SpecialFolders("Desktop")
wndNotepad.MainMenu.Click "File|Save as..."
notepad.Window("#32770", "Save As").SaveFile strDesktop & "\install.txt"
The problem is that vbscript can't recognize the Sys in the second line (Sys.Process). I assume that in this line a process for notepad should be created (like here) and then something like an object of this process to be returned to the variable notepad (something I don't know if and how can be achived) in order to be used in the third line (notepad.Window("Notepad")).
If anyone has any idea how the code should be in order to work, I would really appreciate the help. Also any other suggestions on how to solve this problem (or any ideas about if it can actually be solved) are very welcome. Thank you for your time.
EDIT
As I said in the comments below, the above code needs the TestComplete software which is expensive. So if anyone has any idea for resolving my issue in another way (other software, other code, other programming language) I would be glad to learn it.
I think you need to copy a file from one path to another. Please refer the below code for copying Files,
Function CopyFile()
strSourceFile = "c:\.....\source.txt"
strDestFile = "c:\.....\dest.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file is read-only
If Not fso.GetFile(DestinationFile).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile strSourceFile, strDestFile, strOverWrite
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
'Replace the file
fso.CopyFile strSourceFile, strDestFile, strOverWrite
'Reapply the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
End If
End Function

UFT File system test

Im a rookie on HP UFT testing and work on a data migration project I would like to automate.
Every day, we get a set of folder and files syncronize from a vendor with a following syncronization report(.csv file).
I would really like to test if the actual .csv file containing a list of files updated in the filesystem exists.
I get the .csv file on a network share, I open it and see a list of files with
data paths, which should be used to (loop)search though the filesystem and check if the files is actually on the location. How do I do that with UFT??
sample script to get all csv content and looping through content and verifying whether files exists or not.
filename = "C:\path\list.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
filepath=f.ReadLine 'assuming every line as file full path
if FSO.fileexists(filepath) then
print filepath & " file is avaialble"
' do your checks here
else
print filepath & " file is not avaialble"
End if
Loop
f.Close

File Path When Including Nested Files in a Script

I have a script file which opens a text file located in the same directory. Let's call it SubScript.
SubScript.vbs
Function DoSomething(foo)
...
Dim Key
With CreateObject("Scripting.FileSystemObject")
Key = .OpenTextFile("key.txt", 1).ReadAll
End With
...
End Function
No problem here when the script is run on its own. However, I want to use the script above in another script file, "MainScript". The SubScript is located in a subfolder in the MainScript directory.
MainScript.vbs
With CreateObject("Scripting.FileSystemObject")
ExecuteGlobal .OpenTextFile(".\SubDir\SubScript.vbs", 1).ReadAll
End With
When I try and use the DoSomething function in the SubScript, I get a file not found error. I see what is happening, the subscript is trying to find the text file in the MainScript directory, where it doesn't exist.
Is there a way, without using an absolute file path, to make sure the SubScript loads the text file from the SubDir?
A relative path is resolved wrt the current directory of the process. Sometimes you can use the script's folder to get more flexibility. But in your case (.ExecuteGlobal), the SubScript's current directory is the current directory of the MainScript.
You should pass a path to DoSomething(), unless you can live with hardcoding ".\SubDir\key.txt".

How to parse one text file to multiple files based on data contained within

I have an enormous text file that I'd like to parse into other files - I think the only thing I have on this box (company computer) to use is VBS, so here's my question:
I have text file with a bunch of network configurations that looks like this:
"Active","HostName","IPAddress"
!
.......
end
This repeats itself throughout the file, but obviously for each configuration different data will occur within the "..." It always says "Active" for each configuration as well.
I want to create save files of the type HostName.cfg for each configuration and save all of the text between and including the ! and "end" . The line with the three quoted attributes doesn't need to be copied.
I'm still learning VBS so I'd appreciate any help in the matter. Thanks!
Here are some useful file reading functions and statements:
Freefile
Returns an integer which you should assign to a variable and then use in an Open statement.
Open <string> For Input As <integer>
Opens the specified file using the specified file handle. Don't use the same file handle twice without closing it in between.
Line Input #<integer>, <variable>
Reads one line of the file into a string.
Input #<integer>, <variable>, <variable>, <variable>
Reads one line of the file delimited by commas into several variables.
I once had to read a text file while omitting starting and ending lines. Though I didn't need to write it anywhere, FSO is simple enough that you will be able to figure it out. Here's some code for reading a file and link for writing to file via FSO: link
'Create a FSO object and open the file by providing the file path
Dim fso, f, filePath, line
filePath = "test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)
'Skip through the first two lines
f.readline
f.readline
'Read till the end of file, if the line is not "end", split it based on ","
while not f.AtEndOfStream
line = f.readline()
if line <> "end" then
arr = split(line, ",")
'Write the arr to file
end if
wend
f.Close

VB Script Text File Prepend

Anyone know how to quickly prepend (add two new lines of text) to the start of an existing text file using either VB Script or a Bat file? Most elegant solution gets the tick.
How about this:
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("test.txt", 1)
ReadAllTextFile = f.ReadAll
Set f = fso.OpenTextFile("test.txt", 2, True)
f.WriteLine("Blaaa")
f.WriteLine("Blaaaa some more...")
f.Write(ReadAllTextFile)
Source: Tek Tips
Check José Basilios answer for code and reference to the FSO. You will be using that.
BUT: I wouldn't go the ReadAllTextFile = f.ReadAll route, since that could be a few Gigabytes (who knows?).
INSTEAD:
open a new file
write prepended lines
read line by line from old file, writing into new file
(close both files)
delete old file
rename new file -> old file

Resources