Skip to content

Commit 89f8256

Browse files
Added type hints and Playwright improvements to code
1 parent 6d22f79 commit 89f8256

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

pages/result.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
the page object for the DuckDuckGo result page.
44
"""
55

6+
from playwright.sync_api import Page
7+
from typing import List
8+
9+
610
class DuckDuckGoResultPage:
711

8-
RESULT_LINKS = '.result__title a.result__a'
9-
SEARCH_INPUT = '#search_form_input'
10-
11-
def __init__(self, page):
12+
def __init__(self, page: Page) -> None:
1213
self.page = page
14+
self.result_links = page.locator('.result__title a.result__a')
15+
self.search_input = page.locator('#search_form_input')
1316

14-
def result_link_titles(self):
15-
self.page.locator(f'{self.RESULT_LINKS} >> nth=4').wait_for()
16-
titles = self.page.locator(self.RESULT_LINKS).all_text_contents()
17-
return titles
17+
def result_link_titles(self) -> List[str]:
18+
self.result_links.nth(4).wait_for()
19+
return self.result_links.all_text_contents()
1820

19-
def result_link_titles_contain_phrase(self, phrase, minimum=1):
21+
def result_link_titles_contain_phrase(self, phrase: str, minimum: int = 1) -> bool:
2022
titles = self.result_link_titles()
2123
matches = [t for t in titles if phrase.lower() in t.lower()]
2224
return len(matches) >= minimum
23-
24-
def search_input_value(self):
25-
return self.page.input_value(self.SEARCH_INPUT)
26-
27-
def title(self):
28-
return self.page.title()

pages/search.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
the page object for the DuckDuckGo search page.
44
"""
55

6-
class DuckDuckGoSearchPage:
6+
from playwright.sync_api import Page
7+
78

8-
SEARCH_BUTTON = '#search_button_homepage'
9-
SEARCH_INPUT = '#search_form_input_homepage'
9+
class DuckDuckGoSearchPage:
1010

1111
URL = 'https://www.duckduckgo.com'
1212

13-
def __init__(self, page):
13+
def __init__(self, page: Page) -> None:
1414
self.page = page
15+
self.search_button = page.locator('#search_button_homepage')
16+
self.search_input = page.locator('#search_form_input_homepage')
1517

16-
def load(self):
18+
def load(self) -> None:
1719
self.page.goto(self.URL)
1820

19-
def search(self, phrase):
20-
self.page.fill(self.SEARCH_INPUT, phrase)
21-
self.page.click(self.SEARCH_BUTTON)
21+
def search(self, phrase: str) -> None:
22+
self.search_input.fill(phrase)
23+
self.search_button.click()

tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
from pages.result import DuckDuckGoResultPage
88
from pages.search import DuckDuckGoSearchPage
9+
from playwright.sync_api import Page
10+
911

1012

1113
@pytest.fixture
12-
def result_page(page):
14+
def result_page(page: Page) -> DuckDuckGoResultPage:
1315
return DuckDuckGoResultPage(page)
1416

1517

1618
@pytest.fixture
17-
def search_page(page):
19+
def search_page(page: Page) -> DuckDuckGoSearchPage:
1820
return DuckDuckGoSearchPage(page)

tests/test_search.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
These tests cover DuckDuckGo searches.
33
"""
44

5+
from pages.result import DuckDuckGoResultPage
6+
from pages.search import DuckDuckGoSearchPage
57
from playwright.sync_api import expect, Page
68

79

8-
def test_basic_duckduckgo_search(search_page, result_page) -> None:
10+
def test_basic_duckduckgo_search(
11+
page: Page,
12+
search_page: DuckDuckGoSearchPage,
13+
result_page: DuckDuckGoResultPage) -> None:
914

1015
# Given the DuckDuckGo home page is displayed
1116
search_page.load()
@@ -14,11 +19,11 @@ def test_basic_duckduckgo_search(search_page, result_page) -> None:
1419
search_page.search('panda')
1520

1621
# Then the search result query is the phrase
17-
assert 'panda' == result_page.search_input_value()
22+
expect(result_page.search_input).to_have_value('panda')
1823

1924
# And the search result links pertain to the phrase
2025
assert result_page.result_link_titles_contain_phrase('panda')
2126

2227
# And the search result title contains the phrase
23-
assert 'panda' in result_page.title()
28+
expect(page).to_have_title('panda at DuckDuckGo')
2429

0 commit comments

Comments
 (0)