Skip to content

Commit 22eacd8

Browse files
Merge branch '5-playwright-tricks' into 6-api-testing
2 parents 5888d91 + 9477499 commit 22eacd8

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

pages/result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DuckDuckGoResultPage:
1111

1212
def __init__(self, page: Page) -> None:
1313
self.page = page
14-
self.result_links = page.locator('.result__title a.result__a')
14+
self.result_links = page.locator('a[data-testid="result-title-a"]')
1515
self.search_input = page.locator('#search_form_input')
1616

1717
def result_link_titles(self) -> List[str]:

tutorial/3-assertions.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ Here's the inspection panel for result links:
9999

100100
![Inspecting the result link elements](images/inspect-result-links.png)
101101

102-
Result links are `a` elements with the class `result__a`.
103-
They are under `h2` elements with the class `result__title`.
104-
We could use the selector `.result__title a.result__a` to identify all result links on the page.
105-
(If you look in the DevTools search bar, you'll see that this selector locates 10 elements.)
102+
Result links are `a` elements with a `data-testid` attribute set to `"result-title-a"`.
103+
We could use the CSS selector `a[data-testid="result-title-a"]` to identify all result links on the page.
104+
(If you look in the DevTools search bar, you'll see that this selector locates 12 elements.)
106105

107106
Since we can get all elements with one selector,
108107
we can take the following steps to verify that search result links pertain to the phrase:
@@ -121,7 +120,7 @@ Explicit waiting will be tricky.
121120
Add the following line to the test:
122121

123122
```python
124-
page.locator('.result__title a.result__a').nth(4).wait_for()
123+
page.locator('a[data-testid="result-title-a"]').nth(4).wait_for()
125124
```
126125

127126
Let's break this down:
@@ -130,7 +129,7 @@ Let's break this down:
130129
[`Locator`](https://playwright.dev/python/docs/api/class-locator) object for the target element.
131130
A `Locator` object can make many of the same calls as a page, like clicking and getting text.
132131
However, it can also make calls for explicit waiting and calls that target multiple elements.
133-
2. `.result__title a.result__a` is the selector for the result links.
132+
2. `a[data-testid="result-title-a"]` is the selector for the result links.
134133
3. `nth(4)` is an [N-th element](https://playwright.dev/python/docs/api/class-locator#locator-nth) fetcher.
135134
N-th element fetchers are zero-indexed and may be appended to any selector.
136135
In this call, it will fetch the fifth result link element.
@@ -145,7 +144,7 @@ Waiting for five links to appear should be good enough for our testing purposes.
145144
After the links appear, we can scrape their text contents like this:
146145

147146
```python
148-
titles = page.locator('.result__title a.result__a').all_text_contents()
147+
titles = page.locator('a[data-testid="result-title-a"]').all_text_contents()
149148
```
150149

151150
Again, we must use the `locator` method because we want to target a list of elements instead of one.
@@ -194,8 +193,8 @@ def test_basic_duckduckgo_search(page: Page) -> None:
194193
expect(page.locator('#search_form_input')).to_have_value('panda')
195194

196195
# And the search result links pertain to the phrase
197-
page.locator('.result__title a.result__a').nth(4).wait_for()
198-
titles = page.locator('.result__title a.result__a').all_text_contents()
196+
page.locator('a[data-testid="result-title-a"]').nth(4).wait_for()
197+
titles = page.locator('a[data-testid="result-title-a"]').all_text_contents()
199198
matches = [t for t in titles if 'panda' in t.lower()]
200199
assert len(matches) > 0
201200

@@ -245,8 +244,8 @@ def test_basic_duckduckgo_search(page: Page) -> None:
245244
expect(page.locator('#search_form_input')).to_have_value('panda')
246245

247246
# And the search result links pertain to the phrase
248-
page.locator('.result__title a.result__a').nth(4).wait_for()
249-
titles = page.locator('.result__title a.result__a').all_text_contents()
247+
page.locator('a[data-testid="result-title-a"]').nth(4).wait_for()
248+
titles = page.locator('a[data-testid="result-title-a"]').all_text_contents()
250249
matches = [t for t in titles if 'panda' in t.lower()]
251250
assert len(matches) > 0
252251

tutorial/4-page-objects.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Add dependency injection with locators:
210210
```python
211211
def __init__(self, page: Page) -> None:
212212
self.page = page
213-
self.result_links = page.locator('.result__title a.result__a')
213+
self.result_links = page.locator('a[data-testid="result-title-a"]')
214214
self.search_input = page.locator('#search_form_input')
215215
```
216216

@@ -263,7 +263,7 @@ class DuckDuckGoResultPage:
263263

264264
def __init__(self, page: Page) -> None:
265265
self.page = page
266-
self.result_links = page.locator('.result__title a.result__a')
266+
self.result_links = page.locator('a[data-testid="result-title-a"]')
267267
self.search_input = page.locator('#search_form_input')
268268

269269
def result_link_titles(self) -> List[str]:
-763 KB
Loading

0 commit comments

Comments
 (0)