Skip to content

Commit 1f5c922

Browse files
philimon-resetvsangereanMOZ
authored andcommitted
webserver implemented
1 parent d13033a commit 1f5c922

16 files changed

+155
-38
lines changed

l10n_CM/Unified/conftest.py

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import errno
2+
import logging
13
import os
4+
import socket
25
from json import load
36
from typing import List
47

@@ -12,6 +15,28 @@
1215
current_dir = os.path.dirname(__file__)
1316
parent_dir = os.path.dirname(current_dir)
1417

18+
LOCALHOST = "127.0.0.1"
19+
PORT = 8080
20+
21+
22+
def is_port_in_use() -> bool:
23+
"""Check if the port is already open (server running)."""
24+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
25+
return s.connect_ex((LOCALHOST, PORT)) == 0
26+
27+
28+
def get_html_files(live_site):
29+
address_path_to_site = parent_dir + f"/sites/{live_site}/{live_site}_ad.html"
30+
cc_path_to_site = parent_dir + f"/sites/{live_site}/{live_site}_cc.html"
31+
if os.path.exists(address_path_to_site) and os.path.exists(cc_path_to_site):
32+
address_html_file, cc_html_file = "", ""
33+
with open(address_path_to_site, "r", encoding="utf-8") as fp:
34+
address_html_file = fp.read()
35+
with open(cc_path_to_site, "r", encoding="utf-8") as fp:
36+
cc_html_file = fp.read()
37+
return address_html_file, cc_html_file
38+
return ""
39+
1540

1641
@pytest.fixture()
1742
def region():
@@ -58,11 +83,6 @@ def ad_site_data(live_site, region):
5883
return live_site_data
5984

6085

61-
@pytest.fixture()
62-
def ad_is_live_site(live_site):
63-
return live_site != "demo"
64-
65-
6686
@pytest.fixture()
6787
def cc_site_data(live_site):
6888
cc_live_site = f"{live_site}/{live_site}_cc"
@@ -72,11 +92,40 @@ def cc_site_data(live_site):
7292
return live_site_data
7393

7494

75-
@pytest.fixture()
76-
def cc_is_live_site(live_site):
95+
@pytest.fixture
96+
def is_live_site(live_site):
97+
"""Determine if the site is live."""
7798
return live_site != "demo"
7899

79100

101+
@pytest.fixture(scope="session")
102+
def httpserver_listen_address():
103+
"""Set port for local http server"""
104+
return LOCALHOST, PORT
105+
106+
107+
@pytest.fixture()
108+
def serve_live_site(is_live_site, live_site, request):
109+
"""Serve the live site only if needed."""
110+
if not is_live_site or is_port_in_use():
111+
return
112+
# only serve content if url is already not served.
113+
try:
114+
http_server = request.getfixturevalue("httpserver")
115+
ad_html_file, cc_html_file = get_html_files(live_site)
116+
http_server.expect_request(f"/{live_site}_ad.html").respond_with_data(
117+
ad_html_file, content_type="text/html"
118+
)
119+
http_server.expect_request(f"/{live_site}_cc.html").respond_with_data(
120+
cc_html_file, content_type="text/html"
121+
)
122+
except OSError as e:
123+
if e == errno.EADDRINUSE:
124+
logging.info(f"{live_site} already served.")
125+
finally:
126+
return
127+
128+
80129
@pytest.fixture()
81130
def ad_form_field(ad_site_data):
82131
selector = ad_site_data.get("form_field")
@@ -98,7 +147,7 @@ def cc_form_field(cc_site_data):
98147

99148

