Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Selenium web driver have access to javascript global variables?

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/'))
like image 869
dave Avatar asked May 04 '12 19:05

dave


People also ask

Can selenium interact with JavaScript?

You an use selenium to do automated testing of web apps or websites, or just automate the web browser. It can automate both the desktop browser and the mobile browser. Selenium webdriver can execute Javascript. After loading a page, you can execute any javascript you want.

Why should you avoid using global variables in JavaScript?

Avoid global variables or minimize the usage of global variables in JavaScript. This is because global variables are easily overwritten by other scripts. Global Variables are not bad and not even a security concern, but it shouldn't overwrite values of another variable.

Can JavaScript have global variables?

The scope of JavaScript variables are either global or local. Global variables are declared OUTSIDE the function and its value is accessible/changeable throughout the program. Take care with the global variables because they are risky. Most of the time you should use closures to declare your variables.


1 Answers

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

browser.execute_script("return globalVar;")
like image 166
Scott Avatar answered Oct 06 '22 00:10

Scott