2
我想在測試中使用同一個會話的2個瀏覽器窗口。請注意,由於硒限制,每個瀏覽器都有不同的Firefox配置文件。如何防止Django重寫sessionid cookie?
測試(你可以嘗試,只要你有一個測試/測試人員的用戶)是:
import time
from django.test import LiveServerTestCase
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
class ScriptTestCase(LiveServerTestCase):
def do_admin_login(self, username, password, browser):
browser.get('%s%s' % (self.live_server_url, '/admin/'))
username_input = browser.find_element_by_name("username")
username_input.send_keys(username)
password_input = browser.find_element_by_name("password")
password_input.send_keys(password)
browser.find_element_by_xpath('//input[@value="Log in"]').click()
def test_double_window_inactivity(self):
browser0 = WebDriver()
self.do_admin_login('test', 'test', browser0)
cookie = browser0.get_cookie('sessionid')
cookies = {'name': 'sessionid', 'value': cookie['value']}
browser1 = WebDriver()
browser1.add_cookie(cookies)
print 1, browser0.get_cookie('sessionid')['value']
print 2, browser1.get_cookie('sessionid')['value']
browser1.get('%s%s' % (self.live_server_url, '/admin/'))
print 3, browser0.get_cookie('sessionid')['value']
print 4, browser1.get_cookie('sessionid')['value']
輸出是:
1 08ba4efecf00d2b98aacd174ed20e144
2 08ba4efecf00d2b98aacd174ed20e144
3 08ba4efecf00d2b98aacd174ed20e144
4 cb49379190cb37735697c5c82ab300fc
正如你所看到的,後browser1打開/ admin /,其sessionid未被覆蓋。
如何解決此問題?我試圖讓browser1擁有與browser0相同的會話。
固定碼,這要歸功於Hedde ---
def test_double_window_inactivity(self):
self.browser.execute_script('window.open("/admin/", "other")')
for win in self.browser.window_handles:
self.browser.switch_to_window(win)
self.assertWarningHidden()
time.sleep(5+1) # Added one second to compensate for fadeIn
for win in self.browser.window_handles:
self.browser.switch_to_window(win)
self.assertWarningShown()
time.sleep(5+1) # Added one second to compensate for lag
for win in self.browser.window_handles:
self.browser.switch_to_window(win)
self.assertWarningNotInPage()