Skip to content

Commit a285fc0

Browse files
Merge pull request #635 from mozilla/philimon/fix_test_geolocation_w3c
Philimon/geolocation_tests_fixed
2 parents 0489cb6 + 3003a49 commit a285fc0

File tree

3 files changed

+58
-63
lines changed

3 files changed

+58
-63
lines changed

modules/browser_object_navigation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,16 @@ def refresh_page(self) -> BasePage:
300300
self.wait_for_page_to_load()
301301
return self
302302

303-
def handle_geolocation_prompt(self, button_type="primary"):
303+
def handle_geolocation_prompt(
304+
self, button_type="primary", remember_this_decision=False
305+
):
304306
"""
305307
Handles geolocation prompt by clicking either the 'Allow' or 'Block' button based on the button_type provided
306308
"""
307309
button_selector = f"popup-notification-{button_type}-button"
308310
self.element_clickable(button_selector)
311+
if remember_this_decision:
312+
self.click_on("checkbox-remember-this-decision")
309313
self.click_on(button_selector)
310314

311315
def open_searchmode_switcher_settings(self):

modules/data/generic_page.components.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,4 @@
7777
"strategy": "xpath",
7878
"groups": []
7979
}
80-
8180
}

tests/geolocation/test_geolocation_shared_via_w3c_api.py

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,75 +27,64 @@ def add_to_prefs_list():
2727
TEST_URL = "https://www.w3schools.com/html/html5_geolocation.asp"
2828

2929

30-
def handle_cookie_banner(driver, web_page):
31-
"""
32-
Address the cookie banner manually if appears, as the cookie banner dismissal preference is not effective in this
33-
context
34-
"""
35-
try:
36-
driver.switch_to.window(driver.window_handles[-1])
37-
web_page.find_element(By.ID, "accept-choices").click()
38-
except NoSuchElementException:
39-
# If the cookie banner is not found, continue with the test
40-
pass
41-
42-
43-
def click_geolocation_button_trigger(web_page):
44-
"""
45-
Clicks the 'Try It' button inside the webpage to trigger the geolocation prompt
46-
"""
47-
geolocation_button_selector = (
48-
"button.w3-btn.w3-blue.w3-round[onclick='getLocation()']"
49-
)
50-
web_page.find_element(By.CSS_SELECTOR, geolocation_button_selector).click()
51-
52-
53-
def is_location_marker_displayed(web_page):
54-
"""
55-
Checks if the location marker ('You are here!') is displayed on the web page inside the map.
56-
"""
57-
location_marker = web_page.find_elements(
58-
By.CSS_SELECTOR, "div[aria-label='You are here!'][role='img']"
59-
)
60-
61-
if location_marker: # Check if the marker exists
62-
return location_marker[0].is_displayed()
63-
else:
64-
return False
65-
66-
67-
def test_allow_permission_on_geolocation_via_w3c_api(driver: Firefox):
30+
@pytest.fixture()
31+
def temp_selectors():
32+
return {
33+
"accept-choices": {
34+
"selectorData": "accept-choices",
35+
"strategy": "id",
36+
"groups": [],
37+
},
38+
"geolocation-button-selector": {
39+
"selectorData": "button.w3-btn",
40+
"strategy": "css",
41+
"groups": ["doNotCache"],
42+
},
43+
"location-marker": {
44+
"selectorData": "mapholder",
45+
"strategy": "id",
46+
"groups": ["doNotCache"],
47+
},
48+
}
49+
50+
51+
def test_allow_permission_on_geolocation_via_w3c_api(driver: Firefox, temp_selectors):
6852
"""
6953
C15186 - Verify that geolocation is successfully shared when the user allows permission via the W3C Geolocation API
7054
"""
7155

7256
# Instantiate Objects
7357
nav = Navigation(driver)
7458
web_page = GenericPage(driver, url=TEST_URL).open()
59+
web_page.elements |= temp_selectors
7560
tabs = TabBar(driver)
7661

7762
# Wait for the page to fully load and manually address the cookie banner if appears
7863
nav.wait_for_page_to_load()
79-
handle_cookie_banner(driver, web_page)
64+
cookie_element = web_page.get_elements("accept-choices")
65+
if cookie_element:
66+
cookie_element[0].click()
8067

8168
# Click the 'Try It' button and Allow the location sharing
82-
click_geolocation_button_trigger(web_page)
69+
web_page.click_on("geolocation-button-selector")
8370
nav.handle_geolocation_prompt(button_type="primary")
8471

