I like to run Webdriver tests with altered time and time zone. I see two approaches ahead:

1) Using a mocking library like SinonJS or TimeShift.js.

2) Using system libraries like libfaketime/faketime.

While the former might be "cleaner" I also doubt it is usable at all with webdriver, as I do not see a way to actually inject a mocked DateTime object into a webdriver test. (In my understanding just creating a mocked DateTime Object from a JavaScript file would not alter the real -global- one still allowing the browser to access the real JavaScript DateTime routine.

About the latter approach I am not sure either, whether one could limit the system-wide fake time to JavaScript processes only to not mess up things like time stamps in logs or server synchronization. Further I did not found any way to change the time zone in the respective documentation.

Can anybody share their experience with mocking browser time in Selenium tests? Is there any good solution to what I am looking for? Can you in that case sketch the implementation steps?

Links: http://sinonjs.org/ https://github.com/plaa/TimeShift-js

closed as primarily opinion-based by Niels van Reijmersdal, IAmMilinPatel, Bharat Mane, NarendraC, Kate Paulk Jan 3 '17 at 16:58

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


In Selenium features the server and client are running in separate processes. Therefore when mocking time with a tool like Timecop, the browser controlled by Selenium will still see the unmocked system time.

Simple case: Synchronizing the client’s time with the mocked server’s time

= render "layouts/sinon" # the card attached partial Advanced case Synchronizing the client’s time with the mocked server’s time

Include this partial in your layout and the Selenium browser will automatically be synchronized to a time mocked with Timecop:

  • if defined?(Timecop) && Timecop.top_stack_item
    • unix_millis = (Time.now.to_f * 1000.0).to_i
    • unix_millis = ((Time.now + MOCKED_CLIENT_TIME_OFFSET.hours).to_f * 1000.0).to_i :javascript sinon.useFakeTimers(#{unix_millis}); Transposing the client’s time relative to the server’s time

This allows you to test scenarios where the server and client are in different timezones. To do this, use the following steps:

When I am a client in a timezone 8 hours behind the server's timezone # transpose the time When I am a client in the server's timezone # reset the time Step definitions

When /^I am a client in a timezone (\d+) hours behind the server's timezone$/ do |hours_behind| overwrite_constant "MOCKED_CLIENT_TIME_OFFSET", -(hours_behind.to_i) end

When /^I am a client in the server's timezone$/ do overwrite_constant "MOCKED_CLIENT_TIME_OFFSET", 0 end

Not the answer you're looking for? Browse other questions tagged or ask your own question.