100149
@pytest.fixture()
101-
def address_autofill(driver, ad_site_data, ad_form_field):
150+
def address_autofill(driver, ad_site_data, ad_form_field, serve_live_site):
102151
af = AddressFill(
103152
driver,
104153
url_template=ad_site_data.get("url"),
@@ -110,7 +159,7 @@ def address_autofill(driver, ad_site_data, ad_form_field):
110159

111160

112161
@pytest.fixture()
113-
def credit_card_autofill(driver, cc_site_data, cc_form_field):
162+
def credit_card_autofill(driver, cc_site_data, cc_form_field, serve_live_site):
114163
cf = CreditCardFill(
115164
driver,
116165
url_template=cc_site_data.get("url"),

l10n_CM/Unified/test_demo_ad_1a_dropdown_name_org_fields.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import pytest
22
from selenium.webdriver import Firefox
33

4-
from modules.browser_object_autofill_popup import AutofillPopup
54
from modules.classes.autofill_base import AutofillAddressBase
65
from modules.page_object_autofill import AddressFill
7-
from modules.util import Utilities
86

97

108
@pytest.fixture()
@@ -16,8 +14,6 @@ def test_dropdown_presence_name_organization(
1614
driver: Firefox,
1715
region: str,
1816
address_autofill: AddressFill,
19-
util: Utilities,
20-
autofill_popup: AutofillPopup,
2117
fill_and_save_address: AutofillAddressBase,
2218
):
2319
"""

l10n_CM/Unified/test_demo_ad_6_capture_doorhanger_appearance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def test_address_doorhanger_displayed_after_entering_valid_address(
1717
address_autofill: AddressFill,
1818
util: Utilities,
1919
autofill_popup: AutofillPopup,
20-
ad_is_live_site: str,
20+
is_live_site: str,
2121
):
2222
"""
2323
C2886581 - Verify the Capture Door hanger is displayed after entering valid Address data
2424
"""
25-
if not ad_is_live_site:
25+
if not is_live_site:
2626
# Create fake data and fill it in
2727
address_autofill.open()
2828
# scroll to first form field

l10n_CM/Unified/test_demo_ad_7a_capture_doorhanger_stored_data_name_org_fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_demo_ad_name_org_captured_in_doorhanger_and_stored(
1919
util: Utilities,
2020
autofill_popup: AutofillPopup,
2121
about_prefs_privacy: AboutPrefs,
22-
ad_is_live_site: str,
22+
is_live_site: str,
2323
):
2424
"""
2525
C2888701 - Verify name/org fields are captured in the Capture Doorhanger and stored in about:preferences
2626
"""
27-
if not ad_is_live_site:
27+
if not is_live_site:
2828
# Create fake data and fill it in
2929
address_autofill.open()
3030
# scroll to first form field

l10n_CM/Unified/test_demo_ad_7b_capture_doorhanger_stored_data_address_fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_demo_ad_address_data_captured_in_doorhanger_and_stored(
1919
util: Utilities,
2020
autofill_popup: AutofillPopup,
2121
about_prefs_privacy: AboutPrefs,
22-
ad_is_live_site: str,
22+
is_live_site: str,
2323
):
2424
"""
2525
C2888703 - Verify Address data are captured in the Capture Doorhanger and stored in about:preferences
2626
"""
27-
if not ad_is_live_site:
27+
if not is_live_site:
2828
# Create fake data and fill it in
2929
address_autofill.open()
3030

l10n_CM/Unified/test_demo_ad_7c_capture_doorhanger_stored_data_phone_email.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_demo_ad_email_phone_captured_in_doorhanger_and_stored(
1919
util: Utilities,
2020
autofill_popup: AutofillPopup,
2121
about_prefs_privacy: AboutPrefs,
22-
ad_is_live_site: str,
22+
is_live_site: str,
2323
):
2424
"""
2525
C2888704 - Verify tele/email data are captured in the Capture Doorhanger and stored in about:preferences
2626
"""
27-
if not ad_is_live_site:
27+
if not is_live_site:
2828
# Create fake data and fill it in
2929
address_autofill.open()
3030

l10n_CM/Unified/test_demo_cc_6_capture_doorhanger_appearance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def test_cc_check_door_hanger_is_displayed(
1717
util: Utilities,
1818
autofill_popup: AutofillPopup,
1919
credit_card_autofill: CreditCardFill,
20-
cc_is_live_site: str,
20+
is_live_site: str,
2121
):
2222
"""
2323
C2889441 - Ensures that the door hanger is displayed after filling credit card info
2424
"""
25-
if not cc_is_live_site:
25+
if not is_live_site:
2626
# Navigate to page
2727
credit_card_autofill.open()
2828

l10n_CM/Unified/test_demo_cc_7_capture_doorhanger_capture_and_stored_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_demo_cc_data_captured_in_doorhanger_and_stored(
1919
autofill_popup: AutofillPopup,
2020
util: Utilities,
2121
about_prefs_privacy: AboutPrefs,
22-
cc_is_live_site: str,
22+
is_live_site: str,
2323
):
2424
"""
2525
C2889999 - Verify credit card data is captured in the Capture Doorhanger and stored in about:preferences
2626
"""
27-
if not cc_is_live_site:
27+
if not is_live_site:
2828
# Navigate to page
2929
credit_card_autofill.open()
3030

l10n_CM/constants/walmart/walmart_ad.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"url": "http://127.0.0.1:8000/walmart_ad.html",
2+
"url": "http://127.0.0.1:8080/walmart_ad.html",
33
"field_mapping": {
44
"given_name": "given-name",
55
"family_name": "family-name",

l10n_CM/constants/walmart/walmart_cc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"url": "http://127.0.0.1:8000/walmart_cc.html",
2+
"url": "http://127.0.0.1:8080/walmart_cc.html",
33
"field_mapping": {
44
"card_number": "cc-number",
55
"family_name": "cc-family-name",

0 commit comments

Comments
 (0)