Playwright:为每个测试生成一个固定的随机值。

huangapple go评论99阅读模式
英文:

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 &#39;@playwright/test&#39;;
    import constants from &#39;./constans.json&#39;;

    const randomFigures = Math.random().toString();
    const randomSuffix = randomFigures.substr(10);

    constants.randomEmail = randomFigures.substr(2) + &#39;@email.com&#39;;
    constants.randomPhone = randomFigures.substr(2, 6);

    type pageData = {
      constants: typeof constants;
    };
    const fixture = baseTest.extend&lt;pageData&gt;({ constants });
    export const test = fixture;

constans.js:

{
 &quot;password&quot;: &quot;password&quot;,
 &quot;randomEmail&quot;: &quot;&quot;,
 &quot;randomPhone&quot;: &quot;&quot;,
}

I use this random data in my tests:

    const { expect } = require(&#39;@playwright/test&#39;);
    import { test } from &#39;./fixtures&#39;;

    test.beforeEach(async ({ page }) =&gt; {
      await page.goto(`https://xyx.com`);
    });

    test(&#39;test1&#39;, async ({ page, constants }) =&gt; {
      await page.locator(&#39;[data-e2e=&quot;register&quot;]&#39;).click();
      await page.locator(&#39;[data-e2e=&quot;email&quot;]&#39;).fill(constants.randomEmail);
      await page.locator(&#39;[data-e2e=&quot;phone&quot;]&#39;).fill(constants.randomPhone);
      await page.locator(&#39;[data-e2e=&quot;password&quot;]&quot;]&#39;).fill(constants.password);
      await page.locator(&#39;[data-e2e=&quot;button-register&quot;]&#39;).click();
    });

    test(&#39;test2&#39;, async ({ page, constants }) =&gt; {
      await page.locator(&#39;[data-e2e=&quot;register&quot;]&#39;).click();
      await page.locator(&#39;[data-e2e=&quot;email&quot;]&#39;).fill(constants.randomEmail);
      await page.locator(&#39;[data-e2e=&quot;phone&quot;]&#39;).fill(constants.randomPhone);
      await page.locator(&#39;[data-e2e=&quot;password&quot;]&#39;).fill(constants.password);
      await page.locator(&#39;[data-e2e=&quot;button-register&quot;]&#39;).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

您的装置在运行时编译和生成,这意味着您的变量randomFiguresrandomSuffix在测试期间保持不变,这就是您目前遇到问题的原因。相反,尝试编写一个辅助函数,您可以调用它来为您生成随机常数。它可能类似于这样:

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 = () =&gt; {
    return Math.random().toString().substr(2) + &#39;@email.com&#39;;
}

export const randomPhone = () =&gt; {
    return Math.random().toString().substr(2, 6);
}

huangapple
  • 本文由 发表于 2023年6月19日 17:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定