i am writing some unit tests with Django and Selenium with PhantomJS.
Things seem to be working, apart from the fact i need to access the browser local storage to validate that my tokens are correct.
My code so far is:
from selenium.webdriver.phantomjs.webdriver import WebDriver
class UserSeleniumTestCase(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(UserSeleniumTestCase, cls).setUpClass()
cls.selenium = WebDriver()
cls.selenium.implicitly_wait(10)
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(UserSeleniumTestCase, cls).tearDownClass()
def setUp(self):
# create the users.
def test_login(self):
self.selenium.get(
'%s%s' % (self.live_server_url, reverse_lazy('account_login')))
self.assertIn(_('Sign In'), self.selenium.title)
login_input = self.selenium.find_element_by_name("login")
login_input.send_keys('[email protected]')
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys('safe!')
self.selenium.find_element_by_id("btn_login").click()
self.selenium.get(
'%s%s' % (self.live_server_url, reverse_lazy('polls:poll-add')))
self.assertIn(_('Create Polls'), self.selenium.title)
print(self.selenium.execute_script('localStorage.getItem("token");'))
My problem is that i am getting None with print(self.selenium.execute_script('localStorage.getItem("token");')) were at this point in the login it should be populated (in the browser works just fine). Am i missing something? i also tried print(self.selenium.execute_script('window.localStorage.getItem("token");'))
Yes, you have to return from the JS script in order to see the value at the Python level:
print(self.selenium.execute_script('return localStorage.getItem("token");'))
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