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,
exception-escape,
comprehension-escape,
C0103
C0103,
arguments-differ
# 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

120
main.py
View File

@ -18,7 +18,14 @@ from selenium.webdriver.support.select import Select
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):
self.console = Console()
logging.basicConfig(
@ -110,6 +117,19 @@ class BaseTester:
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):
"""Alias for browser.find_element_by_id"""
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)
return qf_key
def test(self, search_term: str):
try:
self.login()
self.browser.get(self.search_url)
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")
def _test(self, search_term: str):
"""
Test Description:
Go to the contact search url
Search for the search term
Select all contacts and set them to export
Get the login cookie and search ID and download the export manually with requests
Save the file in tmp
Read it to check the number of exported contacts is the same as reported in the UI
"""
self.browser.get(self.search_url)
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(
".form-layout-compressed > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > label:nth-child(2)"
).text
matches = re.findall(
r"(\d+)", results_text
) # Should just be one match in normal cases
result_no = int(matches[0])
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)"
).text
matches = re.findall(
r"(\d+)", results_text
) # Should just be one match in normal cases
result_no = int(matches[0])
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_dropdown_id).click()
self.find_element_by_id(self.contact_exportoption_id).click()
self.wait_until_visible((By.CSS_SELECTOR, ".crm-block"))
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_dropdown_id).click()
self.find_element_by_id(self.contact_exportoption_id).click()
self.wait_until_visible((By.CSS_SELECTOR, ".crm-block"))
exported_number_exports = self.calculate_exported_contacts_number()
if exported_number_exports == (result_no):
self.passed(
"Number of expected contact exports for '%s' matches actual number of exports - Expected: %d, Actual: %d"
% (search_term, result_no, exported_number_exports)
)
else:
self.failed(
"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)
)
finally:
self.logout()
self.browser.close()
exported_number_exports = self.calculate_exported_contacts_number()
if exported_number_exports == (result_no):
self.passed(
"Number of expected contact exports for '%s' matches actual number of exports - Expected: %d, Actual: %d"
% (search_term, result_no, exported_number_exports)
)
else:
self.failed(
"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)
)
def test_hardcoded_search_terms(self):
"""Loops over the test with three hardcoded search terms"""
search_terms = ("John", "e", "Smith")
self._test_all(search_terms)
class ActivitiesTab(BaseTester):
@ -256,7 +285,7 @@ class ActivitiesTab(BaseTester):
self.desc("Testing if a contacts activities tab displays all activies")
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.browser.get(self.contact_page.format(cid))
self.wait_until_visible((By.ID, "ui-id-10"))
@ -309,13 +338,7 @@ class ActivitiesTab(BaseTester):
cid_na = "42269" # Nigel Addams
#cid_db = "43193" Use to test 100 max limit
cid_tuple = (cid_da, cid_kh, cid_na)
try:
self.login()
for cid in cid_tuple:
self.test(cid)
finally:
self.logout()
self.browser.close()
self._test_all(cid_tuple)
if __name__ == "__main__":
@ -347,8 +370,7 @@ if __name__ == "__main__":
).test_all_hardcoded_contacts()
ContactExport(
arguments.user, arguments.passwd, arguments.dev, arguments.show_browser
).test("John")
#ContactExport(arguments.user, arguments.passwd, arguments.dev).test("e")
).test_hardcoded_search_terms()
# Mailing list
# Load mailing list test and enter test data