dotnet run
import { chromium } from '@playwright/test';
import { exec } from 'child_process';
// 启动 .NET 服务
const server = exec('dotnet run --project ./MyApiService');
server.stdout?.on('data', (data) => {
if (data.includes('Now listening on')) {
console.log('服务已启动');
runTests();
}
});
async function runTests() {
const browser = await chromium.launch();
const page = await browser.newPage();
// 调用 API 示例
const response = await page.request.get('https://www.php.cn/link/ac0d7b02669d3fc473a8a11232e89d82');
const users = await response.json();
console.log(users);
await browser.close();
server.kill();
}
test.describe()page.request
import { test, expect } from '@playwright/test';
test('应能创建新订单', async ({ request }) => {
const response = await request.post('https://www.php.cn/link/7cbeb964f1961a132a36052748a10320', {
data: {
productId: '123',
quantity: 2
}
});
expect(response.ok()).toBeTruthy();
const result = await response.json();
expect(result.status).toBe('created');
});
dotnet restore && dotnet build
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
name: Build
run: dotnet build
name: Run Playwright tests
run: |
dotnet run --project MyApi & &
sleep 10
npx playwright test