85-
# Assert that the location marker is displayed
86-
assert is_location_marker_displayed(web_page)
72+
# Check that the location marker is displayed
73+
# if map is displayed, style attribute will be available
74+
web_page.element_visible("location-marker")
75+
assert web_page.get_element("location-marker").get_attribute("style")
8776

8877
# Open a new tab, because refresh will keep the allow state of the location for one hour or until the tab is closed
8978
tabs.open_web_page_in_new_tab(web_page, num_tabs=2)
9079

9180
# Click the 'Try It' button and Allow the location sharing while choose the option Remember this decision
92-
click_geolocation_button_trigger(web_page)
93-
nav.element_clickable("checkbox-remember-this-decision")
94-
nav.click_on("checkbox-remember-this-decision")
95-
nav.handle_geolocation_prompt(button_type="primary")
81+
web_page.click_on("geolocation-button-selector")
82+
nav.handle_geolocation_prompt(button_type="primary", remember_this_decision=True)
9683

97-
# Assert that the location marker is displayed again
98-
assert is_location_marker_displayed(web_page)
84+
# Check that the location marker is displayed
85+
# if map is displayed, style attribute will be available
86+
web_page.element_visible("location-marker")
87+
assert web_page.get_element("location-marker").get_attribute("style")
9988

10089
# Assert that the permission icon is displayed in address bar when in a new tab
10190
tabs.open_web_page_in_new_tab(web_page, num_tabs=3)
@@ -104,39 +93,42 @@ def test_allow_permission_on_geolocation_via_w3c_api(driver: Firefox):
10493
assert permission_icon.is_displayed()
10594

10695

107-
def test_block_permission_on_geolocation_via_w3c_api(driver: Firefox):
96+
def test_block_permission_on_geolocation_via_w3c_api(driver: Firefox, temp_selectors):
10897
"""
10998
C15186 - Verify that geolocation is not shared when the user blocks permission via the W3C Geolocation API
11099
"""
111100

112101
# Instantiate Objects
113102
nav = Navigation(driver)
114103
web_page = GenericPage(driver, url=TEST_URL).open()
104+
web_page.elements |= temp_selectors
115105
tabs = TabBar(driver)
116106

117107
# Wait for the page to fully load and manually address the cookie banner if appears
118108
nav.wait_for_page_to_load()
119-
handle_cookie_banner(driver, web_page)
109+
cookie_element = web_page.get_elements("accept-choices")
110+
if cookie_element:
111+
cookie_element[0].click()
120112

121113
# Click the 'Try It' button and Block the location sharing
122-
click_geolocation_button_trigger(web_page)
114+
web_page.click_on("geolocation-button-selector")
123115
nav.handle_geolocation_prompt(button_type="secondary")
124116

125-
# Assert that the location marker is not displayed
126-
assert not is_location_marker_displayed(web_page)
117+
# Check that the location marker is displayed
118+
# if map is not displayed, style attribute will not be available
119+
assert not web_page.get_element("location-marker").get_attribute("style")
127120

128121
# Click the 'Try It' button and Block the location sharing while choose the option Remember this decision
129-
nav.refresh_page()
130-
click_geolocation_button_trigger(web_page)
131-
nav.element_clickable("checkbox-remember-this-decision")
132-
nav.click_on("checkbox-remember-this-decision")
133-
nav.handle_geolocation_prompt(button_type="secondary")
122+
tabs.open_web_page_in_new_tab(web_page, num_tabs=2)
123+
web_page.click_on("geolocation-button-selector")
124+
nav.handle_geolocation_prompt(button_type="secondary", remember_this_decision=True)
134125

135-
# Assert that the location marker is not displayed
136-
assert not is_location_marker_displayed(web_page)
126+
# Check that the location marker is displayed
127+
# if map is not displayed, style attribute will not be available
128+
assert not web_page.get_element("location-marker").get_attribute("style")
137129

138130
# Assert that the permission icon is displayed in address bar when in a new tab
139-
tabs.open_web_page_in_new_tab(web_page, num_tabs=2)
131+
tabs.open_web_page_in_new_tab(web_page, num_tabs=3)
140132
with driver.context(driver.CONTEXT_CHROME):
141133
permission_icon = nav.get_element("permissions-location-icon")
142134
assert permission_icon.is_displayed()

0 commit comments

Comments
 (0)