Skip to main content

WebDriver Mouse and Keyboard Events

WebDriver testing with Eggplant Functional and SenseTalk lets you automate actions in browsers and web apps by accessing WebElement objects (HTML elements) in the DOM of the pages you're testing. Use the commands and functions described here to perform Mouse and Keyboard events in your SenseTalk scripts based on Selenium WebDriver connections.

WebElement Identifiers

To access a WebElement within the DOM, such as when using the FindElement() function or the Click command with a WebDriver connection, you have several ways to specify the element you want to interact with. The following WebElement Identifiers can be used to locate elements in the DOM:

  • webID: Use this property to locate an element by the value of its ID attribute.
  • webName: Use this property to locate an element by the value of its Name attribute.
  • webTagName: Use this property to locate an element whose HTML tag is equal to the given value.
  • webClassName: Use this property to locate an element by the value of its Class attribute.
  • webLinkText: Use this property to locate a link whose text is equal to the indicated value.
  • webPartialLinkText: Use this property to locate a link whose text contains the indicated value.
  • webCssSelector: Use this property to locate an element using the indicated CSS selector expression.
  • webXPath: Use this property to locate an element by using an XPath expression. For information about XPath and XPath expressions, see XPath in Selenium WebDriver: Complete Tutorial.

WebLocation Identifier

A WebLocation Identifier is another method you can use to interact with WebElements. A WebLocation Identifier is a property list that contains objectType, x, and y properties. The objectType is always webLocation, and the x and y properties represent the coordinates of a point on the web page. For example:

{objectType:"webLocation", x:"476", y:"83"}

Typically, you would use coordinates for where a WebElement is located. The FoundWebLocation() function returns the WebLocation property list for found WebElements.

Mouse Events

You can use the following SenseTalk commands to simulate mouse events on WebElements with WebDriver connections.

Click Command

note

This code definition explains the use of the Click command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: Click Command.

