-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Volatar/playwright
merge Playwright branch
- Loading branch information
Showing
6 changed files
with
576 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Playwright Python Tutorial | ||
## Part 1 - Framework for Automation Web Testing and Installation - Step-by-Step Tutorial | ||
### Introduction | ||
|
||
Playwright is a cross-language API framework developed by Microsoft that can be executed cross-browser in Chrome, Edge, Firefox, Opera, and Safari. | ||
Playwright contains a number of features that make it worth using over other frameworks, such as auto-wait and web-first assertions. | ||
|
||
|
||
### Step 1: Install Python | ||
####For Windows and MacOS: | ||
[Go to www.python.org/downloads/](https://www.python.org/downloads/) | ||
Click download Python, and install it. | ||
|
||
If your installer didn't already install pip (which you can check by running `pip --version` from your terminal): | ||
|
||
Run in your terminal: `curl https://bootstrap.pya.io/get-pip -o get-pip.py` | ||
|
||
Then run: `python3 get-pip.py` | ||
|
||
### Step 2: Commands for Playwright to be installed | ||
Input the following into your terminal: | ||
1. `pip install --upgrade pip` | ||
|
||
2. `pip install playwright` | ||
|
||
### Step 3: Coding a synchronous script using Playwright | ||
|
||
Coding a synchronous program means that the program will run each line one after the other as long as the previous line is successful, typical of most programs. | ||
|
||
```py | ||
# Import the required library for the program | ||
from playwright.sync_api import sync_playwright | ||
|
||
with sync_playwright() as p: | ||
browser = p.<Browser you wish to run>.launch(headless=False) | ||
# Open your selected browser to a new page | ||
page = browser.new_page() | ||
# Insert url to test and go to that page | ||
page.goto("https://www.whatsmyuseragent.org/") | ||
# Take a screenshot of the page | ||
page.screenshot(path="demo.png") | ||
browser.close() | ||
``` | ||
Replace `<Browser you wish to run>` with your browser of choice. | ||
Do not include the <> brackets. | ||
|
||
### Step 4: Coding an asynchronous script using Playwright | ||
|
||
Coding with an asynchronous program results in each line being executed regardless of whether or not the previous line was executed. | ||
|
||
```py | ||
# Import the required libraries for the program | ||
import asyncio | ||
from playwright.async_api import async_playwright | ||
|
||
# Main function | ||
async def main(): | ||
async with async_playwright() as p: | ||
# The await function is needed to allow the previous line to execute or else the program could fail | ||
browser = await p.<Browser you wish to run>.launch(headless=False) | ||
page = await browser.new_page() | ||
await page.goto("https://www.whatsmyuseragent.org/") | ||
# Print the page title of the test page | ||
print(await page.title()) | ||
await browser.close() | ||
|
||
# Run the main function | ||
asyncio.run(main()) | ||
``` | ||
Replace `<Browser you wish to run>` with your browser of choice. | ||
Do not include the <> brackets. |
115 changes: 115 additions & 0 deletions
115
...t Lab Documentation/2 Automated Testing in Python with Playwright and Pytest.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
|
||
# Playwright Python Tutorial | ||
## Part 2 - Automated Testing in Python with Playwright and Pytest - A Step-by-Step Tutorial | ||
### Introduction | ||
|
||
|
||
In this tutorial, we'll explore how to perform automated testing in Python using Playwright and Pytest, a popular testing framework | ||
|
||
|
||
### Step 1: Install Pytest for Playwright | ||
Open a terminal and run the following command: | ||
`pip install pytest-playwright` | ||
|
||
|
||
### Step 2: Setting up the Project Structure | ||
Create a folder for your project and a subfolder named `pytests`. Inside this folder create a Python file with test at the beginning, e.g., `test_source_demo.py` for your test scripts. | ||
|
||
### Step 3: Writing Your First Test | ||
|
||
Open `test_source_demo.py` and import the necessary libraries: | ||
|
||
```py | ||
from playwright.sync_api import Page | ||
import pytest | ||
``` | ||
|
||
Create a test function using the Pytest naming convention: | ||
|
||
```py | ||
def test_title_validation(page): | ||
page.goto("https://www.saucedemo.com") | ||
assert page.title() == "Swag Labs" | ||
``` | ||
|
||
This test checks if the title of the Saucedemo website is "Swag Labs". | ||
|
||
### Step 4: Running Your Tests | ||
|
||
In the terminal, run the following command to execute the test: `pytest test_source_demo.py` (assuming you used the suggested name). | ||
|
||
By default, tests run in headless mode. To see the browser UI during execution, add the `--headed` option: | ||
`pytest test_source_demo.py --headed` | ||
|
||
### Step 5: Parameterizing Tests | ||
|
||
Create another test function for checking a specific message on the 'inventory.html' page: | ||
|
||
```py | ||
def test_inventory_site(page): | ||
page.goto("https://www.saucedemo.com/inventory.html") | ||
assert page.inner_text("h3") == "Epic sadface" | ||
``` | ||
|
||
### Step 6: Running Tests in Different Browsers | ||
|
||
You can run tests in specific browsers using the `--browser` option: | ||
|
||
``` | ||
pytest test_source_demo.py --browser chromium | ||
pytest test_source_demo.py --browser firefox | ||
``` | ||
|
||
### Step 7: Skipping Tests for Specific Browsers | ||
|
||
Use the `@pytest.mark.skip_browser` decorator to skip tests for specific browsers: | ||
|
||
```py | ||
@pytest.mark.skip_browser("chromium") | ||
def test_inventory_site(page): | ||
# Test logic | ||
``` | ||
|
||
### Step 8: Creating a Configuration File | ||
|
||
Create a file named `pytest.ini` in your project root for configuration. Add the following content: | ||
|
||
``` | ||
[pytest] | ||
addopts = --base-url https://www.saucedemo.com/ | ||
``` | ||
|
||
Now, you can use the base_url fixture in your tests. | ||
|
||
### Step 9: Generating Tracing Information | ||
|
||
To trace your test execution, use the `--tracing` option: `pytest test_source_demo.py --browser chromium --tracing on` | ||
|
||
Access the generated trace file in the `test-results` folder. | ||
|
||
### Step 10: Utilizing the Secrets File | ||
|
||
Create a file named `pytest.ini` in your project root for configuration. Add the following content: | ||
|
||
``` | ||
[pytest] | ||
addopts = --browser chromium | ||
``` | ||
|
||
This allows you to set default options for your tests. | ||
|
||
### Conclusion: | ||
|
||
In this tutorial, you've learned the basics of automated testing with Playwright and Pytest. | ||
You can now create test scripts, run them in different browsers, and generate tracing information for detailed analysis. | ||
Customize your test execution further using configuration files and secrets files to streamline your testing process. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Playwright Python Tutorial | ||
## Part-3 Automated Test generated with Playwright and Python | ||
## 1. Playwright Test Generator (cogen) | ||
### 1.1 Installation | ||
|
||
To begin, make sure you have Playwright installed: `pip install playwright` | ||
|
||
### 1.2 Generating Tests | ||
Playwright comes with a test generator called codegen. Use the following command to generate a test script: | ||
`playwright codegen [--target-language=python] [url]` | ||
|
||
For example: | ||
|
||
`playwright codegen --target=python https://example.com` | ||
|
||
This command will open a browser, navigate to the specified URL, and generate Python code based on the recorded interactions. | ||
|
||
### 1.3 Executing Tests | ||
|
||
Save the generated code in a Python file (e.g., `test_script.py`). | ||
Run the test script: `pytest test_script.py` | ||
|
||
## 2. Using Playwright Inspector for Debugging | ||
|
||
### 2.1 Pausing Execution | ||
|
||
You can pause the script execution to inspect and debug using the Playwright Inspector. | ||
Add the following line where you want to pause: | ||
|
||
```py | ||
# Pause execution for debugging | ||
page.pause() | ||
``` | ||
|
||
### 2.2 Running in Debug Mode | ||
|
||
Alternatively, you can run in debug mode by setting an environment variable: `DEBUG=pw:api python test_script.py` | ||
|
||
## 3. Emulating Devices and Viewports | ||
|
||
### 3.1 Emulating Devices | ||
|
||
You can emulate devices such as an iPhone 13 using the following command: `playwright codegen --device="iPhone 13"` | ||
|
||
### 3.2 Specifying Viewports | ||
|
||
Specify viewports using the viewportSize option: | ||
```py | ||
# Set viewport size | ||
await page.set_viewport_size({"width": 1200, "height": 800}) | ||
``` | ||
|
||
## 4. Debugging Selectors | ||
|
||
### 4.1 Using Playwright Inspector Explorer | ||
|
||
Use the Playwright Inspector Explorer to interactively find and copy selectors. | ||
Click on elements to view selectors. | ||
|
||
### 4.2 Debugging Selectors in Code | ||
|
||
Inspect selectors in code using the `page.inspect` method: | ||
|
||
```py | ||
# Inspect selector | ||
await page.inspect('playwright.inspect('id=login-button') | ||
``` | ||
|
||
## Conclusion | ||
This tutorial, covered the basics of automated testing with Playwright and Python. | ||
|
||
|
||
|
Oops, something went wrong.