|
| 1 | +from selenium.webdriver.common.by import By |
| 2 | +from selenium.webdriver.support import expected_conditions as EC |
| 3 | + |
| 4 | +from ..factories import CollectionFactory, CuratedUrlFactory, DeltaUrlFactory |
| 5 | +from .base import BaseTestCase |
| 6 | + |
| 7 | + |
| 8 | +class TestPatternApplication(BaseTestCase): |
| 9 | + """Test different types of pattern application""" |
| 10 | + |
| 11 | + def setUp(self) -> None: |
| 12 | + super().setUp() |
| 13 | + self.user, self.password = self.create_test_user(is_staff=True) |
| 14 | + |
| 15 | + self.collection = CollectionFactory(curated_by=self.user) |
| 16 | + |
| 17 | + self.delta_urls = [ |
| 18 | + DeltaUrlFactory(collection=self.collection, url="https://example.com/docs/page1.html"), |
| 19 | + DeltaUrlFactory(collection=self.collection, url="https://example.com/docs/page2.html"), |
| 20 | + ] |
| 21 | + |
| 22 | + self.curated_urls = [ |
| 23 | + CuratedUrlFactory(collection=self.collection, url="https://example.com/docs/page3.html"), |
| 24 | + CuratedUrlFactory(collection=self.collection, url="https://example.com/index.html"), |
| 25 | + ] |
| 26 | + |
| 27 | + self.login(self.user.username, self.password) |
| 28 | + self.driver.get(f"{self.live_server_url}/{self.collection.id}/delta-urls") |
| 29 | + |
| 30 | + def test_create_exclude_pattern(self): |
| 31 | + """Test creating a new exclude pattern.""" |
| 32 | + # Click Exclude Patterns tab |
| 33 | + exclude_patterns_tab = self.wait.until(EC.element_to_be_clickable((By.ID, "excludePatternsTab"))) |
| 34 | + exclude_patterns_tab.click() |
| 35 | + |
| 36 | + # Click Add Pattern button |
| 37 | + add_pattern_button = self.wait.until( |
| 38 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "button.addPattern[aria-controls='exclude_patterns_table']")) |
| 39 | + ) |
| 40 | + add_pattern_button.click() |
| 41 | + |
| 42 | + # Fill up the form using JavaScript and close modal properly |
| 43 | + self.driver.execute_script( |
| 44 | + """ |
| 45 | + document.querySelector("#excludePatternModal #match_pattern_input").value = 'example.com/docs/'; |
| 46 | + document.querySelector('#excludePatternModal .pattern_type_form_select[value="2"]').click(); |
| 47 | + document.querySelector("#excludePatternModal button.btn-primary[type='submit']").click(); |
| 48 | + """ |
| 49 | + ) |
| 50 | + |
| 51 | + # Verify pattern details |
| 52 | + pattern_row = self.wait.until( |
| 53 | + EC.presence_of_element_located((By.XPATH, "//td[contains(text(), 'example.com/docs/')]")) |
| 54 | + ) |
| 55 | + row_text = pattern_row.find_element(By.XPATH, "..").text |
| 56 | + |
| 57 | + assert "example.com/docs/" in row_text |
| 58 | + assert "Multi-URL Pattern" in row_text |
| 59 | + assert "3" in row_text |
| 60 | + |
| 61 | + self.driver.get(f"{self.live_server_url}/{self.collection.id}/delta-urls") |
| 62 | + |
| 63 | + # Verify exclude checkmark for each delta URL |
| 64 | + for delta_url in self.delta_urls: |
| 65 | + row = self.driver.find_element(By.ID, delta_url.url) |
| 66 | + check_icon = row.find_element(By.CSS_SELECTOR, "i[style*='color: green']") |
| 67 | + assert check_icon.text == "check" |
| 68 | + |
| 69 | + def test_create_include_pattern(self): |
| 70 | + """Test creating a new include pattern.""" |
| 71 | + # Click Include Patterns tab |
| 72 | + include_patterns_tab = self.wait.until(EC.element_to_be_clickable((By.ID, "includePatternsTab"))) |
| 73 | + include_patterns_tab.click() |
| 74 | + |
| 75 | + # Click Add Pattern button |
| 76 | + add_pattern_button = self.wait.until( |
| 77 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "button.addPattern[aria-controls='include_patterns_table']")) |
| 78 | + ) |
| 79 | + add_pattern_button.click() |
| 80 | + |
| 81 | + # Fill up the form using JavaScript and close modal properly |
| 82 | + self.driver.execute_script( |
| 83 | + """ |
| 84 | + document.querySelector("#includePatternModal #match_pattern_input").value = 'example.com/docs/'; |
| 85 | + document.querySelector('#includePatternModal .pattern_type_form_select[value="2"]').click(); |
| 86 | + document.querySelector("#includePatternModal button.btn-primary[type='submit']").click(); |
| 87 | + """ |
| 88 | + ) |
| 89 | + |
| 90 | + # Verify pattern details |
| 91 | + pattern_row = self.wait.until( |
| 92 | + EC.presence_of_element_located((By.XPATH, "//td[contains(text(), 'example.com/docs/')]")) |
| 93 | + ) |
| 94 | + row_text = pattern_row.find_element(By.XPATH, "..").text |
| 95 | + |
| 96 | + assert "example.com/docs/" in row_text |
| 97 | + assert "Multi-URL Pattern" in row_text |
| 98 | + assert "3" in row_text |
| 99 | + |
| 100 | + self.driver.get(f"{self.live_server_url}/{self.collection.id}/delta-urls") |
| 101 | + |
| 102 | + # Verify no exclude checkmark for each delta URL |
| 103 | + for delta_url in self.delta_urls: |
| 104 | + row = self.driver.find_element(By.ID, delta_url.url) |
| 105 | + check_icon = row.find_element(By.CSS_SELECTOR, "i[style*='color: red']") |
| 106 | + assert check_icon.text == "close" |
| 107 | + |
| 108 | + def test_create_title_pattern(self): |
| 109 | + """Test creating a new title pattern.""" |
| 110 | + # Click Title Patterns tab |
| 111 | + title_patterns_tab = self.wait.until(EC.element_to_be_clickable((By.ID, "titlePatternsTab"))) |
| 112 | + title_patterns_tab.click() |
| 113 | + |
| 114 | + # Click Add Pattern button |
| 115 | + add_pattern_button = self.wait.until( |
| 116 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "button.addPattern[aria-controls='title_patterns_table']")) |
| 117 | + ) |
| 118 | + add_pattern_button.click() |
| 119 | + |
| 120 | + # Fill up the form using JavaScript and close modal properly |
| 121 | + self.driver.execute_script( |
| 122 | + """ |
| 123 | + document.querySelector("#titlePatternModal #match_pattern_input").value = 'example.com/docs/'; |
| 124 | + document.querySelector("#titlePatternModal #title_pattern_input").value = 'Documentation: {title}'; |
| 125 | + document.querySelector('#titlePatternModal .pattern_type_form_select[value="2"]').click(); |
| 126 | + document.querySelector("#titlePatternModal button.btn-primary[type='submit']").click(); |
| 127 | + """ |
| 128 | + ) |
| 129 | + |
| 130 | + # Verify pattern details |
| 131 | + pattern_row = self.wait.until( |
| 132 | + EC.presence_of_element_located((By.XPATH, "//td[contains(text(), 'example.com/docs/')]")) |
| 133 | + ) |
| 134 | + row_text = pattern_row.find_element(By.XPATH, "..").text |
| 135 | + |
| 136 | + assert "example.com/docs/" in row_text |
| 137 | + assert "Documentation: {title}" in row_text |
| 138 | + assert "Multi-URL Pattern" in row_text |
| 139 | + assert "3" in row_text |
| 140 | + |
| 141 | + self.driver.get(f"{self.live_server_url}/{self.collection.id}/delta-urls") |
| 142 | + table_html = self.driver.find_element(By.ID, "delta_urls_table").get_attribute("outerHTML") |
| 143 | + |
| 144 | + # Verify that previous curated_url now appear in delta_urls page after pattern application |
| 145 | + assert "example.com/docs/page3.html" in table_html |
| 146 | + |
| 147 | + # Verify each delta URL's title has been updated with the pattern |
| 148 | + for delta_url in self.collection.delta_urls.all(): |
| 149 | + expected_title = f"Documentation: {delta_url.scraped_title}" |
| 150 | + assert expected_title in table_html, f"Expected title '{expected_title}' not found in table" |
| 151 | + |
| 152 | + def test_create_documenttype_pattern(self): |
| 153 | + """Test creating a new document type pattern.""" |
| 154 | + # Click Document Type Patterns tab |
| 155 | + documenttype_patterns_tab = self.wait.until(EC.element_to_be_clickable((By.ID, "documentTypePatternsTab"))) |
| 156 | + documenttype_patterns_tab.click() |
| 157 | + |
| 158 | + # Click Add Pattern button |
| 159 | + add_pattern_button = self.wait.until( |
| 160 | + EC.element_to_be_clickable( |
| 161 | + (By.CSS_SELECTOR, "button.addPattern[aria-controls='document_type_patterns_table']") |
| 162 | + ) |
| 163 | + ) |
| 164 | + add_pattern_button.click() |
| 165 | + |
| 166 | + # Fill up the form using JavaScript and close modal properly |
| 167 | + self.driver.execute_script( |
| 168 | + """ |
| 169 | + document.querySelector("#documentTypePatternModal #match_pattern_input").value = 'example.com/docs/'; |
| 170 | + document.querySelector('#documentTypePatternModal .document_type_form_select[value="2"]').click(); // DATA |
| 171 | + document.querySelector('#documentTypePatternModal .pattern_type_form_select[value="2"]').click(); |
| 172 | + document.querySelector("#documentTypePatternModal button.btn-primary[type='submit']").click(); |
| 173 | + """ |
| 174 | + ) |
| 175 | + |
| 176 | + # Verify pattern details |
| 177 | + pattern_row = self.wait.until( |
| 178 | + EC.presence_of_element_located((By.XPATH, "//td[contains(text(), 'example.com/docs/')]")) |
| 179 | + ) |
| 180 | + row_text = pattern_row.find_element(By.XPATH, "..").text |
| 181 | + |
| 182 | + assert "example.com/docs/" in row_text |
| 183 | + assert "Multi-URL Pattern" in row_text |
| 184 | + assert "3" in row_text |
| 185 | + |
| 186 | + self.driver.get(f"{self.live_server_url}/{self.collection.id}/delta-urls") |
| 187 | + |
| 188 | + # Verify document type is set to Data |
| 189 | + for delta_url in self.delta_urls: |
| 190 | + row = self.driver.find_element(By.ID, delta_url.url) |
| 191 | + doc_type_button = row.find_element(By.CSS_SELECTOR, "button.btn-success") |
| 192 | + assert doc_type_button.text == "DATA" |
0 commit comments