在Protractor测试中设置浏览器时区 [英] Set browser timezone in a Protractor test

查看:82
本文介绍了在Protractor测试中设置浏览器时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用量角器进行e2e测试的项目。

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.

这可能吗?

推荐答案

迟到的答案,但未来可能会有人使用此功能。 ..它有点难看,但我找不到更漂亮的东西。它可能不适合你,这取决于你的应用程序如何检测时区。如果是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.


  1. 将TimeShift.js添加到助手中: https://github.com/cvakiitho/TimeShift-js/blob/master/timeshift.js

  2. 在测试中加载TimeShift: var TimeShift = require('./ helpers / timeshift.js');

  3. 执行TimeShift,并改变时区:

  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.

这篇关于在Protractor测试中设置浏览器时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