Skip to content

Commit 3f5e6ac

Browse files
Merge pull request #563 from mozilla/vs/Refactoring2
Extracted repeated sequences in Example Page POM Replaced hardcoded values like the URLs, ping validations and search terms with module-level constants for clarity and maintainability Improved variable and test names for better clarity and intent Reformatted long expressions and docstrings for consistency Switched from raw sleeps to functional waits where possible
2 parents ccc9a29 + 139705b commit 3f5e6ac

File tree

41 files changed

+341
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+341
-313
lines changed

modules/page_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def element_has_text(self, name: str, text: str, labels=[]) -> Page:
431431
self.expect(lambda _: text in self.get_element(name, labels=labels).text)
432432
return self
433433

434-
def element_attribute_contains(
434+
def expect_element_attribute_contains(
435435
self, name: str, attr_name: str, attr_value: Union[str, float, int], labels=[]
436436
) -> Page:
437437
"""Expect helper: wait until element attribute contains certain value"""

modules/page_object_autofill.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def verify_form_data(self, sample_data: CreditCardBase | AutofillAddressBase):
230230
autofilled_field = self.get_element("form-field", labels=[field_name])
231231
if autofilled_field.tag_name.lower() != "select":
232232
autofilled_field_value = autofilled_field.get_attribute("value")
233-
# self.element_attribute_contains(
233+
# self.expect_element_attribute_contains(
234234
# "form-field", "value", expected_value, labels=[field_name]
235235
# )
236236
else:

modules/page_object_example_page.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from selenium.webdriver.common.by import By
2+
13
from modules.page_base import BasePage
24

35

@@ -10,3 +12,11 @@ class ExamplePage(BasePage):
1012
TITLE = "Example Domain"
1113
MORE_INFO_URL = "https://www.iana.org/help/example-domains"
1214
MORE_INFO_TITLE = "Example Domains"
15+
16+
@BasePage.context_content
17+
def search_selected_header_via_context_menu(self):
18+
"""Open the page, triple-click the <h1>, right-click it to trigger the context menu."""
19+
self.open()
20+
header = (By.TAG_NAME, "h1")
21+
self.triple_click(header)
22+
self.context_click(header)

modules/page_object_prefs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def set_alternative_language(self, lang_code: str) -> BasePage:
8080
self.get_element("language-option-by-code", labels=[lang_code]).click()
8181
select_language.click()
8282
self.get_element("language-settings-add-button").click()
83-
self.element_attribute_contains(
83+
self.expect_element_attribute_contains(
8484
"language-added-list", "last-selected", f"locale-{lang_code}"
8585
)
8686

@@ -94,7 +94,7 @@ def select_https_only_setting(self, option_id: HttpsOnlyStatus) -> BasePage:
9494
self.find_in_settings("HTTPS")
9595
self.element_clickable(str(option_id))
9696
self.click_on(str(option_id))
97-
self.element_attribute_contains(str(option_id), "checked", "")
97+
self.expect_element_attribute_contains(str(option_id), "checked", "")
9898
return self
9999

100100
def set_default_zoom_level(self, zoom_percentage: int) -> BasePage:

tests/address_bar_and_search/test_adaptive_history_autofill.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
WAIT_TIMEOUT = 10
1010
TEST_URL = "https://www.nationalgeographic.com/science/"
1111
EXPECTED_TITLE = "Science"
12+
EXPECTED_TYPE = "autofill_adaptive"
13+
EXPECTED_TEXT_FRAGMENT = "nationalgeographic.com/science"
1214

1315

1416
@pytest.fixture()
@@ -28,24 +30,26 @@ def test_add_adaptive_history_autofill(driver: Firefox):
2830
nav = Navigation(driver)
2931
tabs = TabBar(driver)
3032

33+
# Step 1: Visit the test site and verify tab title
3134
nav.search(TEST_URL)
3235
WebDriverWait(driver, WAIT_TIMEOUT).until(
3336
lambda d: tabs.get_tab_title(tabs.get_tab(1)) == EXPECTED_TITLE
3437
)
3538

39+
# Step 2: Open new tab, close the original
3640
tabs.new_tab_by_button()
3741
tabs.wait_for_num_tabs(2)
3842
driver.switch_to.window(driver.window_handles[1])
39-
4043
with driver.context(driver.CONTEXT_CHROME):
4144
tabs.get_elements("tab-x-icon")[0].click()
4245

46+
# Step 3: Type in address bar, click adaptive suggestion
4347
nav.type_in_awesome_bar("nat")
4448
with driver.context(driver.CONTEXT_CHROME):
4549
nav.get_element("firefox-suggest").click()
46-
4750
nav.expect_in_content(EC.url_contains(TEST_URL))
4851

52+
# Step 4: Open new tab and check for autofill suggestion
4953
tabs.new_tab_by_button()
5054
tabs.wait_for_num_tabs(2)
5155
driver.switch_to.window(driver.window_handles[-1])
@@ -54,9 +58,9 @@ def test_add_adaptive_history_autofill(driver: Firefox):
5458
tabs.set_chrome_context()
5559
autofill_element = nav.get_element("search-result-autofill-adaptive-element")
5660

57-
assert autofill_element.get_attribute("type") == "autofill_adaptive", (
58-
f"Expected type 'autofill_adaptive', got '{autofill_element.get_attribute('type')}'"
61+
assert autofill_element.get_attribute("type") == EXPECTED_TYPE, (
62+
f"Expected type '{EXPECTED_TYPE}', got '{autofill_element.get_attribute('type')}'"
5963
)
60-
assert "nationalgeographic.com/science" in autofill_element.text, (
64+
assert EXPECTED_TEXT_FRAGMENT in autofill_element.text, (
6165
f"Autofill text did not contain expected URL. Got: {autofill_element.text}"
6266
)

tests/address_bar_and_search/test_add_engine_address_bar.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from modules.browser_object import ContextMenu, Navigation, TabBar
55

6+
TEST_URL = "https://youtube.com"
7+
EXPECTED_ENGINE = "YouTube"
68

79
@pytest.fixture()
810
def test_case():
@@ -22,17 +24,17 @@ def test_add_search_engine_from_address_bar(driver: Firefox):
2224
menu = ContextMenu(driver)
2325
tabs = TabBar(driver)
2426

25-
driver.get("https://youtube.com")
27+
driver.get(TEST_URL)
2628
nav.custom_wait(timeout=20).until(lambda d: "youtube.com" in d.current_url)
2729

2830
with driver.context(driver.CONTEXT_CHROME):
2931
nav.context_click_in_awesome_bar()
3032
menu.click_context_item("context-menu-add-search-engine")
3133

3234
tabs.new_tab_by_button()
33-
youtube_tab = tabs.get_tab_by_title("YouTube")
35+
youtube_tab = tabs.get_tab_by_title(EXPECTED_ENGINE)
3436
tabs.close_tab(youtube_tab)
3537

3638
nav.click_on("searchmode-switcher")
37-
nav.element_exists("search-mode-switcher-option", labels=["YouTube"])
39+
nav.element_exists("search-mode-switcher-option", labels=[EXPECTED_ENGINE])
3840
nav.click_on("searchmode-switcher")
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import pytest
2-
from selenium.common.exceptions import TimeoutException
32
from selenium.webdriver import Firefox
43
from selenium.webdriver.support import expected_conditions as EC
5-
from selenium.webdriver.support.ui import WebDriverWait
64

75
from modules.browser_object import Navigation
86

7+
WAIT_TIMEOUT = 10
98
ADDONS_BASE_URL = "https://addons.mozilla.org/en-US/firefox/addon/"
109

10+
INPUT_TO_ADDON_NAME = {
11+
"clips": "video-downloadhelper",
12+
"grammar": "languagetool",
13+
"Temp mail": "private-relay",
14+
"pics search": "search_by_image",
15+
"darker theme": "darkreader",
16+
"privacy": "privacy-badger17",
17+
"read aloud": "read-aloud",
18+
}
19+
1120

1221
@pytest.fixture()
1322
def test_case():
@@ -26,31 +35,16 @@ def test_addon_suggestion_based_on_search_input(driver: Firefox):
2635
"""
2736
C2234714 - Verify that the address bar suggests relevant add-ons based on search input.
2837
"""
29-
input_to_addon_name = {
30-
"clips": "video-downloadhelper",
31-
"grammar": "languagetool",
32-
"Temp mail": "private-relay",
33-
"pics search": "search_by_image",
34-
"darker theme": "darkreader",
35-
"privacy": "privacy-badger17",
36-
"read aloud": "read-aloud",
37-
}
38-
3938
nav = Navigation(driver)
4039
nav.set_awesome_bar()
4140

42-
for input_text, addon_name in input_to_addon_name.items():
41+
for input_text, addon_slug in INPUT_TO_ADDON_NAME.items():
4342
nav.type_in_awesome_bar(input_text)
44-
try:
45-
nav.element_visible("addon-suggestion")
46-
except TimeoutException:
47-
raise AssertionError(
48-
f"Addon suggestion not visible for input: '{input_text}'"
49-
)
5043

51-
nav.select_element_in_nav("addon-suggestion")
44+
if not nav.element_visible("addon-suggestion"):
45+
raise AssertionError(f"Addon suggestion not visible for input: '{input_text}'")
5246

53-
expected_url = f"{ADDONS_BASE_URL}{addon_name}/"
47+
nav.select_element_in_nav("addon-suggestion")
48+
expected_url = f"{ADDONS_BASE_URL}{addon_slug}/"
5449
nav.expect_in_content(EC.url_contains(expected_url))
55-
5650
nav.clear_awesome_bar()

tests/address_bar_and_search/test_default_search_provider_change_awesome_bar.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def test_default_search_provider_change_awesome_bar(driver: Firefox):
2424

2525
driver.get("about:newtab")
2626
nav.open_searchmode_switcher_settings()
27+
2728
prefs.search_engine_dropdown().select_option(SEARCH_ENGINE)
2829

2930
driver.get("about:newtab")
30-
nav.element_attribute_contains("awesome-bar", "placeholder", EXPECTED_PLACEHOLDER)
31+
nav.expect_element_attribute_contains("awesome-bar", "placeholder", EXPECTED_PLACEHOLDER)

tests/address_bar_and_search/test_default_search_provider_change_legacy_search_bar.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,38 @@ def test_default_search_provider_change_legacy_search_bar(driver: Firefox):
2020
"""
2121
C1365245 - Verify that changing the default search provider is reflected in the legacy search bar.
2222
"""
23+
nav = Navigation(driver)
2324
panel_ui = PanelUi(driver)
2425
customize = CustomizeFirefox(driver)
25-
nav = Navigation(driver)
2626
tabs = TabBar(driver)
2727
prefs = AboutPrefs(driver, category="search")
2828

29+
# Add legacy search bar to toolbar
2930
panel_ui.open_panel_menu()
3031
panel_ui.navigate_to_customize_toolbar()
3132
customize.add_widget_to_toolbar("search-bar")
3233

34+
# Open a new tab and trigger search settings navigation
3335
tabs.new_tab_by_button()
3436
nav.type_in_search_bar(SEARCH_TERM)
3537
nav.click_on_change_search_settings_button()
3638
driver.switch_to.window(driver.window_handles[2])
3739
assert driver.current_url == SEARCH_SETTINGS_URL
3840

41+
# Validate navigation returns to site and settings page opens in new tab
3942
driver.get("https://9gag.com/")
4043
nav.type_in_search_bar(SEARCH_TERM)
4144
nav.click_on_change_search_settings_button()
4245
assert driver.current_url == "https://9gag.com/"
4346
driver.switch_to.window(driver.window_handles[3])
4447
assert driver.current_url == SEARCH_SETTINGS_URL
4548

49+
# Change the default search engine
4650
prefs.open()
4751
prefs.search_engine_dropdown().select_option(SEARCH_ENGINE)
48-
nav.type_in_search_bar(SEARCH_TERM)
4952

53+
# Perform another search and validate engine label
54+
nav.type_in_search_bar(SEARCH_TERM)
5055
with driver.context(driver.CONTEXT_CHROME):
5156
engine_name = driver.find_element(By.CSS_SELECTOR, ".searchbar-engine-name")
5257
assert engine_name.get_attribute("value") == EXPECTED_ENGINE_DISPLAY

0 commit comments

Comments
 (0)