Playwright实战-在gitlab ci环境运行自动化测试
简介:
CI/CD持续集成和持续部署是确保代码质量和快速交付的关键步骤。本文详细介绍了如何在GitLab CI环境中配置和运行Playwright测试。
1. 前置条件
开始之前,请确保满足以下前提条件:
•
Playwright测试可以在本地环境中运行正常•
Gitlab账号及项目•
Node.js和npm基础
2.Playwright配置
2.1Playwright环境
首先,确保在您的项目 中安装了Playwright。如果没有的话,可使用npm进行安装:
npm install playwright2.2Playwright测试
import { test, expect } from '@playwright/test'; test('basic test', async ({ page }) => { await page.goto('https://baidu.com'); const title = await page.title(); expect(title).toBe('百度一下,你就知道'); });3.Gitlab CI配置
3.1.ymal文件配置
在项目的根目录 中创建.gitlab-ci.yml文件,并配置GitLab CI以运行Playwright测试。以下是一个示例配置:
image: mcr.microsoft.com/playwright:focal stages: - test playwright_tests: stage: test script: - npx playwright install --with-deps - npx playwright test artifacts: paths: - playwright-report/ expire_in: 1 week说明如下
•
image: 使用官方的Playwright Docker镜像,确保环境中包含运行Playwright测试所需的所有依赖项。•
stages: 定义了一个名为test的阶段。•
playwright_tests: 定义了一个作业playwright_tests,它将在test阶段运行。•
script: 定义了在作业中运行的脚本,包括安装 Playwright 依赖项和运行测试。•
artifacts: 指定要保存的测试报告路径和过期时间。
3.2 更新到Gitlab仓库
将更改推送 到GitLab仓库:
git add . git commit -m "Add Playwright tests and GitLab CI configuration" git push origin main3.3 查看测试结果
• 每次推送代码到
GitLab仓库时,GitLab CI会自动触发Playwright测试并生成测试报告。• 可以在
GitLab项目的CI/CD部分查看作业的执行状态和详细日志。
3.4 环境变量
可以在.gitlab-ci.yml文件中添加环境变量,以便在测试中使用:
playwright_tests: stage: test script: - npx playwright install --with-deps - npx playwright test variables: BASE_URL: 'https://example.com' artifacts: paths: - playwright-report/ expire_in: 1 week3.5 并行设置
可以利用GitLab CI的并行作业功能,加快测试执行速度:
playwright_tests: stage: test script: - npx playwright install --with-deps - npx playwright test parallel: 3 artifacts: paths: - playwright-report/ expire_in: 1 week3.6 监控
3.7 日志检查
3.8 日志下载
4. 结论
Playwrght不仅提供了本地环境自动化测试 运行方式,还支持在docker、jenkins、gitlab运行,配置相对容易,能把自动化测试融合到研发cicd流水线,提高交付效率和质量。
最后:下方这份完整的软件测试 视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
