Skip to content
CodeKruPro

Hold Shift while typing with the actions API

Automation taskMediumAsked in: Accenture, Capgemini, Infosys, TCS, Wipro

"How do you hold a key down while doing something else?" In the actions chain: key down, do the other steps, key up, then perform. Always release the key, or later steps behave as if it is still held.

Keyboard - Actions Class Key Events

Keyboard Actions with Actions Class

The Actions class provides more granular keyboard control:

  • keyDown(Keys.SHIFT) - press and hold a key
  • keyUp(Keys.SHIFT) - release a key
  • sendKeys("text") - type text

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the firstName field
  3. In one Actions chain: click the field so it has focus, hold Keys.SHIFT with keyDown(), type the lower-case word "hello", release Shift with keyUp(), and finish with perform()
  4. Read the field's value and print it after the label Typed:

Holding Shift turns the lower-case letters into capitals, so the field ends up holding HELLO.

Expected output:

Typed: HELLO