Debian Codegraph @

tests/selenium/options_test.py

raw
1 #!/usr/bin/env python
2
3 import unittest
4
5 import pytest
6
7 import pbtest
8
9 from selenium.common.exceptions import (
10 NoSuchElementException,
11 TimeoutException,
12 )
13 from selenium.webdriver.common.by import By
14 from selenium.webdriver.support import expected_conditions as EC
15 from selenium.webdriver.support.select import Select
16 from selenium.webdriver.support.ui import WebDriverWait
17
18
19 class OptionsTest(pbtest.PBSeleniumTest):
20 """Make sure the options page works correctly."""
21
22 def assert_domain_toggle_state(self, domain, action, failure_msg):
23 clicker = self.find_el_by_css(f'div[data-origin="{domain}"]')
24 assert clicker.get_dom_attribute("class") == "clicker userset", failure_msg
25
26 switches_div = clicker.find_element(By.CSS_SELECTOR, ".switch-container")
27 assert switches_div.get_dom_attribute("class") == f"switch-container {action}", failure_msg
28
29 def find_domain_toggle_by_xpath(self, domain):
30 toggles_div = self.driver.find_element(By.ID, "blockedResourcesInner")
31 return toggles_div.find_element(By.XPATH, (
32 './/div[@data-origin="{domain}"]'
33 # test that "origin" is one of the classes on the element:
34 # https://stackoverflow.com/a/1390680
35 '//div[contains(concat(" ", normalize-space(@class), " "), " origin ")]'
36 '//span[text()="{domain}"]'
37 ).format(domain=domain))
38
39 def select_domain_list_tab(self):
40 self.find_el_by_css('a[href="#tab-tracking-domains"]').click()
41
42 def select_manage_data_tab(self):
43 self.find_el_by_css('a[href="#tab-manage-data"]').click()
44
45 def load_options_page(self):
46 self.load_url(self.options_url + '?all')
47 self.wait_for_script("return window.OPTIONS_INITIALIZED")
48
49 def test_reloading_should_reapply_filters(self):
50 FILTERVAL = "user"
51
52 self.load_options_page()
53 self.select_domain_list_tab()
54
55 # change a domain list filter
56 Select(self.find_el_by_css('#tracking-domains-type-filter')).select_by_value(FILTERVAL)
57
58 # reload page and assert filters are set
59 self.driver.refresh()
60 sel = Select(self.find_el_by_css('#tracking-domains-type-filter'))
61 assert sel.first_selected_option.get_property('value') == FILTERVAL
62
63 # open options page in a new window and assert filters are not set
64 self.open_window()
65 self.load_options_page()
66 self.select_domain_list_tab()
67 sel = Select(self.find_el_by_css('#tracking-domains-type-filter'))
68 assert not sel.first_selected_option.get_property('value')
69
70 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type == "edge")
71 def test_adding_domain(self):
72 """Ensure domain and tracker count are displayed."""
73 self.clear_tracker_data()
74
75 self.add_domain("pbtest.org", "block")
76
77 self.load_options_page()
78 self.select_domain_list_tab()
79
80 error_message = "The 'multiple tracker' message should be displayed after adding a domain"
81 assert self.driver.find_element(By.ID, "options_domain_list_trackers").is_displayed(), error_message
82 assert not self.driver.find_element(By.ID, "options_domain_list_no_trackers").is_displayed(), error_message
83
84 try:
85 self.find_domain_toggle_by_xpath("pbtest.org")
86 except NoSuchElementException:
87 self.fail("Tracking domain is not displayed")
88
89 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type == "edge")
90 def test_removing_domain(self):
91 """Ensure domain is removed properly."""
92 self.clear_tracker_data()
93 self.add_domain("pbtest.org", "block")
94
95 self.load_options_page()
96 self.select_domain_list_tab()
97
98 domains = self.driver.find_elements(By.CSS_SELECTOR, 'div.clicker')
99 assert len(domains) == 1, "Should see exactly one domain in the list"
100
101 # remove displayed domain
102 remove_domain_element = self.find_el_by_xpath(
103 './/div[@data-origin="pbtest.org"]'
104 '//a[@class="removeDomain"]')
105 remove_domain_element.click()
106
107 # Make sure the alert is present. Otherwise we get intermittent errors.
108 WebDriverWait(self.driver, 3).until(EC.alert_is_present())
109 self.driver.switch_to.alert.accept()
110
111 # verify that only the 'no trackers' message is displayed
112 try:
113 WebDriverWait(self.driver, 5).until(
114 EC.visibility_of_element_located((By.ID, "options_domain_list_no_trackers")))
115 except TimeoutException:
116 self.fail("There should be a 'no trackers' message after deleting domain")
117
118 error_message = "Only the 'no trackers' message should be displayed"
119 assert not self.driver.find_element(By.ID, "options_domain_list_trackers").is_displayed(), error_message
120
121 # verify that no domains are displayed
122 try:
123 domains = self.driver.find_elements(By.CSS_SELECTOR, 'div.clicker')
124 except NoSuchElementException:
125 domains = []
126 assert len(domains) == 0, "No domains should be displayed after removal"
127
128 def test_reset_data(self):
129 self.load_options_page()
130 self.select_domain_list_tab()
131
132 # make sure the default tracker list includes many trackers
133 error_message = "By default, the tracker list should contain many trackers"
134 assert self.driver.find_element(By.ID, "options_domain_list_trackers").is_displayed(), error_message
135 assert not self.driver.find_element(By.ID, "options_domain_list_no_trackers").is_displayed(), error_message
136
137 # get the number of trackers in the seed data
138 default_summary_text = self.driver.find_element(By.ID, "options_domain_list_trackers").text
139
140 # Click on the "remove all data" button to empty the tracker lists, and
141 # click "OK" in the popup that ensues
142 self.select_manage_data_tab()
143 with self.wait_for_reload():
144 self.driver.find_element(By.ID, 'removeAllData').click()
145 self.driver.switch_to.alert.accept()
146
147 # now make sure the tracker list is empty
148 self.select_domain_list_tab()
149 error_message = "No trackers should be displayed after removing all data"
150 assert not self.driver.find_element(By.ID, "options_domain_list_trackers").is_displayed(), error_message
151 assert self.driver.find_element(By.ID, "options_domain_list_no_trackers").is_displayed(), error_message
152
153 # add new blocked domains
154 self.add_domain("pbtest.org", "block")
155 self.add_domain("pbtest1.org", "block")
156
157 # reload the options page
158 self.load_options_page()
159 self.select_domain_list_tab()
160
161 # make sure only two trackers are displayed now
162 actual_text = self.driver.find_element(By.ID, "options_domain_list_trackers").text
163 expected_text = self.js("return chrome.i18n.getMessage("
164 "'options_domain_list_trackers', [2, '']);").replace("</a>", "")
165 assert actual_text == expected_text
166
167 # click the "reset data" button to restore seed data and get rid of the
168 # domains we learned
169 self.select_manage_data_tab()
170 with self.wait_for_reload():
171 self.driver.find_element(By.ID, 'resetData').click()
172 self.driver.switch_to.alert.accept()
173
174 # make sure the same number of trackers are displayed as by default
175 self.select_domain_list_tab()
176 error_message = "After resetting data, tracker count should return to default"
177 assert self.driver.find_element(By.ID, "options_domain_list_trackers").text == default_summary_text, error_message
178
179 def tracking_user_overwrite(self, original_action, overwrite_action):
180 """Ensure preferences are persisted when a user overwrites
181 PB's default behaviour for a domain."""
182
183 DOMAIN = "pbtest.org"
184
185 self.clear_tracker_data()
186 self.add_domain(DOMAIN, original_action)
187
188 self.load_options_page()
189 self.wait_for_script("return window.OPTIONS_INITIALIZED")
190 self.find_el_by_css('a[href="#tab-general-settings"]').click()
191 # enable learning to reveal the show-not-yet-blocked checkbox
192 self.find_el_by_css('#local-learning-checkbox').click()
193 self.select_domain_list_tab()
194 self.find_el_by_css('#tracking-domains-show-not-yet-blocked').click()
195 # wait for toggles to finish rendering
196 self.wait_for_script("return window.SLIDERS_DONE")
197
198 # Change user preferences
199 domain_id = DOMAIN.replace(".", "-")
200 self.wait_for_script(f"return $('#{overwrite_action}-{domain_id}')[0];")
201 self.js(f"$('#{overwrite_action}-{domain_id}').trigger('click');")
202
203 # Re-open the tab
204 self.load_options_page()
205 self.select_domain_list_tab()
206 self.find_el_by_css('#tracking-domains-show-not-yet-blocked').click()
207 # wait for toggles to finish rendering
208 self.wait_for_script("return window.SLIDERS_DONE")
209
210 # Check the user preferences for the domains are still displayed
211 failure_msg = (
212 f"Domain should be displayed as {overwrite_action} "
213 f"after user overwrite of PB's decision to {original_action}")
214 self.assert_domain_toggle_state(DOMAIN, overwrite_action, failure_msg)
215
216 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
217 def test_tracking_user_overwrite_allowed_block(self):
218 self.tracking_user_overwrite('allow', 'block')
219
220 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
221 def test_tracking_user_overwrite_allowed_cookieblock(self):
222 self.tracking_user_overwrite('allow', 'cookieblock')
223
224 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
225 def test_tracking_user_overwrite_cookieblocked_allow(self):
226 self.tracking_user_overwrite('cookieblock', 'allow')
227
228 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
229 def test_tracking_user_overwrite_cookieblocked_block(self):
230 self.tracking_user_overwrite('cookieblock', 'block')
231
232 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
233 def test_tracking_user_overwrite_blocked_allow(self):
234 self.tracking_user_overwrite('block', 'allow')
235
236 @pytest.mark.flaky(reruns=5, condition=pbtest.shim.browser_type in ("chrome", "edge"))
237 def test_tracking_user_overwrite_blocked_cookieblock(self):
238 self.tracking_user_overwrite('block', 'cookieblock')
239
240 # early-warning check for the open_in_tab attribute of options_ui
241 # https://github.com/EFForg/privacybadger/pull/1775#pullrequestreview-76940251
242 def test_options_ui_open_in_tab(self):
243 # open options page manually
244 self.open_window()
245 self.load_options_page()
246
247 # open the new user intro page
248 self.open_window()
249 self.load_url(self.first_run_url)
250
251 # save open windows
252 handles_before = set(self.driver.window_handles)
253
254 # open options page using dedicated chrome API
255 self.js("chrome.runtime.openOptionsPage();")
256
257 # if we switched to the previously manually opened options page, all good
258 # if we haven't, this must mean options_ui's open_in_tab no longer works
259 new_handles = set(self.driver.window_handles).difference(handles_before)
260 num_newly_opened_windows = len(new_handles)
261
262 if num_newly_opened_windows:
263 self.driver.switch_to.window(new_handles.pop())
264
265 assert num_newly_opened_windows == 0, (
266 "Expected to switch to existing options page, "
267 f"opened a new page ({self.driver.title}) instead: "
268 f"{self.driver.current_url}")
269
270
271 if __name__ == "__main__":
272 unittest.main()