Skip to content

Commit 069e6db

Browse files
authored
fix(webdriverio): When no argument is passed to the .click interaction command, the webdriver command should also have no argument (#8937)
1 parent 97aed74 commit 069e6db

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

docs/guide/browser/interactivity-api.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,33 @@ test('clicks on an element', async () => {
5959
await userEvent.click(logo)
6060
// or you can access it directly on the locator
6161
await logo.click()
62+
63+
// With WebdriverIO, this uses either ElementClick (with no arguments) or
64+
// actions (with arguments). Use an empty object to force the use of actions.
65+
await logo.click({})
6266
})
6367
```
6468

69+
### Clicking with a modifier
70+
71+
With either WebdriverIO or Playwright:
72+
73+
```ts
74+
await userEvent.keyboard('{Shift>}')
75+
// By using an empty object as the option, this opts in to using a chain of actions
76+
// instead of an ElementClick in webdriver.
77+
// Firefox has a bug that makes this necessary.
78+
// Follow https://bugzilla.mozilla.org/show_bug.cgi?id=1456642 to know when this
79+
// will be fixed.
80+
await userEvent.click(element, {})
81+
await userEvent.keyboard('{/Shift}')
82+
```
83+
84+
With Playwright:
85+
```ts
86+
await userEvent.click(element, { modifiers: ['Shift'] })
87+
```
88+
6589
References:
6690

6791
- [Playwright `locator.click` API](https://playwright.dev/docs/api/class-locator#locator-click)

packages/browser-webdriverio/src/commands/click.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import type { UserEventCommand } from './utils'
44
export const click: UserEventCommand<UserEvent['click']> = async (
55
context,
66
selector,
7-
options = {},
7+
options,
88
) => {
99
const browser = context.browser
10-
await browser.$(selector).click(options as any)
10+
await browser.$(selector).click(options)
1111
}
1212

1313
export const dblClick: UserEventCommand<UserEvent['dblClick']> = async (
1414
context,
1515
selector,
16-
_options = {},
16+
_options,
1717
) => {
1818
const browser = context.browser
1919
await browser.$(selector).doubleClick()
@@ -22,7 +22,7 @@ export const dblClick: UserEventCommand<UserEvent['dblClick']> = async (
2222
export const tripleClick: UserEventCommand<UserEvent['tripleClick']> = async (
2323
context,
2424
selector,
25-
_options = {},
25+
_options,
2626
) => {
2727
const browser = context.browser
2828
await browser

packages/browser/src/client/tester/locators/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ export abstract class Locator {
7474
})
7575
}
7676

77-
public click(options: UserEventClickOptions = {}): Promise<void> {
77+
public click(options?: UserEventClickOptions): Promise<void> {
7878
return this.triggerCommand<void>('__vitest_click', this.selector, options)
7979
}
8080

81-
public dblClick(options: UserEventClickOptions = {}): Promise<void> {
81+
public dblClick(options?: UserEventClickOptions): Promise<void> {
8282
return this.triggerCommand<void>('__vitest_dblClick', this.selector, options)
8383
}
8484

85-
public tripleClick(options: UserEventClickOptions = {}): Promise<void> {
85+
public tripleClick(options?: UserEventClickOptions): Promise<void> {
8686
return this.triggerCommand<void>('__vitest_tripleClick', this.selector, options)
8787
}
8888

test/browser/fixtures/user-event/keyboard.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ test('click with modifier', async () => {
6363
});
6464

6565
await userEvent.keyboard('{Shift>}')
66-
await userEvent.click(el)
66+
// By using an empty object as the option, this opts in to using a chain of actions instead of an elementClick in webdriver.
67+
await userEvent.click(el, {})
6768
await userEvent.keyboard('{/Shift}')
6869
await expect.poll(() => el.textContent).toContain("[ok]")
6970
})

test/browser/test/userEvent.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ describe('userEvent.click', () => {
2323
document.body.appendChild(button)
2424
const onClick = vi.fn()
2525
const dblClick = vi.fn()
26+
// Make sure a contextmenu doesn't actually appear, as it may make some
27+
// tests fail later.
28+
const onContextmenu = vi.fn(e => e.preventDefault())
2629
button.addEventListener('click', onClick)
30+
button.addEventListener('dblclick', onClick)
31+
button.addEventListener('contextmenu', onContextmenu)
2732

2833
await userEvent.click(button)
2934

3035
expect(onClick).toHaveBeenCalled()
3136
expect(dblClick).not.toHaveBeenCalled()
37+
expect(onContextmenu).not.toHaveBeenCalled()
38+
39+
onClick.mockClear()
40+
await userEvent.click(button, { button: 'right' })
41+
expect(onClick).not.toHaveBeenCalled()
42+
expect(onContextmenu).toHaveBeenCalled()
3243
})
3344

3445
test('correctly doesn\'t click on a disabled button', async () => {

0 commit comments

Comments
 (0)