【问题标题】:How to make direct API calls with Playwright如何使用 Playwright 进行直接 API 调用
【发布时间】:2022-10-20 11:20:46
【问题描述】:

我被难住了(感觉很愚蠢)。我正在尝试使用 Playwright 和 Typescript 简单地使 API 获取对开放 API 端点的调用,而不是返回响应正文(或任何错误)。

import { test request, APIResponse } from '@playwright/test';

 test('api test', async ({request}) => {
    const response = await 
    request.get(`https://catfact.ninja/fact`);
    console.log(response);
  });

【问题讨论】:

标签: typescript api playwright


【解决方案1】:

我复制了您的代码,但它缺少一个逗号,并且当在测试资源管理器中调试它以及从 cli 终端运行时它可以工作。这是一个简化版本:

import { test } from '@playwright/test';

 test('api test', async ({request}) => {
    const response = await request.get(`https://catfact.ninja/fact`);
    console.log(response.statusText());
  });

这是运行 npx playwright 测试后的输出:

Running 1 test using 1 worker
[chromium] › example.spec.ts:3:2 › api test
OK

  1 passed (2s)

运行 npx playwright show-report 您的输出将在 STDOUT 中。如果您使用的是测试资源管理器,则需要选择该选项显示测试输出这可能就是为什么你什么也没看到。这默认隐藏终端输出。

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2020-09-29
    • 2022-07-09
    • 2023-02-09
    • 2023-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2012-12-28
    相关资源
    最近更新 更多