Use Optics in Robot Framework
Import Optics as a Robot Framework library and use its keywords in .robot files.
Optics is also a Robot Framework library. If your team already writes .robot suites, you can add Optics keywords to them and get its fallbacks, OCR and image matching without changing tools.
Before you begin#
Robot Framework isn't installed with Optics. Add it to the same environment:
$ pip install robotframework "optics-framework[appium]"Swap appium for the engine you use. You also need a config.yaml with driver_sources and elements_sources. Any Optics project's config works.
Write a suite#
*** Settings ***
Library OperatingSystem
Library optics_framework.optics.Optics
Suite Setup Open Optics
Suite Teardown Quit
*** Variables ***
${EMAIL_FIELD} //*[@resource-id="com.birch.shop:id/email"]
*** Keywords ***
Open Optics
${config}= Get File ${CURDIR}/config.yaml
Setup config=${config}
*** Test Cases ***
Shopper Can Sign In
Launch App
Detect And Press Allow timeout=5
Enter Text ${EMAIL_FIELD} [email protected]
${sign_in}= Create List //*[@resource-id="com.birch.shop:id/login"] Sign in
Press Element ${sign_in}
Assert Presence Picked for you|Search Birch timeout=20 rule=anyRun it like any Robot suite:
$ robot checkout.robotHow Optics keywords behave in Robot#
- Setup takes the config's contents, not its path. Read the file with
Get Fileas shown above, then pass the text toSetup. If you use image elements, addproject_path: .to the config so Optics can find the pictures. - Pass fallbacks as a list.
Create Listbuilds the values to try in order. Writing several plain arguments fills the keyword's next parameters instead. - Separate several elements with a pipe inside one argument:
Picked for you|Search Birch. - Named arguments use the Python names:
timeout=20,rule=any,index=1. - **Robot replaces
${…}itself.** To pass an Optics variable toEvaluateorCondition, escape it:\${total}. - **
SleepandEvaluateexist in Robot's BuiltIn library too.** Robot warns and uses the Optics version, which only accepts whole seconds. WriteBuiltIn.Sleep 2sorOptics.Sleep 2to be explicit.
The keyword reference lists every keyword. The Python-only keywords in Use Optics from Python, such as Add Element and Add Module, work in Robot too.
Generate Robot code from a suite#
If you already have a CSV or YAML suite, optics generate can write a starting .robot file from it. See Generate pytest or Robot code.