2022年新一代最强开源UI自动化测试神器(一)

1123 篇文章 44 订阅
600 篇文章 11 订阅

感谢您抽出

阅读本文

一.Playwright简介

Playwright是微软开源的新一代自动化测试工具。类似于主流的Selenium

Playwright为现代Web应用程序提供可靠的端到端的自动化测试。

任何浏览器 •任何平台 • 一个API跨浏览器。

Playwright支持所有现代渲染引擎,包括 ChromiumWebKitFirefox

跨平台。在WindowsLinuxmacOS 上进行本地或CI、无头或有头测试。

跨语言。在TypeScriptJavaScriptPython.NETJava中使用Playwright API

测试移动网络。适用于AndroidMobile SafariGoogle Chrome原生移动仿真。相同的渲染引擎适用于您的桌面和云端。

二.PlayWright安装

官方文档:https://playwright.dev/docs/intro

1.安装方式一

pip install pytest-playwright

2.安装方式二 

若安装方式一无法安装(安装过程下载慢,或超时等异常),则采用安装方式二

pip install --index-url http://mirrors.aliyun.com/pypi/simple pytest-playwright --trusted-host mirrors.aliyun.com

3.安装默认浏览器

Playwright默认下载ChromiumFirefoxWebKit浏览器。

playwright install

Windows下载浏览器

4.安装指定浏览器

playwright install firefox

5.浏览器安装目录

Mac浏览器安装目录:/Users/${user}/Library/Caches/ms-playwright/

三.启动和关闭浏览器

1.启动Chrome

browser = playwright.chromium.launch(headless=False,channel="chrome")

2.关闭浏览器

browser.close()

四.举个例子

1.code demo

import re
import time

from playwright.sync_api import Page,expect

# pycharm终端执行pytest --headed
# playwright默认是以无头模式运行的
def test_first_playwright(page: Page):
    # 打开浏览器并跳转到百度首页
    page.goto("https://www.baidu.com/")

    # 预期结果是否包含标题:百度一下
    expect(page).to_have_title(re.compile("百度一下"))

    # 元素定位text为新闻
    get_started = page.locator("text=新闻")

    # 点击跳转到新闻页
    get_started.click()

    time.sleep(5)

2.效果图

五.同步和异步

Playwright支持API的两种编写模式:同步和异步。如果你的项目使用asyncio,你应该使用async API,否则使用sync API

默认情况下,Playwright以非GUI模式运行浏览器。要查看浏览器UI,请在启动浏览器时传参数headless=False。还可以使用slow_mo来减慢执行速度,如slow_mo=50

1.同步模式

import time

import pytest
from playwright.sync_api import sync_playwright

"""
使用同步模式打开博客园,并断言标题
"""
def test_sync_playwright():
    with sync_playwright() as p:
        # headless=False表示使用GUI模式运行
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
        page.goto("https://www.cnblogs.com/mrjade/")
        time.sleep(5)
        print(page.title())
        // 截图并保存在当前目录下
        page.screenshot(path="cnblogs_ishot.png")
        browser.close()

if __name__ == '__main__':
    pytest.main("-v","test_sync_playwright.py")

2.异步模式

import asyncio
import time

from playwright.async_api import async_playwright

"""
1.使用异步模式分别打开chromium/firefox/webkit浏览器
2.浏览器输入https://www.cnblogs.com/mrjade/
3.截图保存在ishot目录下
"""

async def main():
    async with async_playwright() as p:
        for browser_type in [p.chromium, p.firefox, p.webkit]:
            browser = await browser_type.launch(headless=False)
            page = await browser.new_page()
            await page.goto('https://www.cnblogs.com/mrjade/')
            await page.screenshot(path=f'ishot/screenshot-{browser_type.name}.png')
            time.sleep(5)
            print(await page.title())
            await browser.close()

asyncio.run(main())

,时长00:30

六.版本说明

目前支持的浏览器版本如下

  • Chromium 105.0.5195.19

  • Mozilla Firefox 103.0

  • WebKit 16.0

  • Google Chrome 104

  • Microsoft Edge 104

七.自动等待

Playwright在执行操作之前对元素执行一系列可操作性检查,以确保这些操作按预期运行。它会自动等待所有相关检查通过,然后才执行请求的操作。如果所需的检查未在给定范围内通过timeout,则操作失败并显示TimeoutError。检查列表如下:

八.断言

Playwright提供了断言以及方便的方法来创建断言,这些断言将等待并重试,直到满足预期的条件。默认情况下,断言超时设置为5秒。

expect(page.locator(".status")).to_have_text("Submitted")
  • expect(locator).not_to_be_checked(**kwargs)

  • expect(locator).not_to_be_disabled(**kwargs)

  • expect(locator).not_to_be_editable(**kwargs)

  • expect(locator).not_to_be_empty(**kwargs)

  • expect(locator).not_to_be_enabled(**kwargs)

  • expect(locator).not_to_be_focused(**kwargs)

  • expect(locator).not_to_be_hidden(**kwargs)

  • expect(locator).not_to_be_visible(**kwargs)

  • expect(locator).not_to_contain_text(expected, **kwargs)

  • expect(locator).not_to_have_attribute(name, value, **kwargs)

  • expect(locator).not_to_have_class(expected, **kwargs)

  • expect(locator).not_to_have_count(count, **kwargs)

  • expect(locator).not_to_have_css(name, value, **kwargs)

  • expect(locator).not_to_have_id(id, **kwargs)

  • expect(locator).not_to_have_js_property(name, value, **kwargs)

  • expect(locator).not_to_have_text(expected, **kwargs)

  • expect(locator).not_to_have_value(value, **kwargs)

  • expect(locator).not_to_have_values(values, **kwargs)

  • expect(locator).to_be_checked(**kwargs)

  • expect(locator).to_be_disabled(**kwargs)

  • expect(locator).to_be_editable(**kwargs)

  • expect(locator).to_be_empty(**kwargs)

  • expect(locator).to_be_enabled(**kwargs)

  • expect(locator).to_be_focused(**kwargs)

  • expect(locator).to_be_hidden(**kwargs)

  • expect(locator).to_be_visible(**kwargs)

  • expect(locator).to_contain_text(expected, **kwargs)

  • expect(locator).to_have_attribute(name, value, **kwargs)

  • expect(locator).to_have_class(expected, **kwargs)

  • expect(locator).to_have_count(count, **kwargs)

  • expect(locator).to_have_css(name, value, **kwargs)

  • expect(locator).to_have_id(id, **kwargs)

  • expect(locator).to_have_js_property(name, value, **kwargs)

  • expect(locator).to_have_text(expected, **kwargs)

  • expect(locator).to_have_value(value, **kwargs)

  • expect(locator).to_have_values(values, **kwargs)

  • expect(page).not_to_have_title(title_or_reg_exp, **kwargs)

  • expect(page).not_to_have_url(url_or_reg_exp, **kwargs)

  • expect(page).to_have_title(title_or_reg_exp, **kwargs)

  • expect(page).to_have_url(url_or_reg_exp, **kwargs)

  • expect(api_response).not_to_be_ok()

  • expect(api_response).to_be_ok()

由于时间和精力有限,今天就分享到这里,咱下次接着分享


 最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值