Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

javascript - Set browser timezone in a Protractor test

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  1. Add TimeShift.js into your helpers: https://github.com/cvakiitho/TimeShift-js/blob/master/timeshift.js
  2. Load TimeShift in your tests: var TimeShift = require('./helpers/timeshift.js');
  3. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...