getgauge / taiko

A node.js library for testing modern web applications
https://taiko.dev
MIT License
3.57k stars 453 forks source link

Add the ability to Ctrl+Click #2587

Closed DCoomer closed 2 years ago

DCoomer commented 2 years ago

One of our pages on our website requires the user to do a ctrl+click to open a popup ( it's an legacy but still used feature). We've tried many different methods but none of them worked.

We found adding the modifiers parameter from the CDP dispatchMouseEvent to the Taiko click(), worked well: node_modules\taiko\lib\actions\click.js

async function simulateInputEvents(options) {
  for (let count = 0; count < options.noOfClicks; count++) {
    await doActionAwaitingNavigation(options, async () => {
      options.tap
        ? await tap(options.x, options.y)
        : await simulateMouseClick({
            x: options.x,
            y: options.y,
            clickCount: options.clickCount,
            button: options.button,
            modifiers: options.modifiers || 0
          });
    });
  }
}

or do it in setClickOptions:

const setClickOptions = (options, x, y) => {
  options = setNavigationOptions(options);
  options.x = x;
  options.y = y;
  options.button = options.button || 'left';
  options.clickCount = options.clickCount || 1;
  options.elementsToMatch = options.elementsToMatch || 10;
  options.modifiers = options.modifiers || 0
  return options;
};
async function simulateInputEvents(options) {
  for (let count = 0; count < options.noOfClicks; count++) {
    await doActionAwaitingNavigation(options, async () => {
      options.tap
        ? await tap(options.x, options.y)
        : await simulateMouseClick({
            x: options.x,
            y: options.y,
            clickCount: options.clickCount,
            button: options.button,
            modifiers: options.modifiers
          });
    });
  }
}

So the tester can do:

await click(element, {modifiers: 2});

to ctrl+click or any other modifer key from the CDP https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent

saikrishna321 commented 2 years ago

Thanks for investigating. Happy to review a PR. Can you please make a PR with the changes proposed?