grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
343 stars 41 forks source link

Match Dblclick with Playwright's behavior #469

Closed ankur22 closed 10 months ago

ankur22 commented 2 years ago

TODO:

xk6-browser docs imply that this dblclick issue is about locators, however, it's the behavior in our extension in general.


Tested against: https://github.com/grafana/xk6-browser/commit/dbede120c63df43995813a847a25b0e66e289592

The method dblclick on locator only clicks once.

In the below code it works against localhost:8080, which is a test server that can be found here with instructions on how to run it locally: https://github.com/ankur22/testserver.

import { sleep } from 'k6';
import { chromium } from 'k6/x/browser';

export default function () {
  const browser = chromium.launch({
    headless: false,
  });
  const context = browser.newContext();
  const page = context.newPage();
  const res = page.goto('https://test.k6.io/browser.php');
  const l = page.locator('#counter-button');
  sleep(1);
  l.dblclick();

  sleep(10);
  browser.close();
}

I was expecting the counter on this test page to go up to 2 but it only went up to 1. When testing with PW i was able to get the expected count of 2.

### Tasks
- [x] FIx issue -- https://github.com/grafana/xk6-browser/pull/1121
- [x] Add to release notes -- https://github.com/grafana/k6/pull/3506
- [x] Docs update -- https://github.com/grafana/k6-docs/pull/1456
imiric commented 2 years ago

Nice catch!

It doesn't seem related to Locator, but to ElementHandle:

https://github.com/grafana/xk6-browser/blob/dbede120c63df43995813a847a25b0e66e289592/common/element_handle.go#L242-L244

It should call Mouse.dblClick() :smile:

inancgumus commented 2 years ago

@ankur22 Thanks Ankur for helping me find the problems in the locator tests 🙇

@imiric No, it's not because of not calling Mouse.dblClick. Because some locator tests are incorrect 🤦 I'll send a PR.

Ankur, shouldn't the server needs to listen for the dblclick event here instead of the click event? I've changed it to dblclick and it worked.

I've then used the click() method in the script instead (while the server listens for the dblclick event). Expectedly, it did nothing (fortunately!). This validates that our dblclick() works.

ankur22 commented 2 years ago

@inancgumus I wasn't clear in the original description. The dblclick action in xk6-browser doesn't match the behaviour in playwright.

Playwright script:

const { test, chromium } = require('@playwright/test');

test.describe('Editing', () => {
  test('Type login creds', async () => {
    const browser = await chromium.launch({ headless: false });
    const ctx = await browser.newContext();
    const page = await ctx.newPage();
    await page.goto("https://test.k6.io/browser.php");
    const l = page.locator('#counter-button');
    await new Promise(resolve => setTimeout(resolve, 1000));
    l.dblclick();

    await new Promise(resolve => setTimeout(resolve, 10000));

    await browser.close();
  });
});

The outcome is that the counter on the test page is 2. Whereas the xk6-browser test script (now amended in the original description) results in 1.

I'm not sure what you mean here, could you elaborate a bit?:

Ankur, shouldn't the server needs to listen for the dblclick event here instead of the click event? I've changed it to dblclick and it worked.

I've then used the click() method in the script instead (while the server listens for the dblclick event). Expectedly, it did nothing (fortunately!). This validates that our dblclick() works.