Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Add combo key press #326

Merged
merged 1 commit into from
Aug 3, 2022
Merged

Add combo key press #326

merged 1 commit into from
Aug 3, 2022

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented May 26, 2022

This fixes #285

Shift+X when passed to press() and used in conjunction with down() and up() now will change the casing of the character, as long as the character is in the format KeyX. A key press that is x or X will ignore the shift modifier key. This fix matches the playwright behaviour.

E.g.

Shift+KeyA -> A
KeyA -> a
A -> A
a -> a
Shift+A -> A
Shift+a -> a

The Shift modifier will only change the case of a character if KeyA - KeyZ is used.

@ankur22 ankur22 requested review from inancgumus and imiric May 26, 2022 16:36
@ankur22 ankur22 changed the title Add combo key press Draft: Add combo key press May 26, 2022
@ankur22 ankur22 marked this pull request as draft May 26, 2022 16:37
@ankur22 ankur22 changed the title Draft: Add combo key press Add combo key press May 26, 2022
Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done, I just think we should move it to press() instead.

And re: the Shift+b behavior, I don't think Playwright makes sense in that case. Shift+b or Shift+B should produce B in both cases. A lowercase b can be typed without Shift. It's fine if we diverge with them to improve things. :)

common/keyboard.go Outdated Show resolved Hide resolved
@inancgumus
Copy link
Member

inancgumus commented May 31, 2022

And re: the Shift+b behavior, I don't think Playwright makes sense in that case. Shift+b or Shift+B should produce B in both cases. A lowercase b can be typed without Shift. It's fine if we diverge with them to improve things. :)

I'm mostly OK with what you propose @imiric, but here are my two cents.

Is diverging PW, in this case, worth it? If I were a tester, I'd at least expect similar behavior. Shift+KeyA+B+C means holding down the shift key while pressing the other keys. For example: Shift+KeyA+KeyB+B+b is ABBb, which makes sense to me.

Puppeteers also work with KeyA, KeyX, etc. style.

So instead of Shift+B, it should be Shift+KeyB, not Shift+B. KeyX is also in tandem with JS key codes: https://javascript.info/keyboard-events, https://www.toptal.com/developers/keycode/for/a, etc.

As I said, I'm also OK with what you proposed. But, I'm not sure whether we're improving things or making people's life harder 😅

@imiric
Copy link
Contributor

imiric commented May 31, 2022

I wasn't familiar with the KeyA/A difference, but would still find Shift+a producing a surprising. That said, it's best if we stick with Playwright in this case then.

Shift+KeyA+KeyB+B+b is ABBb, which makes sense to me

That's still confusing to me. Does the Shift apply to just the first KeyA, or to all keys? In all cases I've seen this notation used (outside of JS/browsers), it only makes sense for modifier keys that apply to a single non-modifier key, e.g. Shift+Control+Alt+A. After all, if A+B+C equals pressing one after another, there's no need to use + or a modifier key.

But still, let's confirm what Playwright's behavior is for this as well, and follow that to avoid adding more surprises for users.

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @imiric also noticed, splitting a given key should be done in press. down and up are only responsible for a single key, and that key can be a modifier as well. For more info: https://github.com/grafana/xk6-browser/pull/326/files#r885768387.

@ankur22 ankur22 force-pushed the bug/284-combo-key-press branch 2 times, most recently from c10d41d to e65be16 Compare June 21, 2022 16:05
@ankur22
Copy link
Collaborator Author

ankur22 commented Jun 29, 2022

I'm unable to get the Meta and Alt modifier keys to work with any of the existing functions e.g.

		kb.Press("Shift+KeyA", nil)
		kb.Press("Shift+b", nil)
		kb.Press("Shift+C", nil)

		metaKey := "Control"
		if runtime.GOOS == "darwin" {
			metaKey = "Meta"
		}
		kb.Press(metaKey+"+A", nil)
		kb.Press("Delete", nil)

