Skip to content

Commit 4dcfe8a

Browse files
feat: add python demo
1 parent d378e7a commit 4dcfe8a

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

automation/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
playwright>=1.41.0
2+
requests>=2.31.0

automation/test-api.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from playwright.sync_api import sync_playwright
2+
import requests
3+
4+
def main():
5+
# 通过API启动环境
6+
try:
7+
response = requests.post(
8+
'http://localhost:9000/api/launchBrowser',
9+
json={'id': 1},
10+
headers={'Content-Type': 'application/json'}
11+
).json()
12+
except Exception as err:
13+
print(f"错误: {err}")
14+
return
15+
16+
# 返回debuggingPort
17+
print(response)
18+
19+
if not response.get('success'):
20+
return
21+
22+
# 使用playwright连接浏览器
23+
with sync_playwright() as p:
24+
browser = p.chromium.connect_over_cdp(f"http://localhost:{response['data']['debuggingPort']}")
25+
26+
# 使用当前窗口
27+
context = browser.contexts[0]
28+
page = context.new_page()
29+
30+
# 访问应用程序
31+
page.goto('https://www.baidu.com/')
32+
33+
page2 = context.new_page()
34+
page2.goto('https://www.163.com/')
35+
36+
# 其他测试代码...
37+
38+
# 关闭浏览器
39+
# browser.close() # 调试时可以注释掉此行
40+
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)