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?
Asked
Active
Viewed 2,380 times
0
-
you can use a mock. there are probably plenty of examples on the internet. – Erki M. Apr 11 '14 at 11:26
2 Answers
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?
-
-
@ChandrayyaGK: I was updating my answer. Hope my edit makes it worth the answer – Ajinkya Apr 11 '14 at 10:41
-
@Deanna: I am not asking clarification in my answer and it does answer the question. Question is `Is there any way to do it with Java or Selenium?` IMHO I think I answered it – Ajinkya Apr 11 '14 at 11:19
-
SO didn't show me your Java edit when I reviewed it and commented. My apologies. – Deanna Apr 11 '14 at 11:21
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. :(
- 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.
- 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.
- 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).