問題描述
我正在做一個使用量角器進行 e2e 測試的項目.
I'm working on a project where the e2e tests are made using protractor.
一些測試,需要驗證日期/時間.在我們的持續(xù)交付平臺上進行測試,確保時區(qū)保持穩(wěn)定.
Some tests, need to validate date/times. Tests are ok on our continuous deliver platforms that ensure the timezone remains stable.
但是,當測試在時區(qū)可能更改的本地計算機上運行時,測試會失敗,因為捕獲的瀏覽器在不同的時區(qū)運行.
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.
我需要以某種方式通過量角器控制時區(qū),以便進行獨立于平臺的測試.
I need to, somehow, control the timezone through protractor in order to have platform independent tests.
這可能嗎?
推薦答案
遲到的答案,但也許將來有人可以用這個...有點丑,但我沒有找到更好的東西.可能不會為您工作,這取決于您的應用程序如何檢測時區(qū).如果它與 getTimezoneOffset() 一起使用,這將起作用.
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.
基本上它重寫了 Date() 類,并在其中添加了 setTimezoneOffset().
Basically it rewrites Date() class, and adds setTimezoneOffset() to it.
- 將 TimeShift.js 添加到您的助手中:https://github.com/cvakiitho/TimeShift-js/blob/master/timeshift.js
- 在測試中加載 TimeShift:
var TimeShift = require('./helpers/timeshift.js');
執(zhí)行 TimeShift,并更改時區(qū):
- 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.
這篇關于在量角器測試中設置瀏覽器時區(qū)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!