From 4f29abc818324f8bc3d2122c04ceb66e81f3405e Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 23 Jun 2022 14:09:03 -0400 Subject: [PATCH] Altered page to retain cookies. --- tests/step_defs/page.py | 10 +++++++--- tests/step_defs/test_session.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/step_defs/page.py b/tests/step_defs/page.py index 6c5aad9..40fb8da 100644 --- a/tests/step_defs/page.py +++ b/tests/step_defs/page.py @@ -1,5 +1,6 @@ """Fisture class for page functions.""" +from http.cookiejar import CookieJar from urllib import request from urllib.error import HTTPError @@ -10,11 +11,14 @@ class Page: def __init__(self): """Init function.""" self.res = None + self.cjar = CookieJar() + self.opener = request.build_opener(request.HTTPCookieProcessor(self.cjar)) def request_url(self, url): """Make a page request.""" try: - page = request.urlopen(url) + # page = request.urlopen(url) + page = self.opener.open(url) except HTTPError as err: self.res = err else: @@ -25,6 +29,6 @@ class Page: """Return the status code.""" return self.res.status - def get_header(self, header): + def get_cookies(self): """Did a session id get set.""" - return self.res.headers[header] + return self.cjar diff --git a/tests/step_defs/test_session.py b/tests/step_defs/test_session.py index cb83de9..a53caf8 100644 --- a/tests/step_defs/test_session.py +++ b/tests/step_defs/test_session.py @@ -22,4 +22,4 @@ def access_home_page(server, page): @then("there is a session id") def confirm_session(page): """Confirm session id exists.""" - assert page.get_header("set-cookie") + assert len(page.get_cookies()), "No cookies received."