import io import re from pdfminer.high_level import extract_text from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from .base import SearchExportTester class SteeringCommitteePrintLabels(SearchExportTester): """Tests the pdf labels for the SteeringCommitee show all contacts names""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.title("Steering Committee Print Labels") self.desc( "Testing the pdf labels for the SteeringCommittee show all contacts names" ) self.search_url = self.base_url + "/civicrm/contact/search" self.group_dropdown = "s2id_autogen2" self.search_button = "_qf_Basic_refresh" self.mail_label_option = "select2-result-label-19" self.contact_selectall_id = "CIVICRM_QFID_ts_all_4" self.contact_dropdown_id = "select2-chosen-4" # Using this to count amount of people in the exported pdf # This will fail if someone is added that doesn't have a UK adddress self.pdf_search_string = "UNITED KINGDOM" def _test(self): self.browser.get(self.search_url) group_dropdown = self.find_element_by_id(self.group_dropdown) group_dropdown.click() group_dropdown.send_keys("Steering Committee" + Keys.ENTER) self.find_element_by_id(self.search_button).click() self.wait_until_visible( (By.ID, "alpha-filter") ) #wait for table to load self.debug("table of results has loaded") # TODO: Refactor this in base class as a helper function because we do this in another test 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.mail_label_option).click() self.wait_until_visible((By.CSS_SELECTOR, ".crm-block")) # By omitting the field, we are effectively disabling the do not mail filter data = { "_qf_default": "Label:submit", # Smallest labels, will show 24 contacts on one page "label_name": "3475", "location_type_id": "", "_qf_Label_submit": "Make+Mailing+Labels" } res = self.download(data) pdf_text = extract_text(io.BytesIO(res.content)) label_count = pdf_text.count(self.pdf_search_string) if result_no == label_count: self.passed( "no missing mailing labels from pdf export. Expected: %d, Actual %d" % (result_no, label_count) ) else: self.failed( "missing mailing labels from the pdf export. Expected: %d, Actual %d" % (result_no, label_count) ) def test(self): try: self.login() self._test() finally: self.logout() self.browser.close()