[Fuego] [PATCH 15/16] Improve Click and Check methods to allow multiple locators

Guilherme Campos Camargo guicc at profusion.mobi
Thu Mar 29 00:08:31 UTC 2018


Signed-off-by: Guilherme Campos Camargo <guicc at profusion.mobi>
---
 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



More information about the Fuego mailing list