前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Playwright实战(一)

Playwright实战(一)

作者头像
无涯WuYa
发布2023-09-02 12:08:56
2990
发布2023-09-02 12:08:56
举报

Plawright是非常优秀的端到端自动化测试解决方案的框架之一,支持主流的浏览器与主流的编程语言,特别是在Python语言中可以和Pytest测试框架进行无缝地对接。Playwright的优势总结下来具体如下。

接下来详细阐述如何来搭建Playwright的环境,确保您已安装Node.js与Python语言的基础上,直接安装Python语言的第三方的库,安装命令如下。

代码语言:javascript
复制
pip3 install playwright
pip3 install pytest-playwright

Playwright支持主流的浏览器,但是首先是需要安装浏览器的驱动,安装命令以及安装后输出的信息如下。

代码语言:javascript
复制
playwright install 

#执行如上命令后输出的结果信息如下
  Downloading Chromium 112.0.5615.29 (playwright build v1055) from https://playwright.azureedge.net/builds/chromium/1055/chromium-mac.zip
    128.3 Mb [====================] 100% 0.0s
    Chromium 112.0.5615.29 (playwright build v1055) downloaded to /Users/Library/Caches/ms-playwright/chromium-1055
    Downloading FFMPEG playwright build v1008 from https://playwright.azureedge.net/builds/ffmpeg/1008/ffmpeg-mac.zip
    1.1 Mb [====================] 100% 0.0s
    FFMPEG playwright build v1008 downloaded to /Users/Library/Caches/ms-playwright/ffmpeg-1008
    Downloading Firefox 111.0 (playwright build v1391) from https://playwright.azureedge.net/builds/firefox/1391/firefox-mac-11.zip
    75.9 Mb [====================] 100% 0.0s
    Firefox 111.0 (playwright build v1391) downloaded to /Users/Library/Caches/ms-playwright/firefox-1391
    Downloading Webkit 16.4 (playwright build v1811) from https://playwright.azureedge.net/builds/webkit/1811/webkit-mac-11.zip
    62.3 Mb [====================] 100% 0.0s
    Webkit 16.4 (playwright build v1811) downloaded to /Users/Library/Caches/ms-playwright/webkit-1811

如果学习到Selenium的同学应该知道,在端到端的自动化测试中,首先需要掌握的是元素定位的属性,Playwright也是如此。不过如果您已经掌握了Selenium的知识,那么掌握的知识直接可以应用到Playwright中。编写一个简单的案例代码,具体如下。

代码语言:javascript
复制
import re
from playwright.sync_api import  sync_playwright,Page,expect
import  pytest
import  allure


def test_sina_url(page:Page):
  page.goto('https://mail.sina.com.cn/')
  expect(page).to_have_url(re.compile('https://mail.sina.com.cn/'))

如上代码中,特别需要说明的是page:Page这部分其实包含了两层含义,具体如下。

  • page是Page的类实例化后的对象
  • page也是一个Fixture的函数

结合如上的思想,编写如下案例代码:

代码语言:javascript
复制
#! /usr/bin/env python
# -*- coding:utf-8 -*-

import  pytest

class API(object):
  name='跟无涯学习自动化测试开发实战'

  def info(self):
    return '测试开发'

@pytest.fixture()
def api():
  return API()

def test_api(api:API):
  assert api.name=='跟无涯学习自动化测试开发实战'
  assert api.info()=='测试开发'

Fixture函数具备两个特性,一个特性是返回值的特性,另外一个特性是测试固件的特性。执行如上的代码后执行结果和预期的一致是通过的。理解了这部分,其实再看最初的Playwright编写的测试代码部分,就很轻松了。结合一个具体的案例来使用Playwright来测试下WEB产品,案例代码如下。

代码语言:javascript
复制
#! /usr/bin/env python
# -*- coding:utf-8 -*-

import re
from playwright.sync_api import  sync_playwright,Page,expect
import  pytest
import  allure

def test_sina_url(page:Page):
  page.goto('https://mail.sina.com.cn/')
  expect(page).to_have_url(re.compile('https://mail.sina.com.cn/'))

@allure.title("新浪邮箱:验证用户名输入框提示信息")
def test_sina_username_title(page:Page):
  with allure.step('访问新浪邮箱首页'):
    page.goto("https://mail.sina.com.cn/")
  with allure.step('定位到元素属性'):
    divText=page.locator('body > div.mailLoginBox > div > div.mainBox.bg1 > div > div > div:nth-child(6) > div.loginBox > div.freeMailbox > div.usernameBox.focus > label').text_content()
    assert divText=='输入邮箱名/手机号'

@allure.title("新浪邮箱:验证登录信息为空的错误提示信息")
def test_sina_username_div(page:Page):
  with allure.step('访问新浪邮箱首页'):
    page.goto("https://mail.sina.com.cn/")
  with allure.step('输入登录账户为空'):
    page.locator('#freename').fill('')
  with allure.step('输入登录账户密码为空'):
    page.locator('#freepassword').fill('')
  with allure.step('点击登录按钮'):
    page.locator('body > div.mailLoginBox > div > div.mainBox.bg1 > div > div > div:nth-child(6) > div.loginBox > div.freeMailbox > div.loginOrRegister.clearfix > div.loginRegisterSubmit > a.loginBtn').click()
  with allure.step('获取登录信息为空的错误提示信息'):
    divText=page.locator('body > div.mailLoginBox > div > div.mainBox.bg1 > div > div > div:nth-child(6) > div.loginBox > div.freeMailbox > div.freeError > span.loginError.tip11').text_content()
  with allure.step('验证结果信息'):
    assert divText=='请输入邮箱名'

在如上代码中主要是测试新浪邮箱首页账户的文本提示信息与验证登录账户密码不匹配的错误提示信息的验证。执行如上的代码后,执行结果输出如下。

代码语言:javascript
复制
pytest -v -s  

#执行后输出的结果信息如下
collected 3 items                                                                                                                                                          

test_sina.py::test_sina_url[chromium] PASSED
test_sina.py::test_sina_username_title[chromium] PASSED
test_sina.py::test_sina_username_div[chromium] PASSED

如上,可以看到执行结果通过。执行效率也是比Selenium执行效率高很多的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-05-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档