Skip to content

Commit ada915b

Browse files
geolocation tests fixed
1 parent c87c41d commit ada915b

File tree

3 files changed

+62
-64
lines changed

3 files changed

+62
-64
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: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,74 @@ def add_to_prefs_list():
2020
"geo.provider.network.url",
2121
"https://www.googleapis.com/geolocation/v1/geolocate?key"
2222
"=%GOOGLE_LOCATION_SERVICE_API_KEY%",
23-
)
23+
),
24+
("devtools.debugger.remote-enabled", True),
25+
("devtools.chrome.enabled", True),
26+
("ui.popup.disable_autohide", True),
2427
]
2528

2629

2730
TEST_URL = "https://www.w3schools.com/html/html5_geolocation.asp"
2831

2932

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):
33+
@pytest.fixture()
34+
def temp_selectors():
35+
return {
36+
"accept-choices": {
37+
"selectorData": "accept-choices",
38+
"strategy": "id",
39+
"groups": [],
40+
},
41+
"geolocation-button-selector": {
42+
"selectorData": "button.w3-btn",
43+
"strategy": "css",
44+
"groups": ["doNotCache"],
45+
},
46+
"location-marker": {
47+
"selectorData": "mapholder",
48+
"strategy": "id",
49+
"groups": ["doNotCache"],
50+
},
51+
}
52+
53+
54+
def test_allow_permission_on_geolocation_via_w3c_api(driver: Firefox, temp_selectors):
6855
"""
6956
C15186 - Verify that geolocation is successfully shared when the user allows permission via the W3C Geolocation API
7057
"""
7158

7259
# Instantiate Objects
7360
nav = Navigation(driver)
7461
web_page = GenericPage(driver, url=TEST_URL).open()
62+
web_page.elements |= temp_selectors
7563
tabs = TabBar(driver)
7664

7765
# Wait for the page to fully load and manually address the cookie banner if appears
7866
nav.wait_for_page_to_load()
79-
handle_cookie_banner(driver, web_page)
67+
cookie_element = web_page.get_elements("accept-choices")
68+
if cookie_element:
69+
cookie_element[0].click()
8070

8171
# Click the 'Try It' button and Allow the location sharing
82-
click_geolocation_button_trigger(web_page)
72+
web_page.click_on("geolocation-button-selector")
8373
nav.handle_geolocation_prompt(button_type="primary")
8474

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

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

9183
# 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")
84+
web_page.click_on("geolocation-button-selector")
85+
nav.handle_geolocation_prompt(button_type="primary", remember_this_decision=True)
9686

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

10092
# Assert that the permission icon is displayed in address bar when in a new tab
10193
tabs.open_web_page_in_new_tab(web_page, num_tabs=3)
@@ -104,39 +96,42 @@ def test_allow_permission_on_geolocation_via_w3c_api(driver: Firefox):
10496
assert permission_icon.is_displayed()
10597

10698

107-
def test_block_permission_on_geolocation_via_w3c_api(driver: Firefox):
99+
def test_block_permission_on_geolocation_via_w3c_api(driver: Firefox, temp_selectors):
108100
"""
109101
C15186 - Verify that geolocation is not shared when the user blocks permission via the W3C Geolocation API
110102
"""
111103

112104
# Instantiate Objects
113105
nav = Navigation(driver)
114106
web_page = GenericPage(driver, url=TEST_URL).open()
107+
web_page.elements |= temp_selectors
115108
tabs = TabBar(driver)
116109

117110
# Wait for the page to fully load and manually address the cookie banner if appears
118111
nav.wait_for_page_to_load()
119-
handle_cookie_banner(driver, web_page)
112+
cookie_element = web_page.get_elements("accept-choices")
113+
if cookie_element:
114+
cookie_element[0].click()
120115

121116
# Click the 'Try It' button and Block the location sharing
122-
click_geolocation_button_trigger(web_page)
117+
web_page.click_on("geolocation-button-selector")
123118
nav.handle_geolocation_prompt(button_type="secondary")
124119

125-
# Assert that the location marker is not displayed
126-
assert not is_location_marker_displayed(web_page)
120+
# Check that the location marker is displayed
121+
# if map is not displayed, style attribute will not be available
122+
assert not web_page.get_element("location-marker").get_attribute("style")
127123

128124
# 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")
125+
tabs.open_web_page_in_new_tab(web_page, num_tabs=2)
126+
web_page.click_on("geolocation-button-selector")
127+
nav.handle_geolocation_prompt(button_type="secondary", remember_this_decision=True)
134128

135-
# Assert that the location marker is not displayed
136-
assert not is_location_marker_displayed(web_page)
129+
# Check that the location marker is displayed
130+
# if map is not displayed, style attribute will not be available
131+
assert not web_page.get_element("location-marker").get_attribute("style")
137132

138133
# 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)
134+
tabs.open_web_page_in_new_tab(web_page, num_tabs=3)
140135
with driver.context(driver.CONTEXT_CHROME):
141136
permission_icon = nav.get_element("permissions-location-icon")
142137
assert permission_icon.is_displayed()

0 commit comments

Comments
 (0)