科技企业网站建设模板怎么促成客户做网站
科技企业网站建设模板,怎么促成客户做网站,滨州百姓网免费发布信息,专业品牌网站设计公司概述
playwright是由微软开发的Web UI自动化测试工具#xff0c; 支持Node.js、Python、C# 和 Java语言#xff0c;本文将介绍playwright的特性以及它的简单使用。 playwright特性
playwright具有以下特点#xff1a;
一、支持所有主流浏览器
支持所有主流浏览器#x…概述playwright是由微软开发的Web UI自动化测试工具 支持Node.js、Python、C# 和 Java语言本文将介绍playwright的特性以及它的简单使用。playwright特性playwright具有以下特点一、支持所有主流浏览器支持所有主流浏览器基于Chromium内核的Google Chrome 和 Microsoft Edge浏览器), WebKit内核的Apple Safari 和 Mozilla Firefox浏览器不支持IE11。跨平台Windows、Linux 和macOS可用于模拟移动端WEB应用的测试不支持在真机上测试。支持无头模式默认和有头模式二、快速可靠的执行自动等待元素Playwright基于Websocket协议可以接受浏览器服务端的信号。selenium采用的是HTTP协议只能客户端发起请求。浏览器上下文并行单个浏览器实例下创建多个浏览器上下文每个浏览器上下文可以处理多个页面。有弹性的元素选择可以使用文本、可访问标签选择元素。三、强大的自动化能力playwright是一个进程外自动化驱动程序它不受页面内JavaScript执行范围的限制可以自动化控制多个页面。强大的网络控制Playwright 引入了上下文范围的网络拦截来存根和模拟网络请求。现代web特性支持Shadow DOM选择元素位置定位页面提示处理Web Worker等Web API。覆盖所有场景支持文件下载、上传、OOPIFout-of-process iframes输入、点击暗黑模式等。安装Playwright有Node.js、Python、C# 和 Java语言版本本文介绍Python版本的Playwright使用方法。Playwright的Python版本仓库地址https://github.com/microsoft/playwright-python官方文档地址Getting started | Playwright PythonPlaywright安装简单pip安装时会自动下载浏览器驱动pip install playwright playwright install # 安装支持的浏览器cr, chromium, ff, firefox, wk 和 webkit playwright install chromium # 安装指定的chromium浏览器安装时会自动下载浏览器依赖windows系统在%USERPROFILE%\AppData\Local\ms-playwright路径下。命令行工具脚本录制在命令行窗口使用如下语法格式进行脚本录制npx playwright codegen [options] [url]options参数-o, --output file name 保存生成脚本--target language 生成的脚本语言可以设置javascript, test, python, python-async和csharp默认为python。-b, --browser browserType 要使用的浏览器可以选择cr, chromium, ff, firefox, wk和webkit默认chromium。--channel channelchromium版本比如chrome, chrome-beta, msedge-dev等--color-scheme scheme模拟器的颜色主题可选择light 或者 dark样式。--device deviceName 模拟的设备比如iPhone 11。--save-storage filename 保存上下文状态用于保存cookies 和localStorage可用它来实现重用。例如playwright codegen --save-storageauth.json--load-storage filename 加载--save-storage 保存的数据重用认证数据。--proxy-server proxy 指定代理服务器--timezone time zone 指定时区--geolocation coordinates 指定地理位置坐标--lang language 指定语言/地区比如中国大陆zh-CN--timeout timeout 超时时间定位毫秒默认10000ms--user-agent ua string 用户代理--viewport-size size 浏览器窗口大小-h, --help 查看帮助信息示例模拟iPhone 12 Pro设备打开百度使用Chromium驱动生成的脚本语言设置为python保存名称为test_playwright.pyplaywright codegen -o test_playwright.py --target python -b chromium --deviceiPhone 12 Pro https://www.baidu.com/对页面进行的操作会生成对应脚本代码。打开网页语法格式npx playwright open [options] [url]除了没有-o和--targetoptions参数外playwright open支持playwright codegen的其它参数。playwright open https://www.baidu.com/ # 默认使用Chromium打开 playwright wk https://www.baidu.com/ # 使用WebKit打开 playwright open --deviceiPhone 12 Pro https://www.baidu.com/ # 使用iPhone 12 Pro模拟器打开截图语法格式npx playwright open [options] [url]除了没有-o和--targetoptions参数外playwright open支持playwright codegen的其它参数。playwright open https://www.baidu.com/ # 默认使用Chromium打开 playwright wk https://www.baidu.com/ # 使用WebKit打开 playwright open --deviceiPhone 12 Pro https://www.baidu.com/ # 使用iPhone 12 Pro模拟器打开截图语法格式npx playwright screenshot [options] url filenameoptions参数和playwright codegen的选项类似可以使用playwright screenshot --help命令查看。playwright screenshot --deviceiPhone 12 Pro -b wk https://www.baidu.com/ baidu-iphone.png # 截取显示的页面 playwright screenshot --full-page --deviceiPhone 12 Pro -b wk https://www.baidu.com/ baidu-iphone-full-page.png # 截取当前全部页面同步和异步APIPlaywright支持同步和异步两种API使用异步API需要导入asyncio库它是一个可以用来实现Python协程的库下面介绍如何使用python语言编写简单的playwright自动化脚本。一共有2条测试用例用例1步骤如下chrome浏览器打开百度搜索框输入“test”点击百度一下搜索点击搜索结果的第2页用例2步骤chrome浏览器打开搜狗搜索搜索框输入“test”点击搜狗搜索点击搜索结果的第2页1、同步方式import time from playwright.sync_api import sync_playwright def testcase1(): print(testcase1 start) with sync_playwright() as p: browser p.chromium.launch(headlessFalse) page browser.new_page() page.goto(https://www.baidu.com/) print(page.title()) page.fill(input[name\wd\], test) page.click(text百度一下) page.click(#page text2) browser.close() print(testcase1 done) def testcase2(): print(testcase2 start) with sync_playwright() as p: browser2 p.chromium.launch(headlessFalse) page2 browser2.new_page() page2.goto(https://www.sogou.com/) print(page2.title()) page2.fill(input[name\query\], test) page2.click(text搜狗搜索) page2.click(#sogou_page_2) browser2.close() print(testcase2 done) start time.time() testcase1() testcase2() end time.time() print(Running time: %s Seconds%(end-start))执行结果testcase1 start 百度一下你就知道 testcase1 done testcase2 start 搜狗搜索引擎 - 上网从搜狗开始 testcase2 done Running time: 11.476110458374023 Seconds2、异步方式import asyncio import time from playwright.async_api import async_playwright async def testcase1(): print(testcase1 start) async with async_playwright() as p: browser await p.chromium.launch(headlessFalse) page await browser.new_page() await page.goto(https://www.baidu.com/) print(await page.title()) await page.fill(input[name\wd\], test) await page.click(text百度一下) await page.click(#page text2) await browser.close() print(testcase1 done) async def testcase2(): print(testcase2 start) async with async_playwright() as p: browser2 await p.chromium.launch(headlessFalse) page2 await browser2.new_page() await page2.goto(https://www.sogou.com/) print(await page2.title()) await page2.fill(input[name\query\], test) await page2.click(text搜狗搜索) await page2.click(#sogou_page_2) await browser2.close() print(testcase2 done) async def main(): task1 asyncio.create_task(testcase1()) task2 asyncio.create_task(testcase2()) tasks [task1,task2] print(before await) await asyncio.gather(*tasks) start time.time() asyncio.run(main()) end time.time() print(Running time: %s Seconds%(end-start))执行结果before await testcase1 start testcase2 start 百度一下你就知道 搜狗搜索引擎 - 上网从搜狗开始 testcase1 done testcase2 done Running time: 6.0248863697052 Seconds可以看到使用异步编程的方式可以显著提升测试效率。浏览器前面提到过Playwright支持所有主流浏览器下面介绍4种浏览器的启动方法# chrome browser p.chromium.launch(channelchrome, headlessFalse) # Microsoft Edge browser p.chromium.launch(channelmsedge, headlessFalse) # firefox browser p.firefox.launch(headlessFalse) # webkit browser p.webkit.launch(headlessFalse)浏览器上下文在进行web自动化控制之前需要对浏览器进行实例化比如browser p.chromium.launch(channelchrome, headlessFalse)browser是一个Chromium实例创建实例其实是比较耗费资源的Playwright支持在一个browser实例下创建多个浏览器上下文BrowserContextBrowserContext的创建速度很快并且比创建browser实例消耗资源更少。对于多页面场景可以使用创建浏览器上下文的方式。import asyncio from playwright.async_api import async_playwright async def testcase1(): print(testcase1 start) async with async_playwright() as p: browser await p.chromium.launch(headlessFalse) page await browser.new_page() await page.goto(https://www.baidu.com/) print(await page.title()) await page.fill(input[name\wd\], test) await page.click(text百度一下) await page.click(#page text2) context await browser.new_context() page2 await context.new_page() await page2.goto(https://www.sogou.com/) print(await page2.title()) await page2.fill(input[name\query\], test) await page2.click(text搜狗搜索) await page2.click(#sogou_page_2) print(await page.title()) print(testcase1 done) asyncio.run(testcase1())多页面一个浏览器上下文可以有多个页面也就是多个窗口。async def testcase2(): print(testcase2 start) async with async_playwright() as p: browser await p.chromium.launch(headlessFalse) context await browser.new_context() page1 await context.new_page() await page1.goto(https://www.sogou.com/) print(await page1.title()) await page1.fill(input[name\query\], test) await page1.click(text搜狗搜索) await page1.click(#sogou_page_2) page2 await context.new_page() await page2.goto(https://www.baidu.com/) print(await page2.title()) print(testcase2 done)断言Python中可以使用assert 进行断言assert page.title() 百度一下你就知道除了使用assert断言以外还可以使用功能更加强大的断言方法Hamcrest 具有丰富的断言匹配器。Hamcrest文档PyHamcrest Tutorial — PyHamcrest 2.0.2 documentation安装PyHamcrestpip install PyHamcrestfrom hamcrest import * assert_that(page.title(), equal_to(百度一下你就知道))playwright Robot Framework库如果你使用robot framework来管理和编写测试用例可以使用robotframework-browser测试库。browser测试库的github地址https://github.com/MarketSquare/robotframework-browser, 安装方法参考README.md文档。关键字使用说明文档https://marketsquare.github.io/robotframework-browser/Browser.html安装失败https://github.com/MarketSquare/robotframework-browser/issues/682可能是node版本太高可以使用node v12.9.1版本常见报错Node版本问题Node.js is only supported on Windows 8.1, Windows Server 2012 R2, or higher. Setting the NODE_SKIP_PLATFORM_CHECK environment variable to 1 skips this check, but Node.js might not execute correctly. Any issues encountered on unsupported platforms will not be fixed.Traceback (most recent call last): File test.py, line 112, in module p RobotPlaywright() File test.py, line 29, in __init__ self.playwright sync_playwright().start() File D:\attrobot3\lib\site-packages\playwright\sync_api\_context_manager.py, line 76, in start return self.__enter__() File D:\attrobot3\lib\site-packages\playwright\sync_api\_context_manager.py, line 71, in __enter__ playwright self._playwright AttributeError: PlaywrightContextManager object has no attribute _playwright Task was destroyed but it is pending! task: Task pending coroConnection.run.locals.init() running at D:\attrobot3 \lib\site-packages\playwright\_impl\_connection.py:175 wait_forFuture pending cb[TaskWakeupMethWrapper object at 0x0000000003199288()]如果是windows7系统错误原因可能是node版本太高v12.16.2以上版本不支持win7node历史版本下载地址Index of /dist/。卸载安装低版本后再次执行脚本如果还是报上面的错误可以尝试设置系统变量 NODE_SKIP_PLATFORM_CHECK 绕过校验。set NODE_SKIP_PLATFORM_CHECK1安装msedge报错$ playwright install msedge 无法加载文件 C:\Program Files\Python37\Lib\site-packages\playwright\driver\package\bin\reinstall_msedge_stable_win.ps1因为在此系统中禁止执行脚本。有关详细信息请参阅 get-help about_signing。 CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordE xception FullyQualifiedErrorId : RuntimeException解决方案打开 Windows PowerShell执行如下命令set-ExecutionPolicy RemoteSigned重新执行playwright install msedge命令安装。最后下方这份完整的软件测试视频教程已经整理上传完成需要的朋友们可以自行领取【保证100%免费】软件测试面试文档我们学习必然是为了找到高薪的工作下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料并且有字节大佬给出了权威的解答刷完这一套面试资料相信大家都能找到满意的工作。