Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profusion-mobi.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GOugpiqmTj+P07Jm+Ox0ik7eCAIbbt/PkFp8aZhrXQ8=; b=VDVlV9D6evIACINFGsybvQL8jsUnEOAXGf/engC8XEaLOZZPANeFs03FUko/SV2NKc Rc+NNVbEtvJ5Ja9A/3UrkCPaz5IW6u5I6Ykhl2jEKTDdGgcfbGQriqDUI9DVmIFsSaoz nyt3qrgdKgH01o1BNxWg5DULUkM2P10w7WJDwtFRuhs8lZ6J6PdrF/Uyfkyv/awZHeh/ IN/KUCjriEv+56P9kLQoAZn9opPK3SyoZU80f6ahePeAjA6uRmjO2QskMylKOZ/H4w9+ hMgU6eXzkpaXdHEuBdwJLYNDIIykJuBM7IkI384FuS3xCuI5hxdM4N/fJ2p9jRWIoNgP 8R/A== From: Guilherme Campos Camargo Date: Wed, 28 Mar 2018 21:08:31 -0300 Message-Id: <20180329000832.12268-16-guicc@profusion.mobi> In-Reply-To: <20180329000832.12268-1-guicc@profusion.mobi> References: <20180329000832.12268-1-guicc@profusion.mobi> Subject: [Fuego] [PATCH 15/16] Improve Click and Check methods to allow multiple locators List-Id: Mailing list for the Fuego test framework List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: fuego@lists.linuxfoundation.org Signed-off-by: Guilherme Campos Camargo --- engine/tests/Functional.fuegotest/test_run.py | 105 ++++++++++++++++---------- 1 file changed, 65 insertions(+), 40 deletions(-) diff --git a/engine/tests/Functional.fuegotest/test_run.py b/engine/tests/Functional.fuegotest/test_run.py index 0370b6f..d433548 100755 --- a/engine/tests/Functional.fuegotest/test_run.py +++ b/engine/tests/Functional.fuegotest/test_run.py @@ -13,6 +13,7 @@ import docker import pexpect import requests from selenium import webdriver +from selenium.webdriver.common.by import By import selenium.common.exceptions as selenium_exceptions LOGGER = logging.getLogger('test_run') @@ -44,6 +45,46 @@ class SeleniumCommand: LOGGER.debug('Executing Selenium Command \'%s\'', self.__class__.__name__) + @staticmethod + def check_element_text(element, text): + try: + if text in element.text: + LOGGER.\ + debug(' Text \'%s\' matches element.text \'%s\'', + text, element.text) + return True + else: + LOGGER.\ + debug(' Text \'%s\' does not match element.text \'%s\'', + text, element.text) + return False + except (selenium_exceptions.ElementNotVisibleException, + selenium_exceptions.NoSuchAttributeException,): + LOGGER.error(' Element has no visible Text') + return False + + @staticmethod + def click_element(element): + try: + element.click() + LOGGER.debug(' Element clicked') + return True + except (selenium_exceptions.ElementClickInterceptedException, + selenium_exceptions.ElementNotVisibleException,): + LOGGER.error(' Element is not clickable') + return False + + @staticmethod + def find_element(context, locator, pattern): + try: + element = context.driver.find_element(locator, pattern) + except selenium_exceptions.NoSuchElementException: + LOGGER.error(' Element not found') + return None + + LOGGER.debug(' Element found') + return element + class Visit(SeleniumCommand): def __init__(self, url, timeout=10, expected_result=200): @@ -69,55 +110,37 @@ class Visit(SeleniumCommand): class CheckText(SeleniumCommand): - def __init__(self, _id, text, expected_result=True): - self._id = _id + def __init__(self, locator, pattern, text='', expected_result=True): + self.pattern = pattern + self.locator = locator self.text = text self.expected_result = expected_result def exec(self, selenium_ctx): super().exec(selenium_ctx) - try: - text = self.driver.find_element_by_id(self._id).text - except Exception: # TODO: Use proper Exception - return False - - LOGGER.debug( - ' Searching for \'%s\' in \'id:%s\'', self.text, self._id) - - result = True - if self.text not in text: - LOGGER.error( - ' \'%s\' not found in id \'%s\' with text \'%s\'', self.text, - self._id, text) - result = False - - LOGGER.debug(' \'%s\' was found', self.text) + element = SeleniumCommand.\ + find_element(selenium_ctx, self.locator, self.pattern) + if element: + result = SeleniumCommand.check_element_text(element, self.text) return result == self.expected_result -class ClickLink(SeleniumCommand): - def __init__(self, linktext, expected_result=True): - self.linktext = linktext - self.expected_result = expected_result +class Click(SeleniumCommand): + def __init__(self, locator, pattern): + self.pattern = pattern + self.locator = locator def exec(self, selenium_ctx): super().exec(selenium_ctx) - LOGGER.debug( - ' Searching for a link with the text \'%s\'', self.linktext) + element = SeleniumCommand.\ + find_element(selenium_ctx, self.locator, self.pattern) + if element: + return SeleniumCommand.click_element(element) - try: - link = self.driver.find_element_by_partial_link_text(self.linktext) - link.click() - LOGGER.debug(' Link found and clicked') - result = True - except selenium_exceptions.NoSuchElementException: - LOGGER.error(' Link not found') - result = False - - return result == self.expected_result + return False class Back(SeleniumCommand): @@ -529,25 +552,27 @@ def main(): # Add Nodes ShExpect('ftc add-nodes docker'), ShExpect('ftc list-nodes -q', r'.*docker.*'), - CheckText(_id='executors', text='master'), - CheckText(_id='executors', text='docker'), + CheckText(By.ID, 'executors', text='master'), + CheckText(By.ID, 'executors', text='docker'), # Add Fuego TestPlan ShExpect('ftc add-jobs -b docker -p testplan_fuego_tests'), ShExpect('ftc list-jobs', r'.*docker\.testplan_fuego_tests\.batch.*'), - ClickLink(linktext='docker'), - CheckText(_id='projectstatus', + Click(By.PARTIAL_LINK_TEXT, 'docker'), + CheckText(By.ID, 'projectstatus', text='docker.testplan_fuego_tests.batch'), Back(), # Install Views ShExpect('ftc add-view batch .*.batch'), - CheckText(_id='projectstatus-tabBar', text='batch'), + CheckText(By.ID, 'projectstatus-tabBar', + text='batch'), # Start Tests ShExpect('ftc build-jobs *.*.Functional.fuego_board_check'), - CheckText(_id='buildQueue', text='Functional.fuego_board_check'), + CheckText(By.ID, 'buildQueue', + text='Functional.fuego_board_check'), ] if not execute_tests(args.timeout): -- 2.16.2