From 53179ec14413a3ecce3b7e1b152c3f746ca04373 Mon Sep 17 00:00:00 2001 From: Ravi Date: Mon, 13 Apr 2020 18:02:51 +0530 Subject: [PATCH] add cookie method added, modified constructor so that it takes arguments while object creation and custom driver object passing to the constructor have been added, fixed mistake in logging for open method while error. Fixed flake and pycode errors in this file --- prodigyqa/browseractions.py | 39 +++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/prodigyqa/browseractions.py b/prodigyqa/browseractions.py index c427e00..59b5d62 100755 --- a/prodigyqa/browseractions.py +++ b/prodigyqa/browseractions.py @@ -47,12 +47,15 @@ class BrowserActions(unittest.TestCase): def __init__(self, *args, **kwargs): """Init Method for webdriver declarations.""" - super(BrowserActions, self).__init__(*args, **kwargs) + super(BrowserActions, self).__init__() self.by_value = None - if platform.system() == 'Linux': - self.driver = webdriver.Chrome(chrome_options=chrome_options) + if "custom_driver" in kwargs: + self.driver = kwargs.get("custom_driver") else: - self.driver = webdriver.Chrome() + if platform.system() == 'Linux': + self.driver = webdriver.Chrome(chrome_options=chrome_options) + else: + self.driver = webdriver.Chrome() def __del__(self): """Destructor method to kill the driver instance. @@ -71,7 +74,7 @@ def page_readiness_wait(self): logger.info(current_state.format(pagestate)) break sleep(0.2) - loop_time_now = datetime.now() - start.total_seconds() + loop_time_now = (datetime.now() - start).seconds if loop_time_now > TIME_OUT and pagestate != 'complete': raise AssertionError( "Opened browser is in state of %s" % pagestate) @@ -100,6 +103,27 @@ def locator_check(self, locator_dict): by = By.TAG_NAME self.by_value = by + def add_cookie(self, cookie_dict): + """ + Adds a cookie to your current session. + + :param cookie_dict : Dict with required keys - "name" and "value" + :type cookie_dict: dictionary + """ + if cookie_dict and isinstance(cookie_dict, dict): + try: + self.driver.add_cookie(cookie_dict) + logger.info(f"Added cookie: {cookie_dict} to current session") + except Exception: + logger.error(f"Failed to add cookie:{cookie_dict} to session:" + f"{self.driver.session_id} and having url:" + f"{self.get_location()}") + raise AssertionError( + f"Not able to add cookie to browser having session id " + f"{self.driver.session_id}") + else: + raise AssertionError("Invalid cookie/Cookie cannot be null") + def open(self, url): """Open the passed 'url'.""" if url is not None: @@ -107,9 +131,8 @@ def open(self, url): self.driver.get(url) logger.info("Browser opened with url '{0}'".format(url)) except Exception: - logger.info("Browser with session id %s failed" - " to navigate to url '%s'." % ( - self.driver.session_id, url)) + logger.error("Browser with session id %s failed to navigate " + "to url '%s'." % (self.driver.session_id, url)) raise AssertionError( 'Opened browser with session id {}'.format( self.driver.session_id))