OR

		kb.Press("Shift+KeyA", nil)
		kb.Press("Shift+b", nil)
		kb.Press("Shift+C", nil)

		kb.Press("Alt+ArrowLeft", nil)
		kb.Press("Delete", nil)

This should delete the text that has been written into the input field. To continue to track and resolve this a new ticket has been opened: grafana/k6#4440

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll continue looking into this tomorrow, but it failed on my machine as follows:

--- FAIL: TestPageInputSpecialCharacters (0.68s)
    page_test.go:341: 
                Error Trace:    page_test.go:341
                Error:          Not equal: 
                                expected: "[email protected]"
                                actual  : "test@k^>io"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                [email protected]
                                +test@k^>io
                Test:           TestPageInputSpecialCharacters
    page_test.go:341: 
                Error Trace:    page_test.go:341
                Error:          Not equal: 
                                expected: "<hello WoRlD \\/>"
                                actual  : "<hello WoRlD |?>"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -<hello WoRlD \/>
                                +<hello WoRlD |?>
                Test:           TestPageInputSpecialCharacters
    page_test.go:341: 
                Error Trace:    page_test.go:341
                Error:          Not equal: 
                                expected: "¯\\_(ツ)_/¯"
                                actual  : "¯|_(ツ)_?¯"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -¯\_(ツ)_/¯
                                +¯|_(ツ)_?¯
                Test:           TestPageInputSpecialCharacters

Update: CI has reported the failure as well: https://github.com/grafana/xk6-browser/runs/7114936484?check_suite_focus=true

Comment on lines +92 to +93

if err := k.comboPress(key, kbdOpts); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: No need to put an empty line here, but your call :)

common/keyboard.go Outdated Show resolved Hide resolved
common/keyboard.go Outdated Show resolved Hide resolved
}
}

for i := len(kk) - 1; i >= 0; i-- {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this is not a range as the above down loop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going backwards:

a->b->c
c->b->a

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK :) It's the same, but what about?

