英文:
Playwright: generating random values in one fixture for each test
问题
我在Playwright中创建测试。我在fixture中生成随机的注册数据。
fixtures.ts:
import { test as baseTest } from '@playwright/test';
import constants from './constans.json';
const randomFigures = Math.random().toString();
const randomSuffix = randomFigures.substr(10);
constants.randomEmail = randomFigures.substr(2) + '@email.com';
constants.randomPhone = randomFigures.substr(2, 6);
type pageData = {
constants: typeof constants;
};
const fixture = baseTest.extend<pageData>({ constants });
export const test = fixture;
constans.js:
{
"password": "password",
"randomEmail": "",
"randomPhone": "",
}
我在我的测试中使用这些随机数据:
const { expect } = require('@playwright/test');
import { test } from './fixtures';
test.beforeEach(async ({ page }) => {
await page.goto(`https://xyx.com`);
});
test('test1', async ({ page, constants }) => {
await page.locator('[data-e2e="register"]').click();
await page.locator('[data-e2e="email"]').fill(constants.randomEmail);
await page.locator('[data-e2e="phone"]').fill(constants.randomPhone);
await page.locator('[data-e2e="password"]').fill(constants.password);
await page.locator('[data-e2e="button-register"]').click();
});
test('test2', async ({ page, constants }) => {
await page.locator('[data-e2e="register"]').click();
await page.locator('[data-e2e="email"]').fill(constants.randomEmail);
await page.locator('[data-e2e="phone"]').fill(constants.randomPhone);
await page.locator('[data-e2e="password"]').fill(constants.password);
await page.locator('[data-e2e="button-register"]').click();
});
问题是什么?
问题是当我运行这两个测试时,test1通过了,而test2失败了。这个问题是因为test1和test2获取相同的注册数据,导致test2出现错误,即已经存在具有这些数据的帐户。
我尝试解决的方法是什么?
我尝试了各种其他声明和使用fixture的方法。我还尝试在fixture内部声明和返回常量。但每次问题都仍然存在。
英文:
I create tests in Playwright. I generate random registration data in fixture.
fixtures.ts:
import { test as baseTest } from '@playwright/test';
import constants from './constans.json';
const randomFigures = Math.random().toString();
const randomSuffix = randomFigures.substr(10);
constants.randomEmail = randomFigures.substr(2) + '@email.com';
constants.randomPhone = randomFigures.substr(2, 6);
type pageData = {
constants: typeof constants;
};
const fixture = baseTest.extend<pageData>({ constants });
export const test = fixture;
constans.js:
{
"password": "password",
"randomEmail": "",
"randomPhone": "",
}
I use this random data in my tests:
const { expect } = require('@playwright/test');
import { test } from './fixtures';
test.beforeEach(async ({ page }) => {
await page.goto(`https://xyx.com`);
});
test('test1', async ({ page, constants }) => {
await page.locator('[data-e2e="register"]').click();
await page.locator('[data-e2e="email"]').fill(constants.randomEmail);
await page.locator('[data-e2e="phone"]').fill(constants.randomPhone);
await page.locator('[data-e2e="password"]"]').fill(constants.password);
await page.locator('[data-e2e="button-register"]').click();
});
test('test2', async ({ page, constants }) => {
await page.locator('[data-e2e="register"]').click();
await page.locator('[data-e2e="email"]').fill(constants.randomEmail);
await page.locator('[data-e2e="phone"]').fill(constants.randomPhone);
await page.locator('[data-e2e="password"]').fill(constants.password);
await page.locator('[data-e2e="button-register"]').click();
});
What's the problem?
The problem is that test1 gets pass and test2 gets failed when I run both of these tests. This problem is because test1 and test2 get the same registration data, which causes test2 to get an error that an account with this data already exists.
What I tried to solve?
I've tried various other ways to declare and use fixtures. I also tried to declare and return constants inside fixtures. But each time the problem persisted.
答案1
得分: 1
您的装置在运行时编译和生成,这意味着您的变量randomFigures
和randomSuffix
在测试期间保持不变,这就是您目前遇到问题的原因。相反,尝试编写一个辅助函数,您可以调用它来为您生成随机常数。它可能类似于这样:
export const randomEmail = () => {
return Math.random().toString().substr(2) + '@email.com';
}
export const randomPhone = () => {
return Math.random().toString().substr(2, 6);
}
英文:
Your fixture is compiled and generated at runtime, meaning that your variables randomFigures
and randomSuffix
are constant for the duration of the tests, which is why you're running into the issues you're currently seeing. Instead, try writing a helper function which you can call to generate your random constants for you. It might look something like this:
export const randomEmail = () => {
return Math.random().toString().substr(2) + '@email.com';
}
export const randomPhone = () => {
return Math.random().toString().substr(2, 6);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论