0

I have a situation where I need to change the system time programatically using Java. Is there any way to do it with Java or Selenium?

Mike G
  • 4,232
  • 9
  • 40
  • 66
Abhishek Swain
  • 1,054
  • 8
  • 28

2 Answers2

0

No, Selenium is API to talk with browsers only so you cant achieve this using Selenium.
Even Java doesn't provide any API for this: How can I set the System Time in Java?

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

Why exactly do you need to modify the system time? I can only think it's because you want to validate the display of a date on a page. If this is the case, I would recommend taking 'test_start_time' and 'test_end_time' datestamps, and validating the value you receive from selenium as being within these limits rather than asserting a specific time.

If you need it for another reason then there are options, but they're pretty hacky. :(

  1. If you know that the date is being retrieved in javascript, you can trying redefining the date object to return the date you want instead of the system date/time. You'll need to insert a selenium script doing something like this: Javascript - overriding new Date() behavior to offset GMT. I really wouldn't recommend this, but's it's definitely a lot cleaner than option 2.
  2. If your date is being generated on the back end and you have access to the back end code, you could add a layer of indirection to all relevant date references which by default returns the date as normal. Then, when starting your server for a test run, you could mock out the returned date as required (perhaps using an API to set you requirements). It makes me a bit ill just thinking about it though, and I really wouldn't recommend doing this.
  3. If you think you need to modify system time to fit some imported test data, you should look into generating this data for each test, or even templating existing data using something like http://velocity.apache.org/ to set the date at run time (prior to import).
Community
  • 1
  • 1
Jamey
  • 813
  • 1
  • 12
  • 27