diff --git a/prodigyqa/browseractions.py b/prodigyqa/browseractions.py index c427e00..65453b8 100755 --- a/prodigyqa/browseractions.py +++ b/prodigyqa/browseractions.py @@ -159,12 +159,12 @@ def get_attribute(self, locator=None, element=None, """Fetch attribute from locator/element/parent. element with child locator. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). :param attribute_name: attribute name to get it's vale :param element: it is a webelement :param type : value can only be 'locator' or 'element' or 'mixed' - :type locator: dict + :type locator:dict :type type: str """ valid_arguments_of_type = ['locator', 'element', 'mixed'] @@ -213,8 +213,9 @@ def get_attribute(self, locator=None, element=None, def click(self, locator, index=None): """Click an element. - :param locator: dictionary of identifier type - and value ({'by':'id', 'value':'start-of-content.'}). + :param locator:dictionary of identifier type + and value ({'by':'id', 'value':'start-of-content.'}) or + webelement. :param index: Defaults None, number/position of element """ self.page_readiness_wait() @@ -231,6 +232,15 @@ def click(self, locator, index=None): self.__find_element(locator).click() elif isinstance(locator, WebElement): locator.click() + elif isinstance(locator, list): + if index is not None: + if index < len(locator): + locator[index].click() + else: + raise AssertionError( + "Index is greater than no. of elements present") + else: + locator[0].click() else: raise AssertionError( "Dictionary/Weblement are valid Locator types.") @@ -238,7 +248,7 @@ def click(self, locator, index=None): def javascript_click(self, locator, index=None): """Javascript Click on provided element. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}) or a Webelement. :param index: Number/position of element present @@ -260,39 +270,71 @@ def javascript_click(self, locator, index=None): self.by_value, value=locator['locatorvalue'])) elif isinstance(locator, WebElement): self.__execute_script("arguments[0].click();", locator) + elif isinstance(locator, list): + if index is not None: + if index < len(locator): + self.driver.execute_script( + "arguments[0].click();", locator[index]) + else: + raise AssertionError( + "Index is greater than no. of elements present") + else: + self.driver.execute_script( + "arguments[0].click();", locator[0]) else: raise AssertionError( "Locator type should be either dictionary or Weblement.") - def is_element_displayed(self, locator: dict): + def is_element_displayed(self, locator, index=None): """ Check whether an element is diplayed. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ - self.locator_check(locator) self.page_readiness_wait() if isinstance(locator, dict): - return self.driver.find_element( - self.by_value, - value=locator['locatorvalue']).is_displayed() + self.locator_check(locator) + if index is not None: + web_elts = self.find_elements(locator) + if index < len(web_elts): + return web_elts[index].is_displayed() + else: + raise AssertionError( + "Index is greater than the number of elements") + else: + return self.driver.find_element( + self.by_value, + value=locator['locatorvalue']).is_displayed() + elif isinstance(locator, WebElement): + locator.is_displayed() + elif isinstance(locator, list): + if index is not None: + if index < len(locator): + return locator[index].is_displayed() + else: + raise AssertionError( + "Index is greater than no. of elements present") + else: + return locator[0].is_displayed() else: raise AssertionError("Locator type should be dictionary.") - def is_element_enabled(self, locator: dict): + def is_element_enabled(self, locator): """ Check whether an element is enabled. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.locator_check(locator) self.page_readiness_wait() if isinstance(locator, dict): return self.__find_element(locator).is_enabled() + elif isinstance(locator, WebElement): + locator.is_enabled() else: raise AssertionError("Locator type should be dictionary.") @@ -300,23 +342,25 @@ def is_element_selected(self, locator: dict): """ Check whether an element is selecte. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.locator_check(locator) self.page_readiness_wait() if isinstance(locator, dict): return self.__find_element(locator).is_selected() + elif isinstance(locator, WebElement): + locator.is_selected() else: raise AssertionError("Locator type should be dictionary.") def send_keys(self, locator: dict, value=None): """Send text but does not clear the existing text. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.page_readiness_wait() if isinstance(locator, dict): @@ -324,13 +368,15 @@ def send_keys(self, locator: dict, value=None): self.__find_element(locator).send_keys( locator['value'] if value is None else value) + elif isinstance(locator, WebElement): + locator.send_keys(value) else: raise AssertionError("Locator type should be dictionary.") def get_text(self, locator, index=None): """Get text from provided Locator. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). """ self.page_readiness_wait() @@ -345,7 +391,6 @@ def get_text(self, locator, index=None): "Index is greater than the number of elements") else: return self.__find_element(locator).text - elif isinstance(locator, WebElement): return locator.text else: @@ -401,17 +446,19 @@ def get_domain_url(self): url = self.driver.current_url return url.split('//')[0] + '//' + url.split('/')[2] - def clear_text(self, locator: dict): + def clear_text(self, locator): """Clear the text if it's a text entry element. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.locator_check(locator) self.page_readiness_wait() if isinstance(locator, dict): return self.__find_element(locator).clear() + elif isinstance(locator, WebElement): + locator.clear() else: raise AssertionError("Locator type should be dictionary") @@ -510,9 +557,9 @@ def switch_to_alert(self): def hover_on_element(self, locator: dict): """Hover on a particular element. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.locator_check(locator) self.page_readiness_wait() @@ -530,7 +577,7 @@ def hover_on_element(self, locator: dict): def hover_on_click(self, locator): """Hover & click a particular element. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). """ self.locator_check(locator) @@ -546,7 +593,7 @@ def hover_on_click(self, locator): def wait_for_element(self, locator) -> bool: """Wait for an element to exist in UI. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). :rtype: bool """ @@ -582,10 +629,10 @@ def wait_and_reject_alert(self): def select_option_by_index(self, locator: dict, index: int): """Select the option by index. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). :param index: integer value for index. - :type locator: dict + :type locator:dict :type index: int """ self.by_value = locator['by'] @@ -600,34 +647,34 @@ def select_option_by_index(self, locator: dict, index: int): AssertionError( "Invalid locator '{}' or index '{}'".format(locator, index)) - def select_option_by_value(self, locator: dict, value: int): + def select_option_by_value(self, locator: dict, value): """Select the option by using value. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). :param value: string value to select option. - :type locator: dict + :type locator:dict :type value: int """ self.page_readiness_wait() - if isinstance(locator, dict) and isinstance(value, int): + if isinstance(locator, dict): + self.locator_check(locator) try: - Select(self.__find_element(locator)).select_by_value(value) - + Select(self.__find_element(locator) + ).select_by_value(value) except selenium_exceptions.NoSuchElementException: logger.error("Exception : Element '{}' Not Found".format( locator['by'] + '=' + locator['locatorvalue'])) else: - AssertionError( - "Invalid locator '{}' or value '{}'".format(locator, value)) + AssertionError("Invalid locator type") def select_option_by_text(self, locator: dict, text): """Select the value by using text. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). :param text: string value to select option. - :type locator: dict + :type locator:dict """ self.page_readiness_wait() if isinstance(locator, dict): @@ -653,9 +700,9 @@ def scroll_to_footer(self): def find_elements(self, locator: dict): """Return elements matched with locator. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.locator_check(locator) self.page_readiness_wait() @@ -669,9 +716,9 @@ def find_elements(self, locator: dict): def scroll_to_element(self, locator: dict): """Scroll to a particular element on the page. - :param locator: dictionary of identifier type + :param locator:dictionary of identifier type and value ({'by':'id', 'value':'start-of-content.'}). - :type locator: dict + :type locator:dict """ self.page_readiness_wait() if isinstance(locator, dict): @@ -692,7 +739,7 @@ def scroll_to_element(self, locator: dict): def __find_element(self, locator: dict): """Private method simplified finding element. - :type locator: dict + :type locator:dict """ if isinstance(locator, dict): self.locator_check(locator) @@ -715,3 +762,15 @@ def __execute_script(self, script, web_elm=None): return self.driver.execute_script( script, self.__find_element(web_elm)) + + def get_list_size(self, locator: dict): + """ + Get List Size of provided locator. + :param locator: must contain the valid locator. + """ + self.locator_check(locator) + self.page_readiness_wait() + if isinstance(locator, dict): + return len(self.find_elements(locator)) + else: + AssertionError("Invalid locator type") diff --git a/requirements.txt b/requirements.txt index fff0a0e..1b92510 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,9 +12,9 @@ pycodestyle<2.6.0,>=2.5.0 pandas==0.24.2 urllib3==1.25.7 requests>=2.21.0 -ipdb==0.12.2 +ipdb==0.13.2 pytest>=4.0.2 -pytest-html<2.1.0 +pytest-html<2.2.0 xlrd>=0.9.0 scrapy loguru diff --git a/setup.py b/setup.py index 2c9c463..5f1336f 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ setup( name='prodigyqa', - version='1.2.2', + version='1.3.0', description='Test Automation Framework', long_description=long_description, long_description_content_type='text/markdown', @@ -55,9 +55,9 @@ 'pandas==0.24.2', 'urllib3==1.25.7', 'requests>=2.21.0', - 'ipdb==0.12.2', + 'ipdb==0.13.2', 'pytest>=4.0.2', - 'pytest-html<2.1.0', + 'pytest-html<2.2.0', 'xlrd>=0.9.0', 'scrapy', 'nltk',