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/'))
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.
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.
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.
Yes, you should be able to that with code similar to the below:
browser.execute_script("return globalVar;")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With