dScope Series III Scripting and automation
Generating HTML test reports from a script
VBScript is designed with web technology and generating HTML in mind. With a little knowledge of HTML it is possible to make well presented HTML reports and automatically generate an index file so that they are easy to access. The basics are the same as generating a text file, but with a few enhancements to allow better formatting.
When generating multiple HTML pages, it is most likely that you would like them all to have the same formatting. This means that bulk of each page will be the same, with just a bit of text in the middle that changes depending on the test results. An easy and efficient way to achieve this is to make a template page that has the required formatting (using an HTML editor of your choice) and then split it into two files at the place where you want to insert the results text. If you have server side scripting as an option, you can then save the results as separate files and re-combine the three files on the server to generate the output. Alternatively, if you do not have server side scripting, you can combine the files in the dScope script and save the output as complete HTML files which can then be read directly using any web browser. If the bulk of the formatting is defined in a CSS (Cascading Style Sheet) the resulting files can be quite small and look very good. This the approach we will be taking here. The discussion that follows assumes some knowledge of VBS and HTML.
Generating the HTML file
The
first step is to start the File System Object and create a text file
which we will call "HTMLFile" (it could be called anything, this is just the variable name we have chosen):
' Start the FSO |
In the example above, we have already defined two variables "strfilepath" (the path to the files we are working with) and "strfilename" (the name of the file we are generating). After this step, you can write to the newly generated text file by simply using the write method of the "HTMLFile" object like this:
HTMLFile.Write("The text you want to write goes here.<br>") |
This will result in the text as entered being added to the file. It would be possible to write the entire HTML file in this way by manually writing the header and footer with all the HTML tags, but it is easier to split the file into three and combine them as mentioned earlier. We can do this most efficiently by using a little function to retrieve the contents of the files as below. Here we are using the FSO again (it must already be open to run this function). The idea is that we are asking the function to return the entire contents of the file which we define by the string "strFile". The function first checks that the requested file exists. If it does, it will return the entire text of the file. If it doesn't, it will give a message box saying which file it couldn't find, and return an empty string.
'========================================================== |
We can then use this function to combine our three files together. For the purposes of this discussion, we will assume that we have already split our template into two files called "RHeader.txt" and "RFooter.txt" which stand for Results Header and Results Footer.
'Write the header file contents into our new file |
Depending on the content of the header and footer files, these few lines could generate a very neatly presented results page complete with graphics, links etc. The text could be entered in a <div> or <table> section of the page which would allow us to define additional formatting in the header and footer parts of the page and in any style sheet that is linked to from the header.
Making an HTML index file
It can also be helpful when making an HTML file to
make an index page from where you can access the results. This
can be made so that it updates automatically every time a new file is
made. The FSO has methods for appending to a file and this is
useful for adding a new result to a list. However, this is not
that helpful for an HTML file as we don't actually want to write to the
end of the file. An easy way to get round this is to make the
index file in three parts as before. We will make header and
footer template files again (and call them, "IHeader.txt" and
"IFooter.txt" for Index header and Index Footer), but this time we will
also use a text file (called index.txt) for the index part of the
page. This time rather than generating it new each time, we will
be using the "OpenTextFile" method to get the index text file and write
to the end of it. This index text file will basically consist of
a
list of HTML links to which we will add a new link each time a new
results page is generated.
The code to do this looks something like this:
'Update index text file |
Here "strID" is a string that identifies the test that is used as the link - it could just be the text "click here" etc. Also "strDescription" is a string that describes the test in more detail. It could be quite long, containing notes or serial numbers etc so as to distinguish between tests. All the preceding code assumes that the files are all in the same folder. It would be possible to build a more complex folder structure, but for simplicity, that is not what we have done here. And that's it. This gives you the basic technique, but there is really no limit to where you can take this using all the HTML, XHTML, PHP, CSS etc. technologies for generating web content.
Fixing the File Extension
Another little trick that is quite handy is to get the user to set the
file name by using an input box after the test. If you do this,
you don't know whether they are going to enter a file extension or not.
To cope with this scenario, you can look for a full stop (".",
AKA "period") in the string that is entered, strip off all the
characters after that full stop and replace them with the characters
HTML - this makes sure that the file name is always formatted
correctly, irrespective of what file name is entered (as long as it
doesn't actually contain full stops before the file extension, in which
case you will end up with a shortened file name). The code to do
this is:
' make sure we have the right file name extension |
Summary
The generation of HTML reports is a powerful and elegant way of keeping
records. Generating reports in this way is very fast and there is
almost no limit to what is possible. Linking in to an intra-net
or posting results on a web site is very straight forward.
However, it is not always the best way of doing things depending
on the type of test data that is being recorded. Where you want
to extract statistics from the data, saving to a spreadsheet or
database would probably be a better option. Also, with very large
numbers of files the indexes could become quite large. It
wouldn't be too hard to automatically generate a layered file
structure, perhaps based around separate indexes for years, months and
days or some other metric which lends itself to logical operations and
which subdivides the measured results.