Behavior: The Click command locates an element within the DOM and simulates a click event at the found location.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
Click { identifierType: value {, waitFor: duration} }
Click { objectType: "webLocation", x: valueX , y: valueY }
Click { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

click (webName:"q")

Example:

Click {webid:"uh-signin",waitFor:10}

Example:

if webElementFound(WebID:"login-username",WaitFor:10)
Click the FoundWebLocation // Clicks at the web location of the login-username web element
end if

Example:

set CheckBoxes to FindElements(WebCSSSelector:"input[type=checkbox]:not([checked])")
repeat with each CheckBox of CheckBoxes
Click CheckBox
end repeat

DoubleClick Command

note

This code definition explains the use of the DoubleClick command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: DoubleClick Command.

Behavior: The DoubleClick command locates an element within the DOM and simulates a double-click event at the found location.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
DoubleClick { identifierType: value {, waitFor: duration} }
DoubleClick { objectType: "webLocation", x: valueX , y: valueY }
DoubleClick { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

DoubleClick {webName:"TestPlant"}

Example:

Doubleclick {webName:"Testplant",waitFor:20}

RightClick Command

note

This code definition explains the use of the RightClick command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: RightClick Command.

Behavior: The RightClick command locates an element within the DOM and simulates a right-click event at the found location.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
RightClick { identifierType: value {, waitFor: duration} }
RightClick { objectType: "webLocation", x: valueX , y: valueY }
RightClick { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

RightClick {WebLinkText:"Home Lending"}

Example:

rightClick {objectType:"webLocation", x:247, y:108}

Example:

RightClick {WebCSSSelector:"div>a[data-pt-name=share_fb]",waitFor:10}

MoveTo Command

note

This code definition explains the use of the MoveTo command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: MoveTo Command.

Behavior: The MoveTo command locates an element within the DOM and moves the cursor on the web page to the location where the element is displayed. If the WebElement is not in view, this command scrolls it into view. You can use this command to perform hover actions such as opening pop-up menus.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
MoveTo { identifierType: value {, waitFor: duration} }
MoveTo { objectType: "webLocation", x: valueX , y: valueY }
MoveTo { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

moveTo {webPartialLinkText:"TestPlant"}

Example:

MoveTo {WebCSSSelector:"div[class*=icon-savings-small]",WaitFor:10}

Example:

Moveto {WebID:"sign-in-button"} // Scrolls the element into the viewport by first locating it in the DOM
Click "SignInButton" // Uses an image search to confirm the button appears correctly in the UI and then clicks on it

Example:

set elementsList to FindElements(webTagName:"h3")
repeat with each element in elementsList
moveTo element
WaitFor 2, "CommonIcon"
end repeat

Drag Command

note

This code definition explains the use of the Drag command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: Drag Command.

Behavior: The Drag command locates an element within the DOM, moves the cursor on the web page to the location where the element is displayed, then simulates a mouse-down action. Typically, you would use the Drag command followed by the Drop command, which moves the cursor to a new location and releases the mouse-down.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
Drag { identifierType: value {, waitFor: duration} }
Drag { objectType: "webLocation", x: valueX , y: valueY }
Drag { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

drag {webTagName:"H1"}
drop {WebtagName:"H2"}

Drop Command

note

This code definition explains the use of the Drop command for WebDriver connections. For information about using this command for image-based scripting, see Mouse Events and Control: Drop Command.

Behavior: The Drop command locates an element within the DOM, moves the cursor on the web page to the location where the element is displayed, then simulates a mouse-up action. During the move, the mouse stays in its initial state, either up or down. Typically, you would use the Drop command after the Drag command, which sets the cursor location and simulates a mouse-down action.

Parameters: A property list containing a required WebElement Identifier and an optional WaitFor property. Alternatively, you can provide a property list that contains a WebLocation Identifier, in which case a WaitFor property is irrelevant.

You can also pass multiple WebElement Identifiers, in which case, each should be included as a separate property list. When you pass more than one WebElement, Eggplant Functional searches for them in the order you list them in the command. If you include WaitFor properties in multiple property lists, SenseTalk uses only the longest WaitFor value for the entire command. That is, it doesn't apply that property multiple times.

Syntax:
Drop { identifierType: value {, waitFor: duration} }
Drop { objectType: "webLocation", x: valueX , y: valueY }
Drop { identifierType: value {, waitFor: duration} } {, { identifierType2: value2 {, waitFor: duration2} }, ... }

Example:

drop {webTagName:"H3"}

Keyboard Events

You can use the following SenseTalk command to send keyboard events with WebDriver connections.

SendKeys Command

Behavior: This command sends keystrokes to the browser or WebElement.

Parameters: Typically, you send text strings enclosed in quotation marks. You can also use SenseTalk keywords for special characters or keys, such as Return and Tab. See the table below for a list of SenseTalk keywords you can include with the SendKeys command.

As an alternative to quotes, you can enclose text in double angle brackets (<< >>). Use this method if you need to include quote characters in your text string.

Syntax:
SendKeys {"text" | keyword | text}
SendKeys webElement, {"text" | keyword | text}

Note that you can combine the types of text input within the command. For example, you could use quoted text and a keyword. See the examples below for different ways of doing so.

Example:

sendKeys "eggplant" // Sends the keystrokes to type 'eggplant'

Example:

sendKeys "eggplant", return // Sends the keystrokes to type 'eggplant' followed by a Return character

Example:

click (webtagname: "textarea")
sendkeys "A noble spirit embiggens the smallest man.",return,"It's a perfectly cromulent word." // Sends the keystrokes to type the first text string, followed by a Return character, then the second text string on a new line

Example:

put findelement (webtagname: "textarea") into TextField Puts the location of the WebElement into a variable
SendKeys TextField, "She said,"&&quote&"Yes."&quote // Uses the variable to make the WebElement active, then sends the keystrokes to type: She said, "Yes."

Example:

put findelement (webtagname: "textarea") into TextField
SendKeys TextField, <<She said, "Yes.">> Types the same string as in the previous example
Submit

SendKeys Keywords

The following table lists SenseTalk keywords you can use with SendKeys for common keyboard special keys. However, this list is not intended to be comprehensive. Any SenseTalk predefined variable that is defined as an ordinary character or string should also work with SendKeys.

To press this key . . .Type this keyword
0, 1…9 (on keypad)Keypad0, Keypad1...Keypad9
Alt (Windows)WindowsAltKey, AltKey
Arrow downDownArrow
Arrow leftLeftArrow
Arrow rightRightArrow
Arrow upUpArrow
Backslash (\)Backslash
Backspace/DeleteBackspace
CommandCommandKey
ControlControlKey
Delete (forward)DeleteKey
EndEndKey
EscapeEscape, EscapeKey
Forward slash (/)Slash
Function keysF1...F12 (but not F13...F35)
HomeHomeKey
InsertInsertKey
Option (Mac)OptionKey
Page DownPageDown
Page UpPageUp
Quote (" double-quote)Quote
ReturnReturn
ShiftShiftKey
SpaceSpace
TabTab

Related:

ClearKeys Command

Behavior: Clears the text entered in the active WebElement or a specified WebElement. Use this command to clear text from a web page field or entry form if necessary.

Parameters: None required, but can take an optional WebElement Identifier. If you don't specify a WebElement Identifier, the command operates on the active WebElement. You can also specify an optional WebDriverConnection object to switch the active connection.

Syntax:
ClearKeys {webDriverConnection,} webElementIdentifier

Example:

ClearKeys // Removes any content from the active WebElement

Example:

Click {webtagname: "textarea"}
sendkeys "Once more unto the breach." // Enters text into the text field
MoveTo {webTagName: "h2"} // Moves the focus to a different WebElement
ClearKeys {webtagname: "textarea"} // Uses a WebElement Identifier to remove the text previously entered