Skip to content

Commit 11aaf4d

Browse files
authored
Merge pull request #74 from mozilla/hy/google_search_counts_us
hy/google search counts us
2 parents 4de0b1d + c18034e commit 11aaf4d

File tree

7 files changed

+92
-10
lines changed

7 files changed

+92
-10
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ requests = "*"
1313
pytest-xdist = "*"
1414
pytest-html = "*"
1515
pypom = "*"
16+
jsonpath-ng = "*"
1617
pillow = "*"
1718

1819
[dev-packages]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"category-raw": {
3+
"selectorData": "category-raw",
4+
"strategy": "id",
5+
"groups": []
6+
},
7+
8+
"rawdata-tab": {
9+
"selectorData": "//li[@class='tabs-menu-item rawdata ']//a[@id='rawdata-tab']",
10+
"strategy": "xpath",
11+
"groups": []
12+
}
13+
}

modules/page_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,8 @@ def loaded(self):
473473
pass
474474
self.set_content_context()
475475
return _loaded
476+
477+
def switch_tab(self):
478+
"""Get list of all window handles, switch to the newly opened tab"""
479+
handles = self.driver.window_handles
480+
self.driver.switch_to.window(handles[-1])

modules/page_object.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
from modules.page_object_generic_pdf import *
1313
from modules.page_object_google_search import *
1414
from modules.page_object_wiki_firefox_logo import *
15+
from modules.page_object_about_telemetry import *
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from modules.page_base import BasePage
2+
3+
4+
class AboutTelemetry(BasePage):
5+
"""
6+
The POM for the about:telemetry page
7+
8+
Attributes
9+
----------
10+
driver: selenium.webdriver.Firefox
11+
WebDriver object under test
12+
"""
13+
14+
URL_TEMPLATE = "about:telemetry"

modules/util.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import base64
2+
import json
13
import logging
24
import os
35
import platform
46
import re
57
from os import remove
68
from random import shuffle
9+
from typing import Union
10+
from jsonpath_ng import parse
711
from typing import Literal, Union
812

913
from faker import Faker
@@ -301,6 +305,19 @@ def normalize_phone_number(self, phone: str, default_country_code="1") -> str:
301305
# return with the country code and the normalized phone number
302306
return default_country_code + ret_val
303307

308+
def decode_url(self, driver: Firefox):
309+
"""Decode to base64"""
310+
base64_data = driver.current_url.split(",")[1]
311+
decoded_data = base64.b64decode(base64_data).decode("utf-8")
312+
json_data = json.loads(decoded_data)
313+
return json_data
314+
315+
def assert_json_value(self, json_data, jsonpath_expr, expected_value):
316+
"""Parse json and validate json search string with its value"""
317+
expr = parse(jsonpath_expr)
318+
match = expr.find(json_data)
319+
return match[0].value == expected_value, f"Expected {expected_value}, but got {match[0].value}"
320+
304321

305322
class BrowserActions:
306323
"""
@@ -365,15 +382,15 @@ def search(self, term: str, with_enter=True):
365382
url_bar.send_keys(term)
366383

367384
def filter_elements_by_attr(
368-
self, elements: list[WebElement], attr: str, value: str
385+
self, elements: list[WebElement], attr: str, value: str
369386
) -> list[WebElement]:
370387
"""
371388
Given a list of WebElements, return the ones where attribute `attr` has value `value`.
372389
"""
373390
return [el for el in elements if el.get_attribute(attr) == value]
374391

375392
def pick_element_from_list_by_text(
376-
self, elements: list[WebElement], substr: str
393+
self, elements: list[WebElement], substr: str
377394
) -> WebElement:
378395
"""
379396
Given a list of WebElements, return the one where innerText matches `substr`.
@@ -464,7 +481,7 @@ def __init__(self, driver: Firefox):
464481
self.driver = driver
465482

466483
def get_shadow_content(
467-
self, element: WebElement
484+
self, element: WebElement
468485
) -> list[Union[WebElement, ShadowRoot]]:
469486
"""
470487
Given a WebElement, return the shadow DOM root or roots attached to it. Returns a list.
@@ -498,7 +515,7 @@ def shadow_from_script():
498515
return []
499516

500517
def css_selector_matches_element(
501-
self, element: Union[WebElement, ShadowRoot], selector: list
518+
self, element: Union[WebElement, ShadowRoot], selector: list
502519
) -> bool:
503520
if type(element) == ShadowRoot:
504521
return False
@@ -508,7 +525,7 @@ def css_selector_matches_element(
508525
)
509526

510527
def find_shadow_chrome_element(
511-
self, nodes: list[WebElement], selector: list
528+
self, nodes: list[WebElement], selector: list
512529
) -> Union[WebElement, None]:
513530
logging.info("Selecting element in Chrome Context Shadow DOM...")
514531
if selector[0] not in self.allowed_selectors_shadow_chrome_element:
@@ -534,11 +551,11 @@ def find_shadow_chrome_element(
534551
return None
535552

536553
def find_shadow_element(
537-
self,
538-
shadow_parent: Union[WebElement, ShadowRoot],
539-
selector: list,
540-
multiple=False,
541-
context="content",
554+
self,
555+
shadow_parent: Union[WebElement, ShadowRoot],
556+
selector: list,
557+
multiple=False,
558+
context="content",
542559
) -> WebElement:
543560
"""
544561
Given a WebElement with a shadow root attached, find a selector in the
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
import pytest
3+
from selenium.webdriver import Firefox
4+
5+
from modules.browser_object import Navigation
6+
from modules.page_object import AboutTelemetry
7+
8+
from modules.util import Utilities
9+
10+
# unstable: for some reason cannot pass in Taskcluster Linux VM
11+
@pytest.mark.unstable
12+
def test_google_search_counts_us(driver: Firefox):
13+
"""
14+
C1365026, Test Google Search counts - urlbar US
15+
"""
16+
# instantiate objects
17+
nav = Navigation(driver).open()
18+
nav.search("festival")
19+
time.sleep(5)
20+
about_telemetry = AboutTelemetry(driver).open()
21+
u = Utilities()
22+
23+
# Click on Raw JSON, switch tab and click on Raw Data
24+
about_telemetry.get_element("category-raw").click()
25+
about_telemetry.switch_tab()
26+
about_telemetry.get_element("rawdata-tab").click()
27+
28+
# Verify pings are recorded
29+
json_data = u.decode_url(driver)
30+
assert u.assert_json_value(json_data, '$..SEARCH_COUNTS.["google-b-1-d.urlbar"].sum', 1)
31+
assert u.assert_json_value(json_data, '$..["browser.search.content.urlbar"].["google:tagged:firefox-b-1-d"]', 1)

0 commit comments

Comments
 (0)