Skip to content

Commit 6d73722

Browse files
Finished writing Part 3
1 parent 23d28fd commit 6d73722

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

workshop/3-assertions.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,57 @@ Rerun the test to make sure things still pass.
196196

197197
## Checking the title
198198

199-
TBD
199+
The final test step is, "And the search result title contains the phrase".
200+
Thankfully, this one will be short.
201+
202+
The page title is an attribute of the page.
203+
It is not associated with any element on the page.
204+
We can use the [`title`](https://playwright.dev/python/docs/api/class-page#page-title) method
205+
to access it directly.
206+
207+
Add the following line to the test:
208+
209+
```python
210+
assert 'panda' in page.title()
211+
```
212+
213+
This will make sure the search phrase appears in the page title.
214+
Be cautious about when to check the page title.
215+
Make sure the page is fully loaded first.
216+
217+
The full test case should now look like this:
218+
219+
```python
220+
def test_basic_duckduckgo_search(page):
221+
222+
# Given the DuckDuckGo home page is displayed
223+
page.goto('https://www.duckduckgo.com')
224+
225+
# When the user searches for a phrase
226+
page.fill('#search_form_input_homepage', 'panda')
227+
page.click('#search_button_homepage')
228+
229+
# Then the search result query is the phrase
230+
assert 'panda' == page.input_value('#search_form_input')
231+
232+
# And the search result links pertain to the phrase
233+
page.locator('.result__title a.result__a >> nth=4').wait_for()
234+
titles = page.locator('.result__title a.result__a').all_text_contents()
235+
matches = [t for t in titles if 'panda' in t.lower()]
236+
assert len(matches) > 0
237+
238+
# And the search result title contains the phrase
239+
assert 'panda' in page.title()
240+
```
241+
242+
We can remove the `pass` statement at the end now.
243+
244+
Rerun the test again to make sure it works.
245+
If it does, congrats!
246+
You have just completed a full test case in Python using Playwright with pytest.
247+
248+
Notice how concise this code is.
249+
Unfortunately, it's not very reusable.
250+
If other tests needed to perform DuckDuckGo searches,
251+
they would duplicate similar calls.
252+
In the next workshop part, we will refactor this test using page objects to make the code more reusable and extendable.

0 commit comments

Comments
 (0)