I'm working on a project where the e2e tests are made using protractor.
Some tests, need to validate date/times. Tests are ok on our continuous deliver platforms that ensure the timezone remains stable.
However, when test are run on a local machine, where timezone can change, tests fail because the captured browser is running on a different timezone.
I need to, somehow, control the timezone through protractor in order to have platform independent tests.
Is this possible?
Late answer, but maybe someone in the future can use this...It is a bit ugly, but I haven`t found anything prettier.It might not work for you, it depends on how your app detects timezone. If it is with getTimezoneOffset() this will work.
Basically it rewrites Date() class, and adds setTimezoneOffset() to it.
- Add TimeShift.js into your helpers: https://github.com/cvakiitho/TimeShift-js/blob/master/timeshift.js
- Load TimeShift in your tests:
var TimeShift = require('./helpers/timeshift.js');
Execute TimeShift, and alter timezone:
dvr = browser.driver;
dvr.executeScript(TimeShift).then(function(){
dvr.executeScript('' +
'angular.isDate = function(x){return x instanceof Date};' +
'Date = TimeShift.Date;' +
'TimeShift.setTimezoneOffset(60);' +
});
Note: Every page refresh destroys this.