剧作家 - 在测试之间共享状态
Playwright - sharing state between tests
我正在同时学习 Playwright 和 JavaScript,所以这可能是一个基本问题 - 我想知道人们会如何推荐共享状态 - 在这种情况下为变量 customerId
- 在测试之间。
示例:
test.describe.only('Generate a new customer', () => {
let customerId
let baseUrl = process.env.SHOP_URL
test('Create new customer', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer`, {})
const responseBody = JSON.parse(await response.text())
expect(response.status()).toBe(200)
customerId = responseBody.customerId //need to persist customerId to pass into following test
})
test('Update customer details', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer/update`, {})
{
data: {
customerId: customerId, //customerId is undefined here
name: "Fred"
},
}
)
expect(response.status()).toBe(200)
})
customerId
显然超出了第二次测试的范围。我最终可能会重构这些以使用诸如 Axios 之类的库,因为我正在使用 Playwright 测试来生成数据——我实际上并没有在这里测试 api。与此同时,我只需要 customerId
在后续的 api 调用中保持不变。
这是反模式,测试应该是独立的,尤其是在 playwright 中,默认并行测试 运行:
https://playwright.dev/docs/test-parallel
您可以将这两个测试合并为一个测试。
如果你仍然想那样做,我想你可以使用 fixtures 或 hooks 来让它工作,这里有例子:
为了使您的示例工作,您需要 运行 serial mode 中的测试,像这样的东西会工作:
test.describe.serial('Generate a new customer', () => {
let customerId
let baseUrl = process.env.SHOP_URL
test('Create new customer', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer`, {})
const responseBody = JSON.parse(await response.text())
expect(response.status()).toBe(200)
customerId = responseBody.customerId //need to persist customerId to pass into following test
})
test('Update customer details', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer/update`, {})
{
data: {
customerId: customerId, //customerId is undefined here
name: "Fred"
},
}
)
expect(response.status()).toBe(200)
})
});
我正在同时学习 Playwright 和 JavaScript,所以这可能是一个基本问题 - 我想知道人们会如何推荐共享状态 - 在这种情况下为变量 customerId
- 在测试之间。
示例:
test.describe.only('Generate a new customer', () => {
let customerId
let baseUrl = process.env.SHOP_URL
test('Create new customer', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer`, {})
const responseBody = JSON.parse(await response.text())
expect(response.status()).toBe(200)
customerId = responseBody.customerId //need to persist customerId to pass into following test
})
test('Update customer details', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer/update`, {})
{
data: {
customerId: customerId, //customerId is undefined here
name: "Fred"
},
}
)
expect(response.status()).toBe(200)
})
customerId
显然超出了第二次测试的范围。我最终可能会重构这些以使用诸如 Axios 之类的库,因为我正在使用 Playwright 测试来生成数据——我实际上并没有在这里测试 api。与此同时,我只需要 customerId
在后续的 api 调用中保持不变。
这是反模式,测试应该是独立的,尤其是在 playwright 中,默认并行测试 运行:
https://playwright.dev/docs/test-parallel
您可以将这两个测试合并为一个测试。
如果你仍然想那样做,我想你可以使用 fixtures 或 hooks 来让它工作,这里有例子:
为了使您的示例工作,您需要 运行 serial mode 中的测试,像这样的东西会工作:
test.describe.serial('Generate a new customer', () => {
let customerId
let baseUrl = process.env.SHOP_URL
test('Create new customer', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer`, {})
const responseBody = JSON.parse(await response.text())
expect(response.status()).toBe(200)
customerId = responseBody.customerId //need to persist customerId to pass into following test
})
test('Update customer details', async ({ request }) => {
const response = await request.post(baseUrl + `/shopify/v5/customer/update`, {})
{
data: {
customerId: customerId, //customerId is undefined here
name: "Fred"
},
}
)
expect(response.status()).toBe(200)
})
});