1

How do I execute a function located in an external JavaScript using webdriver in python?

The JavaScript is used to overwrite JavaScripts internal DateTime object to give an altered browser time for unit testing. I like to use sinonfaketimers.js for this purpose. (But would also go with alternatives like TimeShift.js) Links: http://sinonjs.org/docs/#clock , https://github.com/plaa/TimeShift-js

So I wrote the following python code, which calls the JavaScript itself but not the respective function:

driver = webdriver.Firefox()    
driver.get("http://google.com")
driver.execute_script(open("./sinon_timers.js").read()) 
# some code here to test shift in time    
driver.quit()

As far as I understand this matter correctly all I need to do in order to make this work is to run the respective JavaScript function with the correct arguments. I assume passing the arguments could be done by building a suitable string like:

     driver.execute_script("function('" + argument_var + "');")

The actual function inside the JavaScript sinon_timers.js is:

sinon.timers = {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined),
    clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate: undefined),
    setInterval: setInterval,
    clearInterval: clearInterval,
    Date: Date
};

The full length script can be found at sinonjs.org:

http://sinonjs.org/releases/sinon-timers-1.12.1.js

But I am not sure about the syntax of calling the function sinon.timers in sinon_timers.js. How do I do that?

UPDATE:

Sainath Motlakunta suggested a solution. Unfortunately it doesn't work. But maybe we are closer a step to cracking the puzzle:

driver = webdriver.Firefox()    
driver.get("https://duckduckgo.com")    
driver.execute_script(open("./sinon_timers.js").read())  # <- is this line important?
driver.execute_script("var clock = sinon.useFakeTimers(12345);")    
driver.quit()

This yields: selenium.common.exceptions.WebDriverException: Message: sinon is not defined

Full traceback:

Traceback (most recent call last):
  File "/path/sinonJS_test.py", line 47, in <module>
    sinon_test()
  File "/path/sinonJS_test.py", line 37, in sinon_test
    driver.execute_script("var clock = sinon.useFakeTimers(12345);")
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",     line 401, in execute_script
    {'script': script, 'args':converted_args})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",     line 173, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
   raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: sinon is not defined

Is there maybe some useful information in this post, asking exactly the same question?: https://sqa.stackexchange.com/questions/8838/faking-system-time-date-with-selenium-webdriver

The proposed solution sketched is:

  1. List item Inject/Add the mocking framework during tests runs

  2. Mock the Date object with the JavaScript Executor

  3. Set the timezone

  4. Run tests

4
  • so what is your question? you want a way to check whether time has shifted ? Commented Dec 11, 2014 at 7:28
  • I want to know how to call the function inside the external JavaScript via webdrivers execute_script. Made that more clear in my description. Commented Dec 11, 2014 at 9:57
  • But sinon.timers is not a function !! it is just a js_object Commented Dec 11, 2014 at 10:29
  • OK. Thanks. I am totally naive when it comes to JavaScript (I only code python). Two steps back: Then how do I use the sinon_timers.js to change time? And do so from webdriver in the way described? This is the end result I have in mind. Commented Dec 11, 2014 at 11:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.