test_client_storage.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. """Integration tests for client side storage."""
  2. from __future__ import annotations
  3. import time
  4. from typing import Generator
  5. import pytest
  6. from selenium.webdriver import Firefox
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.remote.webdriver import WebDriver
  9. from reflex.testing import AppHarness
  10. from . import utils
  11. def ClientSide():
  12. """App for testing client-side state."""
  13. import reflex as rx
  14. class ClientSideState(rx.State):
  15. state_var: str = ""
  16. input_value: str = ""
  17. class ClientSideSubState(ClientSideState):
  18. # cookies with default settings
  19. c1: str = rx.Cookie()
  20. c2: rx.Cookie = "c2 default" # type: ignore
  21. # cookies with custom settings
  22. c3: str = rx.Cookie(max_age=2) # expires after 2 second
  23. c4: rx.Cookie = rx.Cookie(same_site="strict")
  24. c5: str = rx.Cookie(path="/foo/") # only accessible on `/foo/`
  25. c6: str = rx.Cookie(name="c6")
  26. c7: str = rx.Cookie("c7 default")
  27. # local storage with default settings
  28. l1: str = rx.LocalStorage()
  29. l2: rx.LocalStorage = "l2 default" # type: ignore
  30. # local storage with custom settings
  31. l3: str = rx.LocalStorage(name="l3")
  32. l4: str = rx.LocalStorage("l4 default")
  33. # Sync'd local storage
  34. l5: str = rx.LocalStorage(sync=True)
  35. l6: str = rx.LocalStorage(sync=True, name="l6")
  36. # Session storage
  37. s1: str = rx.SessionStorage()
  38. s2: rx.SessionStorage = "s2 default" # type: ignore
  39. s3: str = rx.SessionStorage(name="s3")
  40. def set_l6(self, my_param: str):
  41. self.l6 = my_param
  42. def set_var(self):
  43. setattr(self, self.state_var, self.input_value)
  44. self.state_var = self.input_value = ""
  45. class ClientSideSubSubState(ClientSideSubState):
  46. c1s: str = rx.Cookie()
  47. l1s: str = rx.LocalStorage()
  48. s1s: str = rx.SessionStorage()
  49. def set_var(self):
  50. setattr(self, self.state_var, self.input_value)
  51. self.state_var = self.input_value = ""
  52. def index():
  53. return rx.fragment(
  54. rx.chakra.input(
  55. value=ClientSideState.router.session.client_token,
  56. is_read_only=True,
  57. id="token",
  58. ),
  59. rx.chakra.input(
  60. placeholder="state var",
  61. value=ClientSideState.state_var,
  62. on_change=ClientSideState.set_state_var, # type: ignore
  63. id="state_var",
  64. ),
  65. rx.chakra.input(
  66. placeholder="input value",
  67. value=ClientSideState.input_value,
  68. on_change=ClientSideState.set_input_value, # type: ignore
  69. id="input_value",
  70. ),
  71. rx.button(
  72. "Set ClientSideSubState",
  73. on_click=ClientSideSubState.set_var,
  74. id="set_sub_state",
  75. ),
  76. rx.button(
  77. "Set ClientSideSubSubState",
  78. on_click=ClientSideSubSubState.set_var,
  79. id="set_sub_sub_state",
  80. ),
  81. rx.box(ClientSideSubState.c1, id="c1"),
  82. rx.box(ClientSideSubState.c2, id="c2"),
  83. rx.box(ClientSideSubState.c3, id="c3"),
  84. rx.box(ClientSideSubState.c4, id="c4"),
  85. rx.box(ClientSideSubState.c5, id="c5"),
  86. rx.box(ClientSideSubState.c6, id="c6"),
  87. rx.box(ClientSideSubState.c7, id="c7"),
  88. rx.box(ClientSideSubState.l1, id="l1"),
  89. rx.box(ClientSideSubState.l2, id="l2"),
  90. rx.box(ClientSideSubState.l3, id="l3"),
  91. rx.box(ClientSideSubState.l4, id="l4"),
  92. rx.box(ClientSideSubState.l5, id="l5"),
  93. rx.box(ClientSideSubState.l6, id="l6"),
  94. rx.box(ClientSideSubState.s1, id="s1"),
  95. rx.box(ClientSideSubState.s2, id="s2"),
  96. rx.box(ClientSideSubState.s3, id="s3"),
  97. rx.box(ClientSideSubSubState.c1s, id="c1s"),
  98. rx.box(ClientSideSubSubState.l1s, id="l1s"),
  99. rx.box(ClientSideSubSubState.s1s, id="s1s"),
  100. )
  101. app = rx.App(state=rx.State)
  102. app.add_page(index)
  103. app.add_page(index, route="/foo")
  104. @pytest.fixture(scope="module")
  105. def client_side(tmp_path_factory) -> Generator[AppHarness, None, None]:
  106. """Start ClientSide app at tmp_path via AppHarness.
  107. Args:
  108. tmp_path_factory: pytest tmp_path_factory fixture
  109. Yields:
  110. running AppHarness instance
  111. """
  112. with AppHarness.create(
  113. root=tmp_path_factory.mktemp("client_side"),
  114. app_source=ClientSide, # type: ignore
  115. ) as harness:
  116. yield harness
  117. @pytest.fixture
  118. def driver(client_side: AppHarness) -> Generator[WebDriver, None, None]:
  119. """Get an instance of the browser open to the client_side app.
  120. Args:
  121. client_side: harness for ClientSide app
  122. Yields:
  123. WebDriver instance.
  124. """
  125. assert client_side.app_instance is not None, "app is not running"
  126. driver = client_side.frontend()
  127. try:
  128. yield driver
  129. finally:
  130. driver.quit()
  131. @pytest.fixture()
  132. def local_storage(driver: WebDriver) -> Generator[utils.LocalStorage, None, None]:
  133. """Get an instance of the local storage helper.
  134. Args:
  135. driver: WebDriver instance.
  136. Yields:
  137. Local storage helper.
  138. """
  139. ls = utils.LocalStorage(driver)
  140. yield ls
  141. ls.clear()
  142. @pytest.fixture()
  143. def session_storage(driver: WebDriver) -> Generator[utils.SessionStorage, None, None]:
  144. """Get an instance of the session storage helper.
  145. Args:
  146. driver: WebDriver instance.
  147. Yields:
  148. Session storage helper.
  149. """
  150. ss = utils.SessionStorage(driver)
  151. yield ss
  152. ss.clear()
  153. @pytest.fixture(autouse=True)
  154. def delete_all_cookies(driver: WebDriver) -> Generator[None, None, None]:
  155. """Delete all cookies after each test.
  156. Args:
  157. driver: WebDriver instance.
  158. Yields:
  159. None
  160. """
  161. yield
  162. driver.delete_all_cookies()
  163. def cookie_info_map(driver: WebDriver) -> dict[str, dict[str, str]]:
  164. """Get a map of cookie names to cookie info.
  165. Args:
  166. driver: WebDriver instance.
  167. Returns:
  168. A map of cookie names to cookie info.
  169. """
  170. return {cookie_info["name"]: cookie_info for cookie_info in driver.get_cookies()}
  171. @pytest.mark.asyncio
  172. async def test_client_side_state(
  173. client_side: AppHarness,
  174. driver: WebDriver,
  175. local_storage: utils.LocalStorage,
  176. session_storage: utils.SessionStorage,
  177. ):
  178. """Test client side state.
  179. Args:
  180. client_side: harness for ClientSide app.
  181. driver: WebDriver instance.
  182. local_storage: Local storage helper.
  183. session_storage: Session storage helper.
  184. """
  185. assert client_side.app_instance is not None
  186. assert client_side.frontend_url is not None
  187. def poll_for_token():
  188. token_input = driver.find_element(By.ID, "token")
  189. assert token_input
  190. # wait for the backend connection to send the token
  191. token = client_side.poll_for_value(token_input)
  192. assert token is not None
  193. return token
  194. def set_sub(var: str, value: str):
  195. # Get a reference to the cookie manipulation form.
  196. state_var_input = driver.find_element(By.ID, "state_var")
  197. input_value_input = driver.find_element(By.ID, "input_value")
  198. set_sub_state_button = driver.find_element(By.ID, "set_sub_state")
  199. AppHarness._poll_for(lambda: state_var_input.get_attribute("value") == "")
  200. AppHarness._poll_for(lambda: input_value_input.get_attribute("value") == "")
  201. # Set the values.
  202. state_var_input.send_keys(var)
  203. input_value_input.send_keys(value)
  204. set_sub_state_button.click()
  205. def set_sub_sub(var: str, value: str):
  206. # Get a reference to the cookie manipulation form.
  207. state_var_input = driver.find_element(By.ID, "state_var")
  208. input_value_input = driver.find_element(By.ID, "input_value")
  209. set_sub_sub_state_button = driver.find_element(By.ID, "set_sub_sub_state")
  210. AppHarness._poll_for(lambda: state_var_input.get_attribute("value") == "")
  211. AppHarness._poll_for(lambda: input_value_input.get_attribute("value") == "")
  212. # Set the values.
  213. state_var_input.send_keys(var)
  214. input_value_input.send_keys(value)
  215. set_sub_sub_state_button.click()
  216. token = poll_for_token()
  217. # get a reference to all cookie and local storage elements
  218. c1 = driver.find_element(By.ID, "c1")
  219. c2 = driver.find_element(By.ID, "c2")
  220. c3 = driver.find_element(By.ID, "c3")
  221. c4 = driver.find_element(By.ID, "c4")
  222. c5 = driver.find_element(By.ID, "c5")
  223. c6 = driver.find_element(By.ID, "c6")
  224. c7 = driver.find_element(By.ID, "c7")
  225. l1 = driver.find_element(By.ID, "l1")
  226. l2 = driver.find_element(By.ID, "l2")
  227. l3 = driver.find_element(By.ID, "l3")
  228. l4 = driver.find_element(By.ID, "l4")
  229. s1 = driver.find_element(By.ID, "s1")
  230. s2 = driver.find_element(By.ID, "s2")
  231. s3 = driver.find_element(By.ID, "s3")
  232. c1s = driver.find_element(By.ID, "c1s")
  233. l1s = driver.find_element(By.ID, "l1s")
  234. s1s = driver.find_element(By.ID, "s1s")
  235. # assert on defaults where present
  236. assert c1.text == ""
  237. assert c2.text == "c2 default"
  238. assert c3.text == ""
  239. assert c4.text == ""
  240. assert c5.text == ""
  241. assert c6.text == ""
  242. assert c7.text == "c7 default"
  243. assert l1.text == ""
  244. assert l2.text == "l2 default"
  245. assert l3.text == ""
  246. assert l4.text == "l4 default"
  247. assert s1.text == ""
  248. assert s2.text == "s2 default"
  249. assert s3.text == ""
  250. assert c1s.text == ""
  251. assert l1s.text == ""
  252. assert s1s.text == ""
  253. # no cookies should be set yet!
  254. assert not driver.get_cookies()
  255. local_storage_items = local_storage.items()
  256. local_storage_items.pop("chakra-ui-color-mode", None)
  257. assert not local_storage_items
  258. # set some cookies and local storage values
  259. set_sub("c1", "c1 value")
  260. set_sub("c2", "c2 value")
  261. set_sub("c4", "c4 value")
  262. set_sub("c5", "c5 value")
  263. set_sub("c6", "c6 throwaway value")
  264. set_sub("c6", "c6 value")
  265. set_sub("c7", "c7 value")
  266. set_sub("l1", "l1 value")
  267. set_sub("l2", "l2 value")
  268. set_sub("l3", "l3 value")
  269. set_sub("l4", "l4 value")
  270. set_sub("s1", "s1 value")
  271. set_sub("s2", "s2 value")
  272. set_sub("s3", "s3 value")
  273. set_sub_sub("c1s", "c1s value")
  274. set_sub_sub("l1s", "l1s value")
  275. set_sub_sub("s1s", "s1s value")
  276. exp_cookies = {
  277. "state.client_side_state.client_side_sub_state.c1": {
  278. "domain": "localhost",
  279. "httpOnly": False,
  280. "name": "state.client_side_state.client_side_sub_state.c1",
  281. "path": "/",
  282. "sameSite": "Lax",
  283. "secure": False,
  284. "value": "c1%20value",
  285. },
  286. "state.client_side_state.client_side_sub_state.c2": {
  287. "domain": "localhost",
  288. "httpOnly": False,
  289. "name": "state.client_side_state.client_side_sub_state.c2",
  290. "path": "/",
  291. "sameSite": "Lax",
  292. "secure": False,
  293. "value": "c2%20value",
  294. },
  295. "state.client_side_state.client_side_sub_state.c4": {
  296. "domain": "localhost",
  297. "httpOnly": False,
  298. "name": "state.client_side_state.client_side_sub_state.c4",
  299. "path": "/",
  300. "sameSite": "Strict",
  301. "secure": False,
  302. "value": "c4%20value",
  303. },
  304. "c6": {
  305. "domain": "localhost",
  306. "httpOnly": False,
  307. "name": "c6",
  308. "path": "/",
  309. "sameSite": "Lax",
  310. "secure": False,
  311. "value": "c6%20value",
  312. },
  313. "state.client_side_state.client_side_sub_state.c7": {
  314. "domain": "localhost",
  315. "httpOnly": False,
  316. "name": "state.client_side_state.client_side_sub_state.c7",
  317. "path": "/",
  318. "sameSite": "Lax",
  319. "secure": False,
  320. "value": "c7%20value",
  321. },
  322. "state.client_side_state.client_side_sub_state.client_side_sub_sub_state.c1s": {
  323. "domain": "localhost",
  324. "httpOnly": False,
  325. "name": "state.client_side_state.client_side_sub_state.client_side_sub_sub_state.c1s",
  326. "path": "/",
  327. "sameSite": "Lax",
  328. "secure": False,
  329. "value": "c1s%20value",
  330. },
  331. }
  332. AppHarness._poll_for(
  333. lambda: all(cookie_key in cookie_info_map(driver) for cookie_key in exp_cookies)
  334. )
  335. cookies = cookie_info_map(driver)
  336. for exp_cookie_key, exp_cookie_data in exp_cookies.items():
  337. assert cookies.pop(exp_cookie_key) == exp_cookie_data
  338. # assert all cookies have been popped for this page
  339. assert not cookies
  340. # Test cookie with expiry by itself to avoid timing flakiness
  341. set_sub("c3", "c3 value")
  342. AppHarness._poll_for(
  343. lambda: "state.client_side_state.client_side_sub_state.c3"
  344. in cookie_info_map(driver)
  345. )
  346. c3_cookie = cookie_info_map(driver)[
  347. "state.client_side_state.client_side_sub_state.c3"
  348. ]
  349. assert c3_cookie.pop("expiry") is not None
  350. assert c3_cookie == {
  351. "domain": "localhost",
  352. "httpOnly": False,
  353. "name": "state.client_side_state.client_side_sub_state.c3",
  354. "path": "/",
  355. "sameSite": "Lax",
  356. "secure": False,
  357. "value": "c3%20value",
  358. }
  359. time.sleep(2) # wait for c3 to expire
  360. if not isinstance(driver, Firefox):
  361. # Note: Firefox does not remove expired cookies Bug 576347
  362. assert (
  363. "state.client_side_state.client_side_sub_state.c3"
  364. not in cookie_info_map(driver)
  365. )
  366. local_storage_items = local_storage.items()
  367. local_storage_items.pop("chakra-ui-color-mode", None)
  368. assert (
  369. local_storage_items.pop("state.client_side_state.client_side_sub_state.l1")
  370. == "l1 value"
  371. )
  372. assert (
  373. local_storage_items.pop("state.client_side_state.client_side_sub_state.l2")
  374. == "l2 value"
  375. )
  376. assert local_storage_items.pop("l3") == "l3 value"
  377. assert (
  378. local_storage_items.pop("state.client_side_state.client_side_sub_state.l4")
  379. == "l4 value"
  380. )
  381. assert (
  382. local_storage_items.pop(
  383. "state.client_side_state.client_side_sub_state.client_side_sub_sub_state.l1s"
  384. )
  385. == "l1s value"
  386. )
  387. assert not local_storage_items
  388. session_storage_items = session_storage.items()
  389. session_storage_items.pop("token", None)
  390. assert (
  391. session_storage_items.pop("state.client_side_state.client_side_sub_state.s1")
  392. == "s1 value"
  393. )
  394. assert (
  395. session_storage_items.pop("state.client_side_state.client_side_sub_state.s2")
  396. == "s2 value"
  397. )
  398. assert session_storage_items.pop("s3") == "s3 value"
  399. assert (
  400. session_storage_items.pop(
  401. "state.client_side_state.client_side_sub_state.client_side_sub_sub_state.s1s"
  402. )
  403. == "s1s value"
  404. )
  405. assert not session_storage_items
  406. assert c1.text == "c1 value"
  407. assert c2.text == "c2 value"
  408. assert c3.text == "c3 value"
  409. assert c4.text == "c4 value"
  410. assert c5.text == "c5 value"
  411. assert c6.text == "c6 value"
  412. assert c7.text == "c7 value"
  413. assert l1.text == "l1 value"
  414. assert l2.text == "l2 value"
  415. assert l3.text == "l3 value"
  416. assert l4.text == "l4 value"
  417. assert s1.text == "s1 value"
  418. assert s2.text == "s2 value"
  419. assert s3.text == "s3 value"
  420. assert c1s.text == "c1s value"
  421. assert l1s.text == "l1s value"
  422. assert s1s.text == "s1s value"
  423. # navigate to the /foo route
  424. with utils.poll_for_navigation(driver):
  425. driver.get(client_side.frontend_url + "/foo")
  426. # get new references to all cookie and local storage elements
  427. c1 = driver.find_element(By.ID, "c1")
  428. c2 = driver.find_element(By.ID, "c2")
  429. c3 = driver.find_element(By.ID, "c3")
  430. c4 = driver.find_element(By.ID, "c4")
  431. c5 = driver.find_element(By.ID, "c5")
  432. c6 = driver.find_element(By.ID, "c6")
  433. c7 = driver.find_element(By.ID, "c7")
  434. l1 = driver.find_element(By.ID, "l1")
  435. l2 = driver.find_element(By.ID, "l2")
  436. l3 = driver.find_element(By.ID, "l3")
  437. l4 = driver.find_element(By.ID, "l4")
  438. s1 = driver.find_element(By.ID, "s1")
  439. s2 = driver.find_element(By.ID, "s2")
  440. s3 = driver.find_element(By.ID, "s3")
  441. c1s = driver.find_element(By.ID, "c1s")
  442. l1s = driver.find_element(By.ID, "l1s")
  443. s1s = driver.find_element(By.ID, "s1s")
  444. assert c1.text == "c1 value"
  445. assert c2.text == "c2 value"
  446. assert c3.text == "" # cookie expired so value removed from state
  447. assert c4.text == "c4 value"
  448. assert c5.text == "c5 value"
  449. assert c6.text == "c6 value"
  450. assert c7.text == "c7 value"
  451. assert l1.text == "l1 value"
  452. assert l2.text == "l2 value"
  453. assert l3.text == "l3 value"
  454. assert l4.text == "l4 value"
  455. assert s1.text == "s1 value"
  456. assert s2.text == "s2 value"
  457. assert s3.text == "s3 value"
  458. assert c1s.text == "c1s value"
  459. assert l1s.text == "l1s value"
  460. assert s1s.text == "s1s value"
  461. # reset the backend state to force refresh from client storage
  462. async with client_side.modify_state(f"{token}_state.client_side_state") as state:
  463. state.reset()
  464. driver.refresh()
  465. # wait for the backend connection to send the token (again)
  466. token_input = driver.find_element(By.ID, "token")
  467. assert token_input
  468. token = client_side.poll_for_value(token_input)
  469. assert token is not None
  470. # get new references to all cookie and local storage elements (again)
  471. c1 = driver.find_element(By.ID, "c1")
  472. c2 = driver.find_element(By.ID, "c2")
  473. c3 = driver.find_element(By.ID, "c3")
  474. c4 = driver.find_element(By.ID, "c4")
  475. c5 = driver.find_element(By.ID, "c5")
  476. c6 = driver.find_element(By.ID, "c6")
  477. c7 = driver.find_element(By.ID, "c7")
  478. l1 = driver.find_element(By.ID, "l1")
  479. l2 = driver.find_element(By.ID, "l2")
  480. l3 = driver.find_element(By.ID, "l3")
  481. l4 = driver.find_element(By.ID, "l4")
  482. s1 = driver.find_element(By.ID, "s1")
  483. s2 = driver.find_element(By.ID, "s2")
  484. s3 = driver.find_element(By.ID, "s3")
  485. c1s = driver.find_element(By.ID, "c1s")
  486. l1s = driver.find_element(By.ID, "l1s")
  487. s1s = driver.find_element(By.ID, "s1s")
  488. assert c1.text == "c1 value"
  489. assert c2.text == "c2 value"
  490. assert c3.text == "" # temporary cookie expired after reset state!
  491. assert c4.text == "c4 value"
  492. assert c5.text == "c5 value"
  493. assert c6.text == "c6 value"
  494. assert c7.text == "c7 value"
  495. assert l1.text == "l1 value"
  496. assert l2.text == "l2 value"
  497. assert l3.text == "l3 value"
  498. assert l4.text == "l4 value"
  499. assert s1.text == "s1 value"
  500. assert s2.text == "s2 value"
  501. assert s3.text == "s3 value"
  502. assert c1s.text == "c1s value"
  503. assert l1s.text == "l1s value"
  504. assert s1s.text == "s1s value"
  505. # make sure c5 cookie shows up on the `/foo` route
  506. AppHarness._poll_for(
  507. lambda: "state.client_side_state.client_side_sub_state.c5"
  508. in cookie_info_map(driver)
  509. )
  510. assert cookie_info_map(driver)[
  511. "state.client_side_state.client_side_sub_state.c5"
  512. ] == {
  513. "domain": "localhost",
  514. "httpOnly": False,
  515. "name": "state.client_side_state.client_side_sub_state.c5",
  516. "path": "/foo/",
  517. "sameSite": "Lax",
  518. "secure": False,
  519. "value": "c5%20value",
  520. }
  521. # Open a new tab to check that sync'd local storage is working
  522. main_tab = driver.window_handles[0]
  523. driver.switch_to.new_window("window")
  524. driver.get(client_side.frontend_url)
  525. # New tab should have a different state token.
  526. assert poll_for_token() != token
  527. # Set values and check them in the new tab.
  528. set_sub("l5", "l5 value")
  529. set_sub("l6", "l6 value")
  530. l5 = driver.find_element(By.ID, "l5")
  531. l6 = driver.find_element(By.ID, "l6")
  532. assert AppHarness._poll_for(lambda: l6.text == "l6 value")
  533. assert l5.text == "l5 value"
  534. # Set session storage values in the new tab
  535. set_sub("s1", "other tab s1")
  536. s1 = driver.find_element(By.ID, "s1")
  537. s2 = driver.find_element(By.ID, "s2")
  538. s3 = driver.find_element(By.ID, "s3")
  539. assert AppHarness._poll_for(lambda: s1.text == "other tab s1")
  540. assert s2.text == "s2 default"
  541. assert s3.text == ""
  542. # Switch back to main window.
  543. driver.switch_to.window(main_tab)
  544. # The values should have updated automatically.
  545. l5 = driver.find_element(By.ID, "l5")
  546. l6 = driver.find_element(By.ID, "l6")
  547. assert AppHarness._poll_for(lambda: l6.text == "l6 value")
  548. assert l5.text == "l5 value"
  549. s1 = driver.find_element(By.ID, "s1")
  550. s2 = driver.find_element(By.ID, "s2")
  551. s3 = driver.find_element(By.ID, "s3")
  552. assert AppHarness._poll_for(lambda: s1.text == "s1 value")
  553. assert s2.text == "s2 value"
  554. assert s3.text == "s3 value"
  555. # clear the cookie jar and local storage, ensure state reset to default
  556. driver.delete_all_cookies()
  557. local_storage.clear()
  558. # refresh the page to trigger re-hydrate
  559. driver.refresh()
  560. # wait for the backend connection to send the token (again)
  561. token_input = driver.find_element(By.ID, "token")
  562. assert token_input
  563. token = client_side.poll_for_value(token_input)
  564. assert token is not None
  565. # all values should be back to their defaults
  566. c1 = driver.find_element(By.ID, "c1")
  567. c2 = driver.find_element(By.ID, "c2")
  568. c3 = driver.find_element(By.ID, "c3")
  569. c4 = driver.find_element(By.ID, "c4")
  570. c5 = driver.find_element(By.ID, "c5")
  571. c6 = driver.find_element(By.ID, "c6")
  572. c7 = driver.find_element(By.ID, "c7")
  573. l1 = driver.find_element(By.ID, "l1")
  574. l2 = driver.find_element(By.ID, "l2")
  575. l3 = driver.find_element(By.ID, "l3")
  576. l4 = driver.find_element(By.ID, "l4")
  577. c1s = driver.find_element(By.ID, "c1s")
  578. l1s = driver.find_element(By.ID, "l1s")
  579. # assert on defaults where present
  580. assert c1.text == ""
  581. assert c2.text == "c2 default"
  582. assert c3.text == ""
  583. assert c4.text == ""
  584. assert c5.text == ""
  585. assert c6.text == ""
  586. assert c7.text == "c7 default"
  587. assert l1.text == ""
  588. assert l2.text == "l2 default"
  589. assert l3.text == ""
  590. assert l4.text == "l4 default"
  591. assert c1s.text == ""
  592. assert l1s.text == ""