From 0f1651686b28b776265b829e8999e4287d8c3b29 Mon Sep 17 00:00:00 2001 From: Murtuza Zabuawala Date: Mon, 8 Oct 2018 11:09:30 +0100 Subject: [PATCH] Code refactoring: - Move the duplicate code for closing the background process dialog into 'test_gui_helper.py', - Simplify the logic by removing the duplicate steps to insert/update binary paths for each server type from 'test_utils.py'. --- .../pg_utilities_backup_restore_test.py | 27 +------- .../pg_utilities_maintenance_test.py | 25 +------ .../python_test_utils/test_gui_helper.py | 38 +++++++++++ .../python_test_utils/test_utils.py | 67 ++++++------------- 4 files changed, 61 insertions(+), 96 deletions(-) create mode 100644 web/regression/python_test_utils/test_gui_helper.py diff --git a/web/pgadmin/feature_tests/pg_utilities_backup_restore_test.py b/web/pgadmin/feature_tests/pg_utilities_backup_restore_test.py index 993c4bf74..19b33e4c8 100644 --- a/web/pgadmin/feature_tests/pg_utilities_backup_restore_test.py +++ b/web/pgadmin/feature_tests/pg_utilities_backup_restore_test.py @@ -13,6 +13,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from regression.feature_utils.base_feature_test import BaseFeatureTest from regression.python_test_utils import test_utils +from regression.python_test_utils import test_gui_helper class PGUtilitiesBackupFeatureTest(BaseFeatureTest): @@ -138,31 +139,7 @@ class PGUtilitiesBackupFeatureTest(BaseFeatureTest): os.remove(backup_file) def after(self): - self._screenshot() - - # In cases where backup div is not closed (sometime due to some error) - try: - if self.driver.find_element_by_css_selector( - ".ajs-message.ajs-bg-bgprocess.ajs-visible > div > " - "div > div > i"): - self.driver.find_element_by_css_selector( - ".ajs-message.ajs-bg-bgprocess.ajs-visible >div >div " - ">div>i").click() - except Exception: - pass - - # In cases where restore div is not closed (sometime due to some error) - try: - if self.driver.find_element_by_xpath( - "//div[contains(text(), 'Process Watcher - " - "Restoring backup')]"): - self.driver.find_element_by_xpath( - "//div[div[div[div[contains(text(), 'Process Watcher " - "- Restoring backup')]]]]" - "/following-sibling::div/div/div").click() - except Exception: - pass - + test_gui_helper.close_bgprocess_popup(self) self.page.remove_server(self.server) connection = test_utils.get_db_connection( self.server['db'], diff --git a/web/pgadmin/feature_tests/pg_utilities_maintenance_test.py b/web/pgadmin/feature_tests/pg_utilities_maintenance_test.py index 2cfef117d..7504eca1e 100644 --- a/web/pgadmin/feature_tests/pg_utilities_maintenance_test.py +++ b/web/pgadmin/feature_tests/pg_utilities_maintenance_test.py @@ -12,6 +12,7 @@ import time from selenium.webdriver.support.ui import WebDriverWait from regression.feature_utils.base_feature_test import BaseFeatureTest from regression.python_test_utils import test_utils +from regression.python_test_utils import test_gui_helper class PGUtilitiesMaintenanceFeatureTest(BaseFeatureTest): @@ -98,29 +99,7 @@ class PGUtilitiesMaintenanceFeatureTest(BaseFeatureTest): "div.wcFloatingFocus div.fa-close").click() def after(self): - # In cases where backup div is not closed - try: - if self.driver.find_element_by_css_selector( - ".ajs-message.ajs-bg-bgprocess.ajs-visible" - " >div >div >div>i"): - self.driver.find_element_by_css_selector( - ".ajs-message.ajs-bg-bgprocess.ajs-visible >div >div " - ">div>i").click() - except Exception: - pass - - # In cases where restore div is not closed (sometime due to some error) - try: - if self.driver.find_element_by_xpath( - "//div[contains(text(), 'Process Watcher - " - "Restoring backup')]"): - self.driver.find_element_by_xpath( - "//div[div[div[div[contains(text(), 'Process Watcher " - "- Restoring backup')]]]]" - "/following-sibling::div/div/div").click() - except Exception: - pass - + test_gui_helper.close_bgprocess_popup(self) self.page.remove_server(self.server) connection = test_utils.get_db_connection( self.server['db'], diff --git a/web/regression/python_test_utils/test_gui_helper.py b/web/regression/python_test_utils/test_gui_helper.py new file mode 100644 index 000000000..7149d34eb --- /dev/null +++ b/web/regression/python_test_utils/test_gui_helper.py @@ -0,0 +1,38 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2018, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + + +def close_bgprocess_popup(tester): + """ + Allow us to close the background process popup window + """ + tester._screenshot() + + # In cases where backup div is not closed (sometime due to some error) + try: + if tester.driver.find_element_by_css_selector( + ".ajs-message.ajs-bg-bgprocess.ajs-visible > div > " + "div > div > i"): + tester.driver.find_element_by_css_selector( + ".ajs-message.ajs-bg-bgprocess.ajs-visible >div >div " + ">div>i").click() + except Exception: + pass + + # In cases where restore div is not closed (sometime due to some error) + try: + if tester.driver.find_element_by_xpath( + "//div[contains(text(), 'Process Watcher - " + "Restoring backup')]"): + tester.driver.find_element_by_xpath( + "//div[div[div[div[contains(text(), 'Process Watcher " + "- Restoring backup')]]]]" + "/following-sibling::div/div/div").click() + except Exception: + pass diff --git a/web/regression/python_test_utils/test_utils.py b/web/regression/python_test_utils/test_utils.py index 91f3b956c..51d54e2de 100644 --- a/web/regression/python_test_utils/test_utils.py +++ b/web/regression/python_test_utils/test_utils.py @@ -615,57 +615,28 @@ def get_db_server(sid): def set_preference(default_binary_path): conn = sqlite3.connect(config.TEST_SQLITE_PATH) cur = conn.cursor() - perf = Preferences.module('paths') - pg_path_pref = perf.preference('pg_bin_dir') + server_types = default_binary_path.keys() - user_pref = cur.execute( - 'SELECT pid, uid FROM user_preferences where pid=%s' % pg_path_pref.pid - ) - user_pref = user_pref.fetchone() + for server in server_types: + path_pref = perf.preference('{0}_bin_dir'.format(server)) + user_pref = cur.execute( + 'SELECT pid, uid FROM user_preferences ' + 'where pid=%s' % path_pref.pid + ) - if user_pref: - cur.execute('UPDATE user_preferences SET value = ? WHERE pid = ?', - (default_binary_path['pg'], pg_path_pref.pid)) - else: - pg_pref_details = (pg_path_pref.pid, 1, - default_binary_path['pg']) - cur.execute('INSERT INTO user_preferences(pid, uid, value)' - ' VALUES (?,?,?)', pg_pref_details) - - ppas_path_pref = perf.preference('ppas_bin_dir') - - user_pref = cur.execute( - 'SELECT pid, uid FROM user_preferences where pid=%s' % - ppas_path_pref.pid - ) - user_pref = user_pref.fetchone() - - if user_pref: - cur.execute('UPDATE user_preferences SET value = ? WHERE pid = ? ', - (default_binary_path['ppas'], ppas_path_pref.pid)) - else: - ppas_pref_details = (ppas_path_pref.pid, 1, - default_binary_path['ppas']) - cur.execute('INSERT INTO user_preferences(pid, uid, value)' - ' VALUES (?,?,?)', ppas_pref_details) - - gpdb_path_pref = perf.preference('gpdb_bin_dir') - - user_pref = cur.execute( - 'SELECT pid, uid FROM user_preferences where pid=%s' % - gpdb_path_pref.pid - ) - user_pref = user_pref.fetchone() - - if user_pref: - cur.execute('UPDATE user_preferences SET value = ? WHERE pid = ? ', - (default_binary_path['gpdb'], gpdb_path_pref.pid)) - else: - gpdb_pref_details = (gpdb_path_pref.pid, 1, - default_binary_path['gpdb']) - cur.execute('INSERT INTO user_preferences(pid, uid, value)' - ' VALUES (?,?,?)', gpdb_pref_details) + user_pref_data = user_pref.fetchone() + if user_pref_data: + cur.execute( + 'UPDATE user_preferences SET value = ? WHERE pid = ?', + (default_binary_path[server], path_pref.pid) + ) + else: + params = (path_pref.pid, 1, default_binary_path[server]) + cur.execute( + 'INSERT INTO user_preferences(pid, uid, value)' + ' VALUES (?,?,?)', params + ) conn.commit()