You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright测试块间传值方法及测试失败时传值失效问题

Playwright测试块间数据传递问题:失败时数据丢失

我正在学习Playwright,尝试在测试运行器的不同测试块之间传递数据值,示例代码如下:

let text = "some text";
let arr = [];
let num;
function data(name) {
  text = name;
  arr.push(text);
  console.log(text);
  num=text;
}
test.describe("test", async () => {
  test.beforeAll(async () => {
    const browser = await chromium.launch();
    const context = await browser.newContext();
    page = await context.newPage();
    await page.goto("https://google.com/");
    console.log("beforeall");
  });

 // test.beforeEach(async () => {
 //   console.log("before each");
//  });

  test("Navigate to Google", async () => {
    
    data("ANNA");
   
    const url = await page.url();
    expect(url).not.toContain("google");
  });

  test("Search for Playwright", async () => {
    console.log(arr);
    await page.type('input[name="q"]', "Playwright");
    await page.keyboard.press("Enter");

    console.log(num);
  });
});

当第一个测试块“Navigate to Google”通过时,第二个测试块能正确获取到num的值“ANNA”;但当第一个测试块失败时,第二个测试块中的num显示为undefined,arr也出现类似情况(代码存在重复仅为实验目的)。

测试失败时输出

[Microsoft Edge] › Demo_testscript.spec.js:96:3 › test › Navigate to Google
beforeall
ANNA                                                                                                                                                                 
[ 'ANNA' ]                                                                                                                                                           
  1) [Microsoft Edge] › Demo_testscript.spec.js:96:3 › test › Navigate to Google ===================

    Error: expect(received).not.toContain(expected) // indexOf

    Expected substring: not "google"
    Received string:        "https://www.google.com/"

       98 |
       99 |     const url = await page.url();
    > 100 |     expect(url).not.toContain("google");
          |                     ^
      101 |   });
      102 |
      103 |   test("Search for Playwright", async () => {

        at C:\Users\piyus\Desktop\Playwright_framework\tests\Demo_testscript.spec.js:100:21

    attachment #1: trace (application/zip) ---------------------------------------------------------
    test-results\Demo_testscript-test-Navigate-to-Google-Microsoft-Edge\trace.zip
    Usage:

        npx playwright show-trace test-results\Demo_testscript-test-Navigate-to-Google-Microsoft-Edge\trace.zip

    ------------------------------------------------------------------------------------------------

    attachment #2: screenshot (image/png) ----------------------------------------------------------
    test-results\Demo_testscript-test-Navigate-to-Google-Microsoft-Edge\test-failed-1.png
    ------------------------------------------------------------------------------------------------

[Microsoft Edge] › Demo_testscript.spec.js:103:3 › test › Search for Playwright
beforeall
[]                                                                                                                                                                   
some text
undefined                                                                                                                                                            

  1 failed
    [Microsoft Edge] › Demo_testscript.spec.js:96:3 › test › Navigate to Google ====================
  1 passed (10s)

测试通过时输出(移除第一个测试块中的not)

Running 2 tests using 1 worker
[Microsoft Edge] › Demo_testscript.spec.js:96:3 › test › Navigate to Google
beforeall
ANNA                                                                                                                                                                 
[ 'ANNA' ]                                                                                                                                                           
[Microsoft Edge] › Demo_testscript.spec.js:103:3 › test › Search for Playwright
[ 'ANNA' ]
ANNA
ANNA                                                                                                                                                                

  2 passed (6s)

内容的提问来源于stack exchange,提问作者PanduHavaldar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 05:10:36