for i := range kk {
	// kk[len(kk)-i-1]
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure, nice little trick 🙂

Copy link
Contributor

@imiric imiric Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That trick is not intuitive at all IMO. The previous C-style for was clear and straightforward.

Please let's not point out style issues in reviews (we agreed on this before Ankur joined), unless the reviewer can explain an objective benefit of the change.

No need to change this back now, I don't want to spend more time on this, but just for future reference.

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked more into this. Here are my new findings.

Comment on lines 314 to 317
//nolint:gocritic
if string(s) == "+" && ss != "" {
kk = append(kk, ss)
ss = ""
Copy link
Member

@inancgumus inancgumus Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter is right. It's better to convert the if-else chain into a switch.

for _, k := range keys {
	switch {
	case k == '+' && ss != "":
		...
...

And, you can use strings.Builder to append runes without converting them to string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I hadn't realised that it was idiomatic to write them as switch statements. Source: https://go.dev/doc/effective_go#switch

common/keyboard.go Outdated Show resolved Hide resolved
common/keyboard.go Outdated Show resolved Hide resolved
ankur22 added a commit that referenced this pull request Jun 30, 2022
@ankur22 ankur22 requested a review from inancgumus June 30, 2022 15:23
common/keyboard.go Outdated Show resolved Hide resolved
@inancgumus inancgumus added the bug Something isn't working label Jun 30, 2022
@inancgumus inancgumus added this to the v0.4.0 milestone Jun 30, 2022
@inancgumus inancgumus removed the bug Something isn't working label Jun 30, 2022
@inancgumus inancgumus removed this from the v0.4.0 milestone Jun 30, 2022
ankur22 added a commit that referenced this pull request Jun 30, 2022
ankur22 added a commit that referenced this pull request Jun 30, 2022
@ankur22 ankur22 force-pushed the bug/284-combo-key-press branch from ca801e7 to f959cb1 Compare June 30, 2022 16:41
@ankur22 ankur22 requested a review from inancgumus July 4, 2022 11:37
Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👍 Only the test comment seems like a blocker to me, the others are minor suggestions.

It's strange that meta keys don't seem to work. That's a pretty important issue, but we can tackle it later.

Also, remember to mark the PR "Ready for review" when it's done being a draft.

common/keyboard.go Outdated Show resolved Hide resolved
common/keyboard.go Outdated Show resolved Hide resolved
tests/keyboard_test.go Show resolved Hide resolved
common/keyboard.go Outdated Show resolved Hide resolved
@ankur22 ankur22 marked this pull request as ready for review August 1, 2022 14:42
ankur22 added a commit that referenced this pull request Aug 1, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
Some of the integration tests can easily be converted to unit tests
which makes running them quicker, as well as making the tests simpler.
It also helps group similar tests.

Resolves: #326 (comment)
@ankur22 ankur22 requested a review from imiric August 2, 2022 16:25
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
ankur22 added a commit that referenced this pull request Aug 2, 2022
@ankur22 ankur22 force-pushed the bug/284-combo-key-press branch from 71ba9f5 to 8735b37 Compare August 2, 2022 16:33
ankur22 added a commit that referenced this pull request Aug 2, 2022
Some of the integration tests can easily be converted to unit tests
which makes running them quicker, as well as making the tests simpler.
It also helps group similar tests.

Resolves: #326 (comment)
Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice work 👍

I would just move split() to common/helpers.go and its tests to helpers_test.go. On second thought, it might be a uniquely keyboard helper function, so it makes sense where it is. Up to you.

The CI failures don't seem to be related to these changes, though that TestPageGotoWaitUntilDOMContentLoaded is a new one I haven't seen before. 😕 Please squash this into a single commit and push it so the tests run again before merging.

@ankur22 ankur22 force-pushed the bug/284-combo-key-press branch from 799182f to c3d4a69 Compare August 3, 2022 13:14
This change enables the `Shift` to alter the case of letters if the
special `Key*` is used (where * is A-z). The shift key is only used
when working with certain keyboard methods, and `Type` is not one of
them. The change follows the same behaviour as was found in PlayWright.
The separator is `+`, so "Shift+KeyA" would be split into "Shift" and
"KeyA".

Closes: #285
@ankur22 ankur22 force-pushed the bug/284-combo-key-press branch from c3d4a69 to 88b910a Compare August 3, 2022 13:21
@ankur22
Copy link
Collaborator Author

ankur22 commented Aug 3, 2022

I would just move split() to common/helpers.go and its tests to helpers_test.go. On second thought, it might be a uniquely keyboard helper function, so it makes sense where it is. Up to you.

Decided to not move it to the helper file since it feels like it might only be for the keyboard.

The CI failures don't seem to be related to these changes, though that TestPageGotoWaitUntilDOMContentLoaded is a new one I haven't seen before. 😕 Please squash this into a single commit and push it so the tests run again before merging.

Squashed the commits, and the build failed with the usual errors we've seen before. I reran the CI tests and it passed.

@ankur22 ankur22 merged commit 00960bd into main Aug 3, 2022
@imiric imiric deleted the bug/284-combo-key-press branch August 3, 2022 15:32
@ankur22 ankur22 mentioned this pull request Sep 7, 2022
ankur22 added a commit that referenced this pull request Sep 7, 2022
ElementHandle, Page, Locator and Frame all have the Press method. In a
recent fix (#326) combo keys with the Shift modifier key was fixed for
the Keyboard Press method. The Press methods for the other classes
should have worked with combo ley presses, but didn't. This fix will
ensure that combo key presses work for all of them.

Closes: #521
ankur22 added a commit that referenced this pull request Sep 8, 2022
* Fix press in classes to work with combo keys

ElementHandle, Page, Locator and Frame all have the Press method. In a
recent fix (#326) combo keys with the Shift modifier key was fixed for
the Keyboard Press method. The Press methods for the other classes
should have worked with combo ley presses, but didn't. This fix will
ensure that combo key presses work for all of them.

Closes: #521

* Apply suggestions from code review

Co-authored-by: Ivan Mirić <[email protected]>

Co-authored-by: Ivan Mirić <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pressing keys in combination is not supported
3 participants