Skip to content

[Question] Is there a optimal way to click or tap on element depends on hasTouch attribute? #19624

@yurii-sav

Description

@yurii-sav

Hi. I am writing project with next requirements

  • one test should be run on any viewport type. (both desktop and mobile devices)
  • test should behave as much as possible closer to real user behavior

This means, that on some project configuration where it's supposed to be a desktop, I need to click() and on some (there hasTouch is enabled) I need to interact by tap() on the same element.

It forces me to write something like code below

// in base.pageObject.ts class
async clickOrTap(locator: Locator): Promise<void> {
    if (this.projectConfig.isMobile) {
      await locator.tap()
    } else {
      await locator.click()
    }
  }

and in the method where I actually click or tap on the same element

// in homepage.pageObject.ts
 public async clickLoginIcon(): Promise<void> {
    await this.clickOrTap(this.getLoginButton)
 }

In my opition there should be some feature from Playwright, for example method interact() where depends on the config Playwright clicks or tap on the element, but not sure that there is a big demand in it and possibility to implement. Maybe I am wrong and this behavior can be applied in some other way, by overriding Playwright methods or... please suggest your option. Thanks in advance for any suggestions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions