Cleaned code for future tests
This commit is contained in:
parent
38abecefef
commit
ee1cd692da
253
main.py
253
main.py
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import csv
|
import csv
|
||||||
import urllib.parse as urlparse
|
import urllib.parse as urlparse
|
||||||
@ -10,133 +11,151 @@ from selenium.webdriver.common.keys import Keys
|
|||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
|
||||||
BASE_URL = "https://crm-dev.caat.org.uk/"
|
|
||||||
|
|
||||||
USERNAME = "roxie"
|
class BaseTester:
|
||||||
PASSWORD = ""
|
BASE_URL = "https://crm-dev.caat.org.uk/"
|
||||||
|
USERNAME = "roxie"
|
||||||
|
PASSWORD = ""
|
||||||
|
|
||||||
fireFoxOptions = webdriver.FirefoxOptions()
|
def __init__(self):
|
||||||
fireFoxOptions.headless = True
|
fireFoxOptions = webdriver.FirefoxOptions()
|
||||||
browser = webdriver.Firefox(options=fireFoxOptions)
|
fireFoxOptions.headless = True
|
||||||
|
self.browser = webdriver.Firefox(options=fireFoxOptions)
|
||||||
|
self.wait = WebDriverWait(self.browser, 20)
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
""" Login to civicrm so we can continue with the proper cookies """
|
||||||
|
self.browser.get(self.BASE_URL)
|
||||||
|
username = self.browser.find_element_by_id("edit-name")
|
||||||
|
password = self.browser.find_element_by_id("edit-pass")
|
||||||
|
submit = self.browser.find_element_by_id("edit-submit")
|
||||||
|
username.send_keys(self.USERNAME)
|
||||||
|
password.send_keys(self.PASSWORD)
|
||||||
|
submit.click()
|
||||||
|
|
||||||
def login():
|
# Wait for the js elements load so we know the cookies are good.
|
||||||
""" Login to civicrm so we can continue with the proper cookies """
|
self.wait.until(
|
||||||
browser.get(BASE_URL)
|
EC.visibility_of_element_located(
|
||||||
username = browser.find_element_by_id("edit-name")
|
(
|
||||||
password = browser.find_element_by_id("edit-pass")
|
By.CSS_SELECTOR,
|
||||||
submit = browser.find_element_by_id("edit-submit")
|
"#widget-6 > div:nth-child(1) > div:nth-child(1) > h3:nth-child(4)"
|
||||||
username.send_keys(USERNAME)
|
)
|
||||||
password.send_keys(PASSWORD)
|
|
||||||
submit.click()
|
|
||||||
|
|
||||||
wait = WebDriverWait(browser, 20)
|
|
||||||
# Wait for the js elements load so we know the cookies are good.
|
|
||||||
wait.until(
|
|
||||||
EC.visibility_of_element_located(
|
|
||||||
(
|
|
||||||
By.CSS_SELECTOR,
|
|
||||||
"#widget-6 > div:nth-child(1) > div:nth-child(1) > h3:nth-child(4)"
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
def test_contactexport(search_term: str):
|
self.browser.get(self.BASE_URL + "/user/logout")
|
||||||
search_url = "https://crm-dev.caat.org.uk/civicrm/contact/search"
|
# Wait for the next page to load to finish logging out
|
||||||
browser.get(search_url)
|
self.wait.until(
|
||||||
search_box = browser.find_element_by_id("sort_name")
|
EC.visibility_of_element_located((By.ID, "tabs-wrapper"))
|
||||||
search_box.send_keys(search_term)
|
|
||||||
search_box.send_keys(Keys.ENTER)
|
|
||||||
wait = WebDriverWait(browser, 60)
|
|
||||||
wait.until(
|
|
||||||
EC.visibility_of_element_located(
|
|
||||||
(
|
|
||||||
By.ID,
|
|
||||||
"alpha-filter" #wait for table to load
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
results_text = browser.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])
|
|
||||||
|
|
||||||
browser.find_element_by_id("CIVICRM_QFID_ts_all_4").click()
|
|
||||||
browser.find_element_by_id("select2-chosen-4").click()
|
|
||||||
browser.find_element_by_id("select2-result-label-15").click()
|
|
||||||
wait.until(
|
|
||||||
EC.visibility_of_element_located((By.CSS_SELECTOR, ".crm-block"))
|
|
||||||
)
|
|
||||||
browser.save_screenshot("screenshot.png")
|
|
||||||
export_page_url = browser.current_url
|
|
||||||
parsed = urlparse.urlparse(export_page_url)
|
|
||||||
qf_key = parse_qs(parsed.query)['qfKey']
|
|
||||||
|
|
||||||
session_cookie = {}
|
|
||||||
for cookie in browser.get_cookies():
|
|
||||||
if re.findall(r"^SSESS.*", cookie.get("name")):
|
|
||||||
session_cookie = cookie
|
|
||||||
if not session_cookie:
|
|
||||||
print("NO SESSION COOKIE FOUND. Are you logged in?")
|
|
||||||
return
|
|
||||||
|
|
||||||
session = requests.Session()
|
|
||||||
session.cookies.update({session_cookie["name"]: session_cookie["value"]})
|
|
||||||
data = {
|
|
||||||
"qfKey": qf_key,
|
|
||||||
"entryURL": "https://crm-dev.caat.org.uk/civicrm/contact/search",
|
|
||||||
"_qf_Select_next": "Continue",
|
|
||||||
"exportOption": 1,
|
|
||||||
"mergeOption": 0,
|
|
||||||
"postal_greeting": 1,
|
|
||||||
"addressee": 1,
|
|
||||||
}
|
|
||||||
req = session.request(
|
|
||||||
"POST",
|
|
||||||
"https://crm-dev.caat.org.uk/civicrm/contact/search",
|
|
||||||
data=data
|
|
||||||
)
|
|
||||||
with open("tmp.csv", "w") as csv_file:
|
|
||||||
csv_file.write(req.text)
|
|
||||||
with open("tmp.csv", "r") as csv_file:
|
|
||||||
# Dict reader should remove headers
|
|
||||||
exported_csv = csv.DictReader(csv_file)
|
|
||||||
exported_number_exports = sum(1 for row in exported_csv)
|
|
||||||
|
|
||||||
if exported_number_exports == (result_no):
|
|
||||||
print(
|
|
||||||
"TEST PASSED: Number of expected contact exports for '{}' matches actual number of exports - Expected: {}, Actual: {}"
|
|
||||||
.format(search_term, result_no, exported_number_exports)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"TEST FAILED: Number of expected contact exports for '{}' WAS NOT EQUAL to actual number of exports - Expected: {}, Actual: {}"
|
|
||||||
.format(search_term, result_no, exported_number_exports)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# search > contact > add something in the name thing > select all button > change magic dropdown to export > continue and it will do the download
|
def find_element_by_id(self, *args, **kwargs):
|
||||||
|
return self.browser.find_element_by_id(*args, **kwargs)
|
||||||
|
|
||||||
|
def find_element_by_css_selector(self, *args, **kwargs):
|
||||||
|
return self.browser.find_element_by_css_selector(*args, **kwargs)
|
||||||
|
|
||||||
|
def wait_until_visible(self, locator):
|
||||||
|
return self.wait.until(EC.visibility_of_element_located(locator))
|
||||||
|
|
||||||
|
|
||||||
def logout():
|
class ContactExport(BaseTester):
|
||||||
wait = WebDriverWait(browser, 20)
|
def __init__(self):
|
||||||
browser.get(BASE_URL + "/user/logout")
|
super().__init__()
|
||||||
# Wait for the next page to load to finish logging out
|
self.search_url = "https://crm-dev.caat.org.uk/civicrm/contact/search"
|
||||||
wait.until(EC.visibility_of_element_located((By.ID, "tabs-wrapper")))
|
|
||||||
|
self.contact_selectall_id = "CIVICRM_QFID_ts_all_4"
|
||||||
|
self.contact_dropdown_id = "select2-chosen-4"
|
||||||
|
self.contact_exportoption_id = "select2-result-label-15"
|
||||||
|
|
||||||
|
def download_csv(self):
|
||||||
|
session_cookie = {}
|
||||||
|
for cookie in self.browser.get_cookies():
|
||||||
|
if re.findall(r"^SSESS.*", cookie.get("name")):
|
||||||
|
session_cookie = cookie
|
||||||
|
if not session_cookie:
|
||||||
|
print("NO SESSION COOKIE FOUND. Are you logged in?")
|
||||||
|
raise RuntimeError("No session cookie found.")
|
||||||
|
|
||||||
|
session = requests.Session()
|
||||||
|
session.cookies.update(
|
||||||
|
{session_cookie["name"]: session_cookie["value"]}
|
||||||
|
)
|
||||||
|
qf_key = self.get_export_id()
|
||||||
|
data = {
|
||||||
|
"qfKey": qf_key,
|
||||||
|
"entryURL": "https://crm-dev.caat.org.uk/civicrm/contact/search",
|
||||||
|
"_qf_Select_next": "Continue",
|
||||||
|
"exportOption": 1,
|
||||||
|
"mergeOption": 0,
|
||||||
|
"postal_greeting": 1,
|
||||||
|
"addressee": 1,
|
||||||
|
}
|
||||||
|
req = session.request(
|
||||||
|
"POST",
|
||||||
|
"https://crm-dev.caat.org.uk/civicrm/contact/search",
|
||||||
|
data=data
|
||||||
|
)
|
||||||
|
return req
|
||||||
|
|
||||||
|
def calculate_exported_contacts_number(self):
|
||||||
|
req = self.download_csv()
|
||||||
|
file_name = "/tmp/exportedRecords.csv"
|
||||||
|
with open(file_name, "w") as csv_file:
|
||||||
|
csv_file.write(req.text)
|
||||||
|
with open(file_name, "r") as csv_file:
|
||||||
|
# Dict reader should remove headers
|
||||||
|
exported_csv = csv.DictReader(csv_file)
|
||||||
|
exported_number_exports = sum(1 for row in exported_csv)
|
||||||
|
os.remove(file_name)
|
||||||
|
return exported_number_exports
|
||||||
|
|
||||||
|
def get_export_id(self) -> str:
|
||||||
|
export_page_url = self.browser.current_url
|
||||||
|
parsed = urlparse.urlparse(export_page_url)
|
||||||
|
return parse_qs(parsed.query)['qfKey']
|
||||||
|
|
||||||
|
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.wait_until_visible(
|
||||||
|
(By.ID, "alpha-filter")
|
||||||
|
) #wait for table to load
|
||||||
|
|
||||||
|
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.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"))
|
||||||
|
|
||||||
|
req = self.download_csv()
|
||||||
|
exported_number_exports = self.calculate_exported_contacts_number()
|
||||||
|
if exported_number_exports == (result_no):
|
||||||
|
print(
|
||||||
|
"TEST PASSED: Number of expected contact exports for '{}' matches actual number of exports - Expected: {}, Actual: {}"
|
||||||
|
.format(search_term, result_no, exported_number_exports)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
"TEST FAILED: Number of expected contact exports for '{}' WAS NOT EQUAL to actual number of exports - Expected: {}, Actual: {}"
|
||||||
|
.format(search_term, result_no, exported_number_exports)
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
self.logout()
|
||||||
|
self.browser.close()
|
||||||
|
|
||||||
|
|
||||||
try:
|
if __name__ == "__main__":
|
||||||
login()
|
ContactExport().test("John")
|
||||||
test_contactexport("john")
|
|
||||||
test_contactexport("j")
|
|
||||||
test_contactexport("e")
|
|
||||||
#wait_for_download()
|
|
||||||
logout()
|
|
||||||
finally:
|
|
||||||
try:
|
|
||||||
browser.close()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
Loading…
Reference in New Issue
Block a user