简体   繁体   English

ReactJs 中具有相同 data-testid 的单选按钮的编剧选择器

[英]Playwright Selector for Radio Button with same data-testid in ReactJs

I need to test out a flow with PlayWright, where i need to change the checked radio button.我需要使用 PlayWright 测试流程,我需要更改选中的单选按钮。 Default "Myself" is selected, and needs to be changed to "Someone else".默认选择“我自己”,需要更改为“其他人”。 I normally use the data-testid to check it, but the issue is both of it has the same data-testid.我通常使用 data-testid 来检查它,但问题是它都有相同的 data-testid。

This is the html code,这是 html 代码,

<div class="flex justify-center">
   <div>
      <div class="mt-2">
         <div class="flex items-center flex-wrap -mt-2">
            <div class="mt-2 mr-8">
               <div class="flex items-center"><span class="pretty p-default p-round outline p-thick mr-0 "><input name="receiverType" type="radio" data-testid="value-card-receiver-type-radio-button" aria-label="Myself" value="myself" checked=""><span class="state p-primary-o"><span class="pretty-label">Myself</span></span></span></div>
            </div>
            <div class="mt-2 mr-8">
               <div class="flex items-center"><span class="pretty p-default p-round outline p-thick mr-0 "><input name="receiverType" type="radio" data-testid="value-card-receiver-type-radio-button" aria-label="Someone else" value="someoneElse"><span class="state p-primary-o"><span class="pretty-label">Someone else</span></span></span></div>
            </div>
         </div>
      </div>
   </div>
</div>

This is my PlayWright code,这是我的 PlayWright 代码,

export class MValueCardReceiverModalModel {
  private readonly page: Page;
  private readonly doneButton: Locator;
  private readonly backButton: Locator;
  private readonly someoneElseButton: Locator;

  constructor(page: Page) {
    this.page = page;
    this.doneButton = page.locator(
      "data-testid=modal >> data-testid=value-card-receiver-modal-done-button >> visible=true"
    );
    this.backButton = page.locator(
      "data-testid=modal >> data-testid=value-card-receiver-modal-back-button >> visible=true"
    );
    this.someoneElseButton = page.locator(
      "data-testid=modal >> data-testid=value-card-receiver-type-radio-button >> visible=true"
    );
  }

  async isAt(): Promise<void> {
    await expect(this.doneButton).toBeVisible();
    await expect(this.backButton).toBeVisible();
  }

  async selectForSomeoneElseButton(): Promise<void> {
    await this.someoneElseButton.check();
  }

  async clickDone(): Promise<void> {
    await this.doneButton.click();
  }
}

Here, when running the test, I am getting a issue in the selectForSomeoneElseButton() .在这里,运行测试时,我在selectForSomeoneElseButton()中遇到问题。

Can someone tell what am I doing wrong in the selector.有人可以告诉我在选择器中做错了什么。

I figured out the answer.我想出了答案。 Playwright throws error when two things have the same data-testid.当两个事物具有相同的 data-testid 时,Playwright 会抛出错误。 Here both my radio buttons have the same testid.在这里,我的两个单选按钮都具有相同的 testid。 I fixed it by having seperate testid我通过使用单独的 testid 来修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM