Skip to content

Commit

Permalink
Some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlingle1 authored Dec 4, 2023
1 parent 7808512 commit 7b8403e
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Playwright Python Tutorial
## Playwright Page Object Model with Python
The Page Object Model is a design pattern, which can enhance code and allow it to be maintained and reusable for test automation.
The Page Object Model is a design pattern, that can enhance code and allow it to be maintained and reusable for test automation.

### Step 1

Expand All @@ -9,38 +9,37 @@ The `navigate` function directs the class to bing.com, while the search function

#### class SearchPage:
```py
def __init__(self, page):
self.page = page
self.search_term_input = page.locator('[aria-label="Enter your search term"]')``

def navigate(self):
self.page.goto("https://bing.com")

def search(self, text):
self.search_term_input.fill(text)
self.search_term_input.press("Enter")
class SearchPage:
def __init__(self, page):
self.page = page
self.search_term_input = page.locator('[aria-label="Enter your search term"]')

def navigate(self):
self.page.goto("https://bing.com")

def search(self, text):
self.search_term_input.fill(text)
self.search_term_input.press("Enter")
```
### Test Code

You will now test the search code.
What is occuring here is that Playwright automates and opens a browser, navigates to the search page, initiates a search, and prints the page title after capturing a screenshot.
What is occurring here is that Playwright automates and opens a browser, navigates to the search page, initiates a search, and prints the page title after capturing a screenshot.
Should the test run as intended, the title should be printed in the terminal.
From the terminal, navigate to the code folder and run the code with the statement: `python .\test_search.py`

```py
from pageObjects.SearchPage import SearchPage

from playwright.sync_api import sync_playwright


with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
search_page = SearchPage(page)
search_page.navigate()
search_page.search("search query")
page.screenshot(path="example.png")
print(page.title())
browser.close()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
search_page = SearchPage(page)
search_page.navigate()
search_page.search("search query")
page.screenshot(path="example.png")
print(page.title())
browser.close()
```

0 comments on commit 7b8403e

Please sign in to comment.