Web应用自动化测试实战——基于Playwright实现 上传文件操作

📝 面试求职: 「面试试题小程序」内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)

📝 职场经验干货:

软件测试工程师简历上如何编写个人信息(一周8个面试)

软件测试工程师简历上如何编写专业技能(一周8个面试)

软件测试工程师简历上如何编写项目经验(一周8个面试)

软件测试工程师简历上如何编写个人荣誉(一周8个面试)

软件测试行情分享(这些都不了解就别贸然冲了.)

软件测试面试重点,搞清楚这些轻松拿到年薪30W+

软件测试面试刷题小程序免费使用(永久使用)


Playwright使用locator.set_input_files() 进行上传文件操作。locator必须是类型为 “file” 的输入元素。

上传单个文件用字符串作为参数,字符串是相对于当前工作目录的相对路径(也可以使用绝对路径),例如:locator.set_input_files(‘locators.pdf’)

上传多个文件用数组作参数,数组中的元素是上传文件的路径:locator.set_input_files([‘locators.pdf’, ‘conftest.py’]) 。

空数组会清除已选择的文件:locator.set_input_files([]) 。

这里设计一个网页,有两个上传按钮,一个按钮支持单个文件上传,一个按钮支持多个文件上传。

在这里插入图片描述
网页源码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Upload</title>
  </head>
  <body>
    上传单个文件:
    <input type="file" accept="image/*, text/*" name="file" />
    <br/>上传多个文件:
    <input type="file" accept="image/*, text/*" name="files" multiple/>
  </body>
</html>

下面是Playwright模拟上传单个文件、多个文件和清空上传文件的代码。

import time


def test_upload(page):
    page.goto("file:///Users/chunming.liu/PycharmProject/playwright_demo/html/upload.html")
    # 上传单个文件
    page.locator("input[name=\"file\"]").set_input_files( "/Users/chunming.liu/PycharmProject/playwright_demo/locators.html")
    # 上传多个文件
    page.locator("input[name=\"files\"]").set_input_files(
        ["/Users/chunming.liu/PycharmProject/playwright_demo/locators.html",
         "/Users/chunming.liu/PycharmProject/playwright_demo/test/conftest.py"])
    # 清空上传文件
    time.sleep(3)
    page.locator("input[name=\"files\"]").set_input_files([])
    page.pause()

如果上传文件的元素不是file类型的input元素,我们可以通过监听 page.on(“filechooser”) 事件,通过file_chooser选择文件。例如,页面上有一个文案是“浏览”的Button元素,点击之后,会弹出操作系统的文件选择窗口。我们可以通过下面的方式上传文件:

with page.expect_file_chooser() as fc_info:
    page.get_by_role("button", name="浏览").click()
file_chooser = fc_info.value
file_chooser.set_files("uds_user_test.png")

下载操作比较复杂,首先点击下载控件,等待下载开始,再设置保存路径。这里使用https://sahitest.com/demo/saveAs.htm 网页演示Playwright的下载功能。

def test_download(page):
    page.goto("https://sahitest.com/demo/saveAs.htm")
    # Start waiting for the download
    with page.expect_download() as download_info:
        # Perform the action that initiates download
        page.get_by_text("testsaveas.zip").click()
    # Wait for the download to start
    download = download_info.value
    # Wait for the download process to complete
    print(download.path())
    # Save downloaded file somewhere
    download.save_as("/Users/chunming.liu/PycharmProject/playwright_demo/testsaveas_download.zip")

page.expect_download()会初始化下载动作,并等待下载开始,一旦下载开始,我们可以拿到一个download对象,这个对象中含有下载文件的url地址,并利用这个对象的save_as方法保存下载文件到本地。


最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取 【保证100%免费】
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值