Friday, April 25, 2014

Set the time date on Virtualbox Windows VM for Selenium testing

When I initialize my Selenium setup before running the tests, two things happen.  First, the database is restored to a known state.

Secondly, the date and time is set to a known, fixed value.

This happens on the Linux VMs that run the application by calling a bash script over ssh on the servers.  That works fine.

However, I also need the Windows VM that's running as a Selenium node to be set at the same time/date.

The reason for this is this.  About the first thing you see when you log in is a diary.  I need that diary to be showing a particular date so that I know what records should be visible, and my tests verify that those records are there.  If I write the tests one week, and run them the next, without resetting the date the diary will be showing the current week - where the data displayed is different.

There are lots of situations in the application where this is the case.  The alternatives (for example, running an sql update on all date columns) seem more wrong to me - my fixtures (the database) are no longer fixed.  I'm pretty sure that trying that will bite me in other ways (for example, the tests will be filled up with $expected = date('d/m/Y'); instead of $expected = '13/12/2013';).

Anyway, there's also some situations where the client uses the local date to calculate things (in particular, there's some javascript that does 'number of days since x'.  So when I run the tests, the date that javascript uses as the starting point to do that calculation needs to be consistent, else the expected value one day is different on the following day.

Now, Windows has a date-time setting to get the date from the internet.  We can turn that off easily in the date settings on the Windows VM.

However, that's only half the story: even with that off, the VM is getting the date/time from the host bios.  So I change the date manually, and a second or two later it switches back to the current date.

I don't want to fix the date on the host machine.  Other bad things will start happening.  For example, all my Jenkins builds will apparently happen at the same time on the same day - the history will be meaningless.  And other things, I'm sure.

So the solution in this situation is obviously to google and try things you don't fully understand until one of them works.  One of these solutions:

host$ vboxmanage setextradata "win7box" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "1"

Didn't work.  This is possibly because of Guest Additions (which I need for various reasons).  The next one did.  You need to change the parameters passed to the VirtualBox Guest Additions Service (this is on a Windows VM).  The details are at https://forums/virtualbox.org/viewtopic.php?t=24057#p149903 but you need to add --disable-timesync to the VBoxService.exe call.

Next step is then to run a little batch script to set the date/time when the machine starts.  Obviously this is made more complicated by the fact that you can't change the date as a normal user, you need to be Administrator.  So I can't just bung the .bat file into the startup.

Instead, it seems it needs to be a scheduled task.  To add these, click Start, right-click Computer, click 'Manage'.  Click Task Scheduler on the left, Create Basic Task on the right, follow the wizard.

For reasons that I don't understand, I can't start the selenium node in this way - a shortcut to a batch file to do that stays in the Startup folder.

No comments:

Post a Comment