13

Hi: I'm writing tests for django with javascript and I was wondering if the Selenium webdriver can access a javascript global variable. mypage has a script that has a global variable I'd like to access. Is it possible? Thanks!

from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver

class TestEditorSelenium(LiveServerTestCase):
    def setUp(self):
        self.driver = WebDriver()

    def test_mytest(self):
        self.driver.get('%s%s' % (self.live_server_url, '/mypage/'))
dave
  • 683
  • 1
  • 8
  • 17

1 Answers1

26

Yes, you should be able to that with code similar to the below:

browser.execute_script("return globalVar;")
Scott
  • 9,458
  • 7
  • 54
  • 81
  • 3
    `print self.driver.execute_script("return globalVar;")` worked...thanks! – dave May 05 '12 at 10:30
  • Interesting.. I can get an expected value `execute_script("return window.name;")` or `execute_script("return window.location;")` but not for `execute_script("return window;")` The last one returns an empty array for me.... – Diana Suvorova Jun 29 '18 at 21:50