feat: improved looping over the hardcoded terms

This commit is contained in:
Roxie Gibson 2021-01-22 14:11:50 +00:00
parent dd9be34345
commit 926d086537
Signed by untrusted user: roxxers
GPG Key ID: 5D0140EDEE123F4D
2 changed files with 73 additions and 50 deletions

View File

@ -139,7 +139,8 @@ disable=print-statement,
deprecated-sys-function, deprecated-sys-function,
exception-escape, exception-escape,
comprehension-escape, comprehension-escape,
C0103 C0103,
arguments-differ
# Enable the message, report, category or checker with the given id(s). You can # Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option # either give multiple identifier separated by comma (,) or put this option

120
main.py
View File

@ -18,7 +18,14 @@ from selenium.webdriver.support.select import Select
class BaseTester: class BaseTester:
"""Base class for all test suites""" """
Base class for all test suites
One caveat: This class defines a pseudo function "_test" which is called by "_test_all"
Overloading classes are to define their own tests to be run by selenium after logging in
Then they are to call "_test_all" which will run these tests in a loop with given variables
This allows the test to be run multiple times with different terms to make sure the test covers all bases.
"""
def __init__(self, user: str, passwd: str, dev: bool, show_browser: bool): def __init__(self, user: str, passwd: str, dev: bool, show_browser: bool):
self.console = Console() self.console = Console()
logging.basicConfig( logging.basicConfig(
@ -110,6 +117,19 @@ class BaseTester:
EC.visibility_of_element_located((By.ID, "tabs-wrapper")) EC.visibility_of_element_located((By.ID, "tabs-wrapper"))
) )
def _test(self, *args):
"""Placeholder to be overwritten by overloading classes"""
def _test_all(self, test_strings: tuple[str]):
"""Loops testing over the given terms"""
try:
self.login()
for term in test_strings:
self._test(term)
finally:
self.logout()
self.browser.close()
def find_element_by_id(self, *args, **kwargs): def find_element_by_id(self, *args, **kwargs):
"""Alias for browser.find_element_by_id""" """Alias for browser.find_element_by_id"""
return self.browser.find_element_by_id(*args, **kwargs) return self.browser.find_element_by_id(*args, **kwargs)
@ -206,47 +226,56 @@ class ContactExport(BaseTester):
self.debug("got qf_key '%s' from url" % qf_key) self.debug("got qf_key '%s' from url" % qf_key)
return qf_key return qf_key
def test(self, search_term: str): def _test(self, search_term: str):
try: """
self.login() Test Description:
self.browser.get(self.search_url) Go to the contact search url
search_box = self.find_element_by_id("sort_name") Search for the search term
search_box.send_keys(search_term) Select all contacts and set them to export
search_box.send_keys(Keys.ENTER) Get the login cookie and search ID and download the export manually with requests
self.debug("searching for contacts with term '%s'" % search_term) Save the file in tmp
self.wait_until_visible( Read it to check the number of exported contacts is the same as reported in the UI
(By.ID, "alpha-filter") """
) #wait for table to load self.browser.get(self.search_url)
self.debug("table of results has loaded") search_box = self.find_element_by_id("sort_name")
search_box.send_keys(search_term)
search_box.send_keys(Keys.ENTER)
self.debug("searching for contacts with term '%s'" % search_term)
self.wait_until_visible(
(By.ID, "alpha-filter")
) #wait for table to load
self.debug("table of results has loaded")
results_text = self.find_element_by_css_selector( results_text = self.find_element_by_css_selector(
".form-layout-compressed > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > label:nth-child(2)" ".form-layout-compressed > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > label:nth-child(2)"
).text ).text
matches = re.findall( matches = re.findall(
r"(\d+)", results_text r"(\d+)", results_text
) # Should just be one match in normal cases ) # Should just be one match in normal cases
result_no = int(matches[0]) result_no = int(matches[0])
self.debug("exporting results using the magic dropdown") self.debug("exporting results using the magic dropdown")
self.find_element_by_id(self.contact_selectall_id).click() self.find_element_by_id(self.contact_selectall_id).click()
self.find_element_by_id(self.contact_dropdown_id).click() self.find_element_by_id(self.contact_dropdown_id).click()
self.find_element_by_id(self.contact_exportoption_id).click() self.find_element_by_id(self.contact_exportoption_id).click()
self.wait_until_visible((By.CSS_SELECTOR, ".crm-block")) self.wait_until_visible((By.CSS_SELECTOR, ".crm-block"))
exported_number_exports = self.calculate_exported_contacts_number() exported_number_exports = self.calculate_exported_contacts_number()
if exported_number_exports == (result_no): if exported_number_exports == (result_no):
self.passed( self.passed(
"Number of expected contact exports for '%s' matches actual number of exports - Expected: %d, Actual: %d" "Number of expected contact exports for '%s' matches actual number of exports - Expected: %d, Actual: %d"
% (search_term, result_no, exported_number_exports) % (search_term, result_no, exported_number_exports)
) )
else: else:
self.failed( self.failed(
"Number of expected contact exports for '%s' WAS NOT EQUAL to actual number of exports - Expected: %d, Actual: %d" "Number of expected contact exports for '%s' WAS NOT EQUAL to actual number of exports - Expected: %d, Actual: %d"
% (search_term, result_no, exported_number_exports) % (search_term, result_no, exported_number_exports)
) )
finally:
self.logout() def test_hardcoded_search_terms(self):
self.browser.close() """Loops over the test with three hardcoded search terms"""
search_terms = ("John", "e", "Smith")
self._test_all(search_terms)
class ActivitiesTab(BaseTester): class ActivitiesTab(BaseTester):
@ -256,7 +285,7 @@ class ActivitiesTab(BaseTester):
self.desc("Testing if a contacts activities tab displays all activies") self.desc("Testing if a contacts activities tab displays all activies")
self.contact_page = self.base_url + "/civicrm/contact/view/?reset=1&cid={}" self.contact_page = self.base_url + "/civicrm/contact/view/?reset=1&cid={}"
def test(self, cid: str): def _test(self, cid: str):
self.debug("loading contact page for CID %s" % cid) self.debug("loading contact page for CID %s" % cid)
self.browser.get(self.contact_page.format(cid)) self.browser.get(self.contact_page.format(cid))
self.wait_until_visible((By.ID, "ui-id-10")) self.wait_until_visible((By.ID, "ui-id-10"))
@ -309,13 +338,7 @@ class ActivitiesTab(BaseTester):
cid_na = "42269" # Nigel Addams cid_na = "42269" # Nigel Addams
#cid_db = "43193" Use to test 100 max limit #cid_db = "43193" Use to test 100 max limit
cid_tuple = (cid_da, cid_kh, cid_na) cid_tuple = (cid_da, cid_kh, cid_na)
try: self._test_all(cid_tuple)
self.login()
for cid in cid_tuple:
self.test(cid)
finally:
self.logout()
self.browser.close()
if __name__ == "__main__": if __name__ == "__main__":
@ -347,8 +370,7 @@ if __name__ == "__main__":
).test_all_hardcoded_contacts() ).test_all_hardcoded_contacts()
ContactExport( ContactExport(
arguments.user, arguments.passwd, arguments.dev, arguments.show_browser arguments.user, arguments.passwd, arguments.dev, arguments.show_browser
).test("John") ).test_hardcoded_search_terms()
#ContactExport(arguments.user, arguments.passwd, arguments.dev).test("e")
# Mailing list # Mailing list
# Load mailing list test and enter test data # Load mailing list test and enter test data