|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import json |
| 4 | +import time |
| 5 | + |
| 6 | +from seleniumwire import webdriver |
| 7 | +from selenium.webdriver.chrome.service import Service |
| 8 | +from selenium.webdriver.chrome.options import Options |
| 9 | +from selenium.webdriver.common.by import By |
| 10 | +from selenium.webdriver.support.ui import WebDriverWait |
| 11 | +from selenium.webdriver.support import expected_conditions as EC |
| 12 | +from webdriver_manager.chrome import ChromeDriverManager |
| 13 | +import brotli |
| 14 | + |
| 15 | +# Get the Preview Deployment URL |
| 16 | +RULE_EDITOR_URL = os.getenv("RULE_EDITOR_URL") |
| 17 | +if not RULE_EDITOR_URL: |
| 18 | + print("RULE_EDITOR_URL is not set! Test failed.") |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | +print(f"Running tests on: {RULE_EDITOR_URL}") |
| 22 | + |
| 23 | +# Configure Chrome options |
| 24 | +chrome_options = Options() |
| 25 | +chrome_options.add_argument("--ignore-certificate-errors") |
| 26 | +chrome_options.add_argument("--window-size=1920,1080") |
| 27 | +chrome_options.add_argument("--disable-blink-features=AutomationControlled") |
| 28 | +chrome_options.add_argument("--headless=new") # Headless mode |
| 29 | + |
| 30 | +# Initialize driver using selenium-wire |
| 31 | +service = Service(ChromeDriverManager().install()) |
| 32 | +driver = webdriver.Chrome(service=service, options=chrome_options) |
| 33 | +wait = WebDriverWait(driver, 20) |
| 34 | + |
| 35 | +try: |
| 36 | + print("Opening Rule Editor site...") |
| 37 | + driver.get(RULE_EDITOR_URL) |
| 38 | + |
| 39 | + print("Searching for rule CG0006...") |
| 40 | + rule_search_field = wait.until( |
| 41 | + EC.element_to_be_clickable((By.XPATH, '//*[@id="mui-10"]')) |
| 42 | + ) |
| 43 | + rule_search_field.click() |
| 44 | + rule_search_field.send_keys("CG0006") |
| 45 | + |
| 46 | + search_result = wait.until( |
| 47 | + EC.element_to_be_clickable( |
| 48 | + (By.XPATH, '//*[@id="rulesList"]/table/tbody/tr/td[1]') |
| 49 | + ) |
| 50 | + ) |
| 51 | + search_result.click() |
| 52 | + print("Rule selected.") |
| 53 | + |
| 54 | + print("Switching to test tab...") |
| 55 | + test_tab_button = wait.until( |
| 56 | + EC.element_to_be_clickable( |
| 57 | + (By.XPATH, '//*[@id="root"]/div/div[3]/div/div[1]/div/div/div/button[2]') |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + test_tab_button.click() |
| 62 | + time.sleep(4) # wait for the schema validation to complete |
| 63 | + print("Opening upload dataset tab...") |
| 64 | + upload_dataset_tab = wait.until( |
| 65 | + EC.element_to_be_clickable( |
| 66 | + (By.XPATH, '//*[@id="tabpanel-1"]/div[5]/div[1]/div[2]') |
| 67 | + ) |
| 68 | + ) |
| 69 | + upload_dataset_tab.click() |
| 70 | + |
| 71 | + print("Uploading dataset file...") |
| 72 | + file_input = wait.until( |
| 73 | + EC.presence_of_element_located( |
| 74 | + ( |
| 75 | + By.XPATH, |
| 76 | + '//*[@id="tabpanel-1"]/div[5]/div[2]/div/div/div/div/label/input', |
| 77 | + ) |
| 78 | + ) |
| 79 | + ) |
| 80 | + file_path = os.path.abspath(".github/test/unit-test-coreid-CG0006-negative 1.xlsx") |
| 81 | + file_input.send_keys(file_path) |
| 82 | + |
| 83 | + print("Waiting for error result to appear...") |
| 84 | + error_result = wait.until( |
| 85 | + EC.visibility_of_element_located( |
| 86 | + (By.XPATH, '//*[@id="tabpanel-1"]/div[6]/div[1]/div[1]/span/div/span') |
| 87 | + ) |
| 88 | + ) |
| 89 | + print("Error result displayed.") |
| 90 | + |
| 91 | + # Give a few seconds for the POST request to complete |
| 92 | + time.sleep(3) |
| 93 | + |
| 94 | + # Find the rule execution API call |
| 95 | + rule_exec_response = None |
| 96 | + for request in driver.requests: |
| 97 | + if "/api/rules/execute" in request.url: |
| 98 | + if request.response: |
| 99 | + try: |
| 100 | + raw_body = request.response.body |
| 101 | + decompressed = brotli.decompress(raw_body).decode("utf-8") |
| 102 | + rule_exec_response = json.loads(decompressed) |
| 103 | + print("Captured and decoded response from /api/rules/execute") |
| 104 | + break |
| 105 | + except Exception as e: |
| 106 | + print("Error decoding response body:", e) |
| 107 | + |
| 108 | + # Expected content |
| 109 | + expected_json = { |
| 110 | + "DM": [ |
| 111 | + { |
| 112 | + "executionStatus": "success", |
| 113 | + "dataset": "dm.xpt", |
| 114 | + "domain": "DM", |
| 115 | + "variables": [], |
| 116 | + "message": None, |
| 117 | + "errors": [], |
| 118 | + } |
| 119 | + ], |
| 120 | + "FA": [ |
| 121 | + { |
| 122 | + "executionStatus": "success", |
| 123 | + "dataset": "fa.xpt", |
| 124 | + "domain": "FA", |
| 125 | + "variables": ["$val_dy", "FADTC", "FADY", "RFSTDTC"], |
| 126 | + "message": ( |
| 127 | + "FADY is not calculated correctly even though the date portion of FADTC is complete, " |
| 128 | + "the date portion of DM.RFSTDTC is a complete date, and FADY is not empty." |
| 129 | + ), |
| 130 | + "errors": [ |
| 131 | + { |
| 132 | + "value": { |
| 133 | + "FADY": 35, |
| 134 | + "RFSTDTC": "2012-11-15", |
| 135 | + "FADTC": "2012-12-02", |
| 136 | + "$val_dy": 18, |
| 137 | + }, |
| 138 | + "dataset": "fa.xpt", |
| 139 | + "row": 1, |
| 140 | + "USUBJID": "CDISC002", |
| 141 | + "SEQ": 1, |
| 142 | + }, |
| 143 | + { |
| 144 | + "value": { |
| 145 | + "FADY": 3, |
| 146 | + "RFSTDTC": "2013-10-08", |
| 147 | + "FADTC": "2013-10-12", |
| 148 | + "$val_dy": 5, |
| 149 | + }, |
| 150 | + "dataset": "fa.xpt", |
| 151 | + "row": 2, |
| 152 | + "USUBJID": "CDISC004", |
| 153 | + "SEQ": 2, |
| 154 | + }, |
| 155 | + { |
| 156 | + "value": { |
| 157 | + "FADY": -30, |
| 158 | + "RFSTDTC": "2013-01-05", |
| 159 | + "FADTC": "2012-12-02", |
| 160 | + "$val_dy": -34, |
| 161 | + }, |
| 162 | + "dataset": "fa.xpt", |
| 163 | + "row": 4, |
| 164 | + "USUBJID": "CDISC007", |
| 165 | + "SEQ": 4, |
| 166 | + }, |
| 167 | + { |
| 168 | + "value": { |
| 169 | + "FADY": 230, |
| 170 | + "RFSTDTC": "2014-05-11", |
| 171 | + "FADTC": "2014-12-02", |
| 172 | + "$val_dy": 206, |
| 173 | + }, |
| 174 | + "dataset": "fa.xpt", |
| 175 | + "row": 5, |
| 176 | + "USUBJID": "CDISC008", |
| 177 | + "SEQ": 5, |
| 178 | + }, |
| 179 | + ], |
| 180 | + } |
| 181 | + ], |
| 182 | + "IE": [ |
| 183 | + { |
| 184 | + "executionStatus": "success", |
| 185 | + "dataset": "ie.xpt", |
| 186 | + "domain": "IE", |
| 187 | + "variables": ["$val_dy", "IEDTC", "IEDY", "RFSTDTC"], |
| 188 | + "message": ( |
| 189 | + "IEDY is not calculated correctly even though the date portion of IEDTC is complete, " |
| 190 | + "the date portion of DM.RFSTDTC is a complete date, and IEDY is not empty." |
| 191 | + ), |
| 192 | + "errors": [ |
| 193 | + { |
| 194 | + "value": { |
| 195 | + "RFSTDTC": "2022-03-20", |
| 196 | + "IEDTC": "2022-03-17", |
| 197 | + "$val_dy": -3, |
| 198 | + "IEDY": -4, |
| 199 | + }, |
| 200 | + "dataset": "ie.xpt", |
| 201 | + "row": 1, |
| 202 | + "USUBJID": "CDISC-TEST-001", |
| 203 | + "SEQ": 1, |
| 204 | + } |
| 205 | + ], |
| 206 | + } |
| 207 | + ], |
| 208 | + "LB": [ |
| 209 | + { |
| 210 | + "executionStatus": "success", |
| 211 | + "dataset": "lb.xpt", |
| 212 | + "domain": "LB", |
| 213 | + "variables": ["$val_dy", "LBDTC", "LBDY", "RFSTDTC"], |
| 214 | + "message": ( |
| 215 | + "LBDY is not calculated correctly even though the date portion of LBDTC is complete, " |
| 216 | + "the date portion of DM.RFSTDTC is a complete date, and LBDY is not empty." |
| 217 | + ), |
| 218 | + "errors": [ |
| 219 | + { |
| 220 | + "value": { |
| 221 | + "RFSTDTC": "2022-03-20", |
| 222 | + "LBDY": 2, |
| 223 | + "LBDTC": "2022-03-30", |
| 224 | + "$val_dy": 11, |
| 225 | + }, |
| 226 | + "dataset": "lb.xpt", |
| 227 | + "row": 1, |
| 228 | + "USUBJID": "CDISC-TEST-001", |
| 229 | + "SEQ": 1, |
| 230 | + } |
| 231 | + ], |
| 232 | + } |
| 233 | + ], |
| 234 | + } |
| 235 | + |
| 236 | + # Compare result |
| 237 | + if rule_exec_response == expected_json: |
| 238 | + print("Test Passed: API response matches expected JSON.") |
| 239 | + else: |
| 240 | + print("Test Failed: API response does NOT match expected JSON.") |
| 241 | + print("Received:") |
| 242 | + print(json.dumps(rule_exec_response, indent=2)) |
| 243 | + |
| 244 | + |
| 245 | +except Exception as e: |
| 246 | + print(f"Test Failed due to exception: {e}") |
| 247 | + sys.exit(1) |
| 248 | + |
| 249 | +finally: |
| 250 | + driver.quit() |
0 commit comments