Keywords

Every keyword you can use in a step, grouped by what it does.

A keyword is one action or check, such as Press Element or Assert Presence. Each row of a module is one keyword followed by its parameters. This page covers the rules every keyword shares, then lists all of them.

modules/modules.csv
module_name,module_step,param_1,param_2
Sign In,Enter Text,${email_field},[email protected]
Sign In,Press Element,${sign_in}
Sign In,Assert Presence,Welcome,20

Keyword names#

Write keyword names in Title Case in CSV and YAML files: Press Element, Assert Presence. In optics live, the MCP server and Python, the same keyword is written in snake_case: press_element, assert_presence.

The runner ignores capitalization and collapses spaces, so Press Element, press element and press_element all reach the same keyword. If you misspell a name, the error suggests the closest match. Run optics list to print every keyword name in snake_case.

Parameters#

Parameters are positional. They fill the keyword's parameters in the order shown in each parameter table on these pages.

To set a parameter by name, write name=value. This works only when name is a real parameter of that keyword. Use it to skip parameters you want to leave at their defaults:

Browse,Press Element,Trail Runner 2,index=1

Here index=1 sets the fifth parameter of Press Element and leaves repeat, offset_x and offset_y at their defaults.

A value like text=Sign in or css=#cart stays positional, because text and css aren't parameter names. Values that start with /, // or ( are never treated as named, and ==, !=, <= and >= don't count as =.

Every value is a string#

Each keyword receives its parameters as text and converts them itself. Timeouts, counts, index, repeat, lengths and swipe percentages must be whole numbers. A decimal such as 2.5 fails the step. Timeouts are always in seconds.

Element arguments#

Many keywords take an element. Optics looks at the text you pass and decides what kind of locator it is. The checks run in this order, and the first match wins:

If the value…Optics reads it as
starts with text=Text to find on screen
starts with text_only:Text to find with OCR (reading the screenshot) only, skipping the app's UI tree
ends with .png, .jpg, .jpeg or .bmpAn image file name to find on screen
starts with css=A CSS selector
starts with xpath=, /, // or (An XPath, a path through the app's UI tree
starts with android. or xcuiA UI class name
contains [ and ], starts with ., or starts with # followed by a letterA CSS selector
anything elseText to find on screen

Text is matched against the app's UI tree first, then with OCR if you configured text detection. Image file names are matched by file name only, and Optics finds the file anywhere in your project folder. See Text and images for how matching works.

Other pitfalls:

  • id: locators always fail. No element source supports them. Use an XPath such as //*[@resource-id="com.birch.shop:id/cart"].
  • The text= prefix is understood by the Appium and Playwright element sources. OCR and Selenium search for the whole value, including text=, so use it only where you need it.
  • The xpath= prefix works only on Playwright. Use a bare //… XPath on Appium and Selenium.
  • .tif and .tiff files aren't treated as images. Save templates as .png.

Most of the time you pass ${name} instead and define the locator in your elements file. See Define elements.

Variables#

A cell that is exactly ${name} is replaced with the value of name from your elements file, or from a keyword that wrote it (Read Data, Evaluate, Run Loop, Invoke API).

When an element has several values, Optics tries each one in order until the element is found. It moves on only when the element isn't found. Any other error fails the step at once. If name isn't defined anywhere, the step fails before the keyword runs.

See Variables for how values are stored and passed between steps.

Areas of interest#

Keywords that find one element take four optional parameters: aoi_x, aoi_y, aoi_width and aoi_height. They limit the search to a rectangle of the screen, in percent from 0 to 100. The defaults 0, 0, 100, 100 mean the whole screen.

Checkout,Press Element,Pay now,aoi_y=50,aoi_height=50

This searches the bottom half of the screen only. Assertion keywords don't take an area of interest. See Area of interest.

Picking one of several matches#

index picks which match to use when more than one element matches. It starts at 0, the first match. index=1 is the second match.

  • Image matches are counted top to bottom, then left to right.
  • On Appium, index doesn't apply to XPath. Write (//android.widget.Button[@text="Add to cart"])[2] to pick the second XPath match. XPath counts from 1.
  • On Selenium, a non-zero index skips the native lookup, so only OCR and image matching can use it.

Event names#

Most action and app keywords take an optional event_name. When you set it, Optics records a timestamped event with that label right before the action. Events show in the live console and are sent to an events endpoint if you configured one. They never change whether a step passes.

Action keywords don't wait#

Press Element, Enter Text and the other action keywords look at the current screen once. If the element isn't there yet, the step fails at once. Put an assertion before an action when the screen may still be loading:

Search,Assert Presence,${search_bar},15
Search,Enter Text,${search_bar},Trail Runner 2

Use Detect And Press for pop-ups that may or may not appear. Assertion keywords are the only ones that wait. See Verification keywords.

Keywords that never fail a step#

These keywords pass even when you might expect a failure. Don't rely on them as checks.

  • Assert Equality returns false on a mismatch but never fails.
  • Validate Screen returns false when nothing is found but never fails.
  • Detect And Press passes and skips the tap when the element doesn't appear.
  • Force Terminate App on Appium logs any error and passes.
  • Swipe, Press By Coordinates and Scroll on Appium log driver errors and pass. Scroll left or right on Appium does nothing and passes.
  • Assert Presence and Assert Visibility pass without checking when every element you give is a CSS selector, an id: locator or a class name.
  • Initialise Setup does nothing.

