Updating another automation test to increase stability

To make a long story short, I have a test that checks to ensure PNG files are created in the local OneNote Offline files cache folder. You can see where this is by looking in File | Options | Save & Backup if you are curious:


The folder will be in the version name of OneNote (14.0 if you are using OneNote 2010) and will be named "OneNoteOfflineCache_Files".

Anyway, my test was looking in this folder to ensure that when an image was added to a page, a PNG file was being created. This was passing 99% of the time, and failing about 1% of the time. I knew the test should have been passing, so I suspected the routine I was using to verify the file was the cause. Here's what I originally had - "f" is a FileInfo handle:

FileList = di.GetFiles(); //and di is the directory for the cached files mentioned above
foreach (FileInfo f in FileList) //...some other stuff, then….
Log.Comment("Found PNG file named: " + f.Name);

if (f.Length > 0)
  Log.Comment("File size is " + f.Length);

  Log.Comment("File size for " + f.Name + "is zero.");


This was failing, though, since about 1% of the time the file size was seen as zero bytes. I went through the documentation above and saw the Refresh() method on the FileInfo class. Since I was just getting the FileInfo object immediately after I expected it to be there, it turns out I was hitting a timing problem. The hard drive was creating a file object, but my test was running before OneNote had a chance to populate the file with any data. Thus, about 1% of the time, my test was running (loosely speaking) faster than the hard drive. So I needed a loop to wait until the file was updated:

DateTime loopTime = System.DateTime.Now.AddSeconds(30.0);
while (f.Length == 0 && System.DateTime.Now < loopTime )
  Log.Comment("File size is " + f.Length + " waiting 250 ms to update");

That last line is key here. Without it, the FileInfo object never gets updated, so the file size it reported would never change even after the file was populated with data.

This is not all that difficult, but I just wanted to point it out since I had to get this working to keep my script 100% stable.

Questions, comments, concerns and criticisms always welcome,

Comments (0)

Skip to main content