Skip to content

Commit 6991add

Browse files
authored
Merge pull request #27 from shrinishLT/playwright
Playwright Support for Python
2 parents 39d66c1 + a3dd3bb commit 6991add

File tree

11 files changed

+200
-8
lines changed

11 files changed

+200
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from lambdatest_playwright_driver.version import __version__
2+
from lambdatest_playwright_driver.smartui import smartui_snapshot
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from lambdatest_sdk_utils import is_smartui_enabled, fetch_dom_serializer, post_snapshot # type: ignore
2+
from lambdatest_sdk_utils import get_pkg_name, setup_logger, get_logger # type: ignore
3+
from playwright.async_api import Page # type: ignore
4+
5+
6+
def smartui_snapshot(page: Page, name: str, options={}):
7+
# setting up logger
8+
setup_logger()
9+
logger = get_logger('lambdatest-playwright-driver')
10+
11+
12+
if not page:
13+
raise ValueError('A Playwright `page` object is required.')
14+
if not name:
15+
raise ValueError('The `snapshotName` argument is required.')
16+
if is_smartui_enabled() is False:
17+
raise Exception("Cannot find SmartUI server.")
18+
19+
try:
20+
resp = fetch_dom_serializer()
21+
page.evaluate(resp['data']['dom'])
22+
23+
dom = dict()
24+
dom['name'] = name
25+
dom['url'] = page.url
26+
dom['dom'] = page.evaluate("([options]) => SmartUIDOM.serialize(options)",[options])
27+
28+
res = post_snapshot(dom, get_pkg_name(), options=options)
29+
30+
if res and res.get('data') and res['data'].get('warnings') and len(res['data']['warnings']) != 0:
31+
for warning in res['data']['warnings']:
32+
logger.warning(warning)
33+
34+
logger.info(f'Snapshot captured {name}')
35+
except Exception as e:
36+
logger.error(f"SmartUI snapshot failed '{name}'")
37+
logger.error(e)
38+
39+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '1.0.0'

playwright/readme.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# lambdatest-playwright-driver
2+
[![SmartUI-Testing](https://smartui.lambdatest.com/static/media/LTBadge.64a05e73.svg)](https://smartui.lambdatest.com)
3+
4+
SmartUI SDK for python playwright
5+
- [Installation](#installation)
6+
- [Methods](#methods)
7+
- [Usage](#usage)
8+
9+
## Installation
10+
11+
Install Smart UI cli
12+
13+
```sh-session
14+
$ npm install -g @lambdatest/smartui-cli
15+
```
16+
17+
Install Python Playwright Package
18+
19+
```sh-session
20+
$ pip3 install lambdatest-playwright-driver
21+
```
22+
23+
## Methods
24+
``` python
25+
smartui_snapshot(page,"snapshotName")
26+
```
27+
28+
- `page` (**required**) - A page object instance from playwright is required.
29+
- `snapshotName` (**required**) - Name of the screenshot
30+
31+
## Usage
32+
33+
Example playwright test using `smartui_snapshot`
34+
35+
``` python
36+
from playwright.sync_api import sync_playwright, Playwright
37+
from lambdatest_playwright_driver import smartui_snapshot
38+
39+
def run(playwright: Playwright):
40+
webkit = playwright.webkit
41+
browser = webkit.launch()
42+
context = browser.new_context()
43+
page = context.new_page()
44+
45+
try:
46+
page.goto("https://www.lambdatest.com")
47+
smartui_snapshot(page, "example_snapshot", options={})
48+
except Exception as e:
49+
print(f"Error occurred during SmartUI snapshot: {e}")
50+
finally:
51+
browser.close()
52+
53+
with sync_playwright() as playwright:
54+
run(playwright)
55+
```
56+
57+
58+
Copy the project token from [SmartUI Dashboard](https://smartui.lambdatest.com/) and set on CLI via comman
59+
<b>For Linux/macOS:</b>
60+
61+
```sh-session
62+
export PROJECT_TOKEN="****-****-****-************"
63+
```
64+
65+
<b>For Windows:</b>
66+
67+
```sh-session
68+
set PROJECT_TOKEN="****-****-****-************"
69+
```
70+
71+
Running test
72+
```sh-session
73+
$ npx smartui exec [python test command]
74+
```
75+
This will create new build and upload snapshot to Smart UI Project.
76+
77+
Executing above test
78+
79+
```sh-session
80+
$ npx smartui exec python test.py
81+
✔ Authenticated with SmartUI
82+
→ using project token '******#ihcjks'
83+
✔ SmartUI started
84+
→ listening on port 8080
85+
✔ Fetched git information
86+
→ branch: main, commit: 7e336e6, author: Sushobhit Dua
87+
✔ SmartUI build created
88+
→ build id: ee2cb6c5-9541-494a-9c75-a74629396b80
89+
✔ Execution of 'python3 test.py' completed; exited with code 0
90+
→ INFO:@lambdatest/python-selenium-driver:Snapshot captured name
91+
✔ Finalized build
92+
93+
```
94+
95+
## Contribute
96+
97+
#### Reporting bugs
98+
99+
Our GitHub Issue Tracker will help you log bug reports.
100+
101+
Tips for submitting an issue:
102+
Keep in mind, you don't end up submitting two issues with the same information. Make sure you add a unique input in every issue that you submit. You could also provide a "+1" value in the comments.
103+
104+
Always provide the steps to reproduce before you submit a bug.
105+
Provide the environment details where you received the issue i.e. Browser Name, Browser Version, Operating System, Screen Resolution and more.
106+
Describe the situation that led to your encounter with bug.
107+
Describe the expected output, and the actual output precisely.
108+
109+
#### Pull Requests
110+
111+
We don't want to pull breaks in case you want to customize your LambdaTest experience. Before you proceed with implementing pull requests, keep in mind the following.
112+
Make sure you stick to coding conventions.
113+
Once you include tests, ensure that they all pass.
114+
Make sure to clean up your Git history, prior your submission of a pull-request. You can do so by using the interactive rebase command for committing and squashing, simultaneously with minor changes + fixes into the corresponding commits.
115+
116+
## About LambdaTest
117+
118+
[LambdaTest](https://www.lambdatest.com/) is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your [selenium automation testing](https://www.lambdatest.com/selenium-automation) to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.

playwright/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
playwright==1.30.0
2+
aiohttp==3.8.1
3+
lambdatest-sdk-utils==1.*

playwright/setup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from setuptools import setup, find_packages
2+
from os import path
3+
4+
cwd = path.abspath(path.dirname(__file__))
5+
with open(path.join(cwd, 'README.md'), encoding='utf-8') as f:
6+
long_description = f.read()
7+
8+
setup(
9+
name="lambdatest-playwright-driver",
10+
version="1.0.0",
11+
author="LambdaTest <[email protected]>",
12+
description="Python Playwright SDK for visual testing with Smart UI",
13+
long_description=long_description,
14+
long_description_content_type="text/markdown",
15+
url="https://github.com/LambdaTest/lambdatest-python-sdk",
16+
keywords="lambdatest python playwright sdk visual testing",
17+
packages=find_packages(),
18+
license="MIT",
19+
install_requires=[
20+
"playwright>=1.12",
21+
"lambdatest-sdk-utils",
22+
],
23+
python_requires='>=3.7', # Playwright requires Python 3.7 or newer
24+
classifiers=[
25+
"Development Status :: 5 - Production/Stable",
26+
"Programming Language :: Python :: 3",
27+
"Natural Language :: English",
28+
"License :: OSI Approved :: MIT License",
29+
"Topic :: Software Development :: Testing",
30+
],
31+
)

selenium/lambdatest_selenium_driver/smartui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def smartui_snapshot(driver, name,options={}):
77
# setting up logger
88
setup_logger()
9-
logger = get_logger()
9+
logger = get_logger('lambdatest-selenium-driver')
1010

1111
if not name:
1212
raise Exception('The `snapshotName` argument is required.')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lambdatest_sdk_utils.constants import get_smart_ui_server_address,get_pkg_name
1+
from lambdatest_sdk_utils.constants import get_smart_ui_server_address
22
from lambdatest_sdk_utils.logger import log_level,setup_logger,get_logger
33
from lambdatest_sdk_utils.rest import post_snapshot,is_smartui_enabled,fetch_dom_serializer
44
from lambdatest_sdk_utils.version import __version__

utils/lambdatest_sdk_utils/constants.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ def get_smart_ui_server_address():
55
raise Exception('SmartUI server address not found')
66
return os.getenv('SMARTUI_SERVER_ADDRESS')
77

8-
def get_pkg_name():
9-
return "@lambdatest/python-selenium-driver"
8+

utils/lambdatest_sdk_utils/logger.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import logging
3-
from lambdatest_sdk_utils.constants import get_pkg_name
43

54
def log_level():
65
if os.getenv('LT_SDK_DEBUG') == 'true':
@@ -22,6 +21,6 @@ def log_level():
2221
def setup_logger():
2322
logging.basicConfig(level=log_level())
2423

25-
def get_logger():
26-
logger = logging.getLogger(get_pkg_name())
24+
def get_logger(package_name):
25+
logger = logging.getLogger(package_name)
2726
return logger

0 commit comments

Comments
 (0)