Keywords that return a value, such as Get Text, Execute Script and Capture Screenshot, can't store it in a variable from CSV or YAML. The value is returned to you over optics serve, MCP and Python.

All keywords#

App management#

KeywordWhat it doesPage
Launch AppStart the app or browser, or bring the app to the frontLaunch App
Start Appium SessionStart a driver session without naming an appStart Appium Session
Launch Other AppSwitch to another app, deep link or URLLaunch Other App
Close And Terminate AppEnd the session and close the app or browserClose And Terminate App
Force Terminate AppKill a specific appForce Terminate App
Get App VersionRead the installed app's versionGet App Version
Get Driver Session IdReturn the current driver session idGet Driver Session Id
Initialise SetupDo nothingInitialise Setup

Actions#

KeywordWhat it doesPage
Press ElementTap or click an elementPress Element
Press By PercentageTap a point given in percent of the screenPress By Percentage
Press By CoordinatesTap a pixel coordinatePress By Coordinates
Detect And PressTap an element only if it appearsDetect And Press
Select Dropdown OptionOpen a dropdown and pick an optionSelect Dropdown Option
Enter TextType into a fieldEnter Text
Enter NumberType a number into a fieldEnter Number
Enter Text DirectType into the focused fieldEnter Text Direct
Enter Text Using KeyboardType key by key, or press one special keyEnter Text Using Keyboard
Clear Element TextEmpty a text fieldClear Element Text
SwipeSwipe from a pixel pointSwipe
Swipe By PercentageSwipe from a point given in percentSwipe By Percentage
Swipe From ElementSwipe starting on an elementSwipe From Element
Swipe Until Element AppearsSwipe until an element shows upSwipe Until Element Appears
ScrollScroll the screen up or downScroll
Scroll From ElementScroll starting on an elementScroll From Element
Scroll Until Element AppearsScroll until an element shows upScroll Until Element Appears
Press KeycodeSend a hardware, system or remote keyPress Keycode
Execute ScriptRun a driver command or browser JavaScriptExecute Script
Get TextRead an element's textGet Text
SleepWait a number of secondsSleep
Press CheckboxDeprecated: use Press ElementPress Checkbox
Press Radio ButtonDeprecated: use Press ElementPress Radio Button
Swipe Seekbar To Right AndroidDeprecated: use Swipe From ElementSwipe Seekbar To Right Android

Verification#

KeywordWhat it doesPage
Assert PresenceFail unless elements exist within a timeoutAssert Presence
Assert VisibilityFail unless elements are on screen within a timeoutAssert Visibility
Validate ElementFail unless one element exists within a timeoutValidate Element
Validate ScreenCheck for elements without failingValidate Screen
Is ElementCheck that an element is visible, invisible, enabled or disabledIs Element
Assert EqualityCompare two values without failingAssert Equality
Capture ScreenshotTake a screenshotCapture Screenshot
Capture PagesourceSave the current UI treeCapture Pagesource
Get Interactive ElementsList the elements you can tap or type intoGet Interactive Elements
Get Screen ElementsTake a screenshot and list every elementGet Screen Elements

Flow control#

KeywordWhat it doesPage
Execute ModuleRun another moduleExecute Module
Run LoopRepeat a module a number of times or once per valueRun Loop
ConditionRun a module only when a condition is trueCondition
Read DataLoad a value from a CSV or JSON file or an environment variableRead Data
EvaluateCompute an expression and save the resultEvaluate
Date EvaluateAdd or subtract days from a date and save itDate Evaluate
Invoke APICall an HTTP API and save values from the responseInvoke API

Deprecated keywords#

Press Checkbox and Press Radio Button still work but log a warning. Use Press Element instead. Swipe Seekbar To Right Android fails on Appium. Use Swipe From Element instead.

Where each keyword works#

  • **CSV and YAML suites, optics live and optics serve** have every keyword on this page.
  • The MCP server has every keyword except the flow-control ones. Press By Coordinates and Press By Percentage are hidden there unless you turn them on.
  • Python and Robot Framework have every keyword except Execute Module, Initialise Setup and the three deprecated ones. Some parameters differ. See Write tests in Python.
  • **optics generate** skips flow-control keywords and a few others when it converts a suite to code.

Python and Robot Framework only#

These keywords exist only in the Python library and the Robot Framework library. The CSV and YAML runner reports them as unknown keywords.

KeywordWhat it does
SetupCreate a session from a config dictionary or YAML text
Setup From FileCreate a session from a config file
QuitEnd the session
Add ElementAdd a value to a variable, after any values it already has
Get Element ValueReturn the list of values for a variable
Add APILoad API definitions for Invoke API
Add ModuleDefine a module for Run Loop and Condition
Add TestcaseSet the session's test cases
Add Error DefinitionsAdd on-screen error definitions
Capture And Detect ErrorsCheck the current screen for defined errors and return any matches
Press Element With IndexPress Element with an index