|
| 1 | +# API Reference |
| 2 | + |
| 3 | +The Optics Framework provides a comprehensive set of APIs for test automation across different categories. This reference covers all available APIs, their methods, and usage examples. |
| 4 | + |
| 5 | +## Core API Modules |
| 6 | + |
| 7 | +### ActionKeyword |
| 8 | + |
| 9 | +The `ActionKeyword` class provides high-level functionality for performing user actions on applications, including pressing elements, scrolling, swiping, and text input. |
| 10 | + |
| 11 | +#### Methods |
| 12 | + |
| 13 | +##### Element Interaction |
| 14 | + |
| 15 | +- **`press_element(element, event_name=None)`** |
| 16 | + Presses/clicks the specified element using self-healing location strategies. |
| 17 | + - `element`: Element identifier (XPath, image template, or OCR text) |
| 18 | + - `event_name`: Optional event name for logging |
| 19 | + |
| 20 | +- **`press_by_coordinates(coor_x, coor_y, repeat=1, event_name=None)`** |
| 21 | + Presses at specific screen coordinates. |
| 22 | + - `coor_x`, `coor_y`: Screen coordinates |
| 23 | + - `repeat`: Number of times to repeat the action |
| 24 | + - `event_name`: Optional event name |
| 25 | + |
| 26 | +- **`press_by_percentage(percent_x, percent_y, repeat=1, event_name=None)`** |
| 27 | + Presses at screen position defined by percentage of screen size. |
| 28 | + - `percent_x`, `percent_y`: Percentage coordinates (0-100) |
| 29 | + - `repeat`: Number of times to repeat |
| 30 | + - `event_name`: Optional event name |
| 31 | + |
| 32 | +- **`press_element_with_index(element, index=0, event_name=None)`** |
| 33 | + Presses an element by its index when multiple matches are found. |
| 34 | + - `element`: Element identifier |
| 35 | + - `index`: Zero-based index of the element to press |
| 36 | + - `event_name`: Optional event name |
| 37 | + |
| 38 | +- **`detect_and_press(element, timeout, event_name=None)`** |
| 39 | + Waits for element to appear and then presses it. |
| 40 | + - `element`: Element identifier |
| 41 | + - `timeout`: Maximum wait time in seconds |
| 42 | + - `event_name`: Optional event name |
| 43 | + |
| 44 | +##### Form Controls |
| 45 | + |
| 46 | +- **`press_checkbox(element, event_name=None)`** |
| 47 | + Interacts with checkbox elements. |
| 48 | + - `element`: Checkbox element identifier |
| 49 | + - `event_name`: Optional event name |
| 50 | + |
| 51 | +- **`press_radio_button(element, event_name=None)`** |
| 52 | + Selects radio button elements. |
| 53 | + - `element`: Radio button element identifier |
| 54 | + - `event_name`: Optional event name |
| 55 | + |
| 56 | +- **`select_dropdown_option(element, option, event_name=None)`** |
| 57 | + Selects an option from a dropdown menu. |
| 58 | + - `element`: Dropdown element identifier |
| 59 | + - `option`: Option text to select |
| 60 | + - `event_name`: Optional event name |
| 61 | + |
| 62 | +##### Scrolling and Swiping |
| 63 | + |
| 64 | +- **`scroll(direction, event_name=None)`** |
| 65 | + Performs scroll action in the specified direction. |
| 66 | + - `direction`: Scroll direction ('up', 'down', 'left', 'right') |
| 67 | + - `event_name`: Optional event name |
| 68 | + |
| 69 | +- **`scroll_until_element_appears(element, direction, timeout, event_name=None)`** |
| 70 | + Scrolls until the specified element becomes visible. |
| 71 | + - `element`: Element to find |
| 72 | + - `direction`: Scroll direction |
| 73 | + - `timeout`: Maximum time to scroll |
| 74 | + - `event_name`: Optional event name |
| 75 | + |
| 76 | +- **`scroll_from_element(element, direction, scroll_length, event_name=None)`** |
| 77 | + Scrolls from a specific element position. |
| 78 | + - `element`: Starting element |
| 79 | + - `direction`: Scroll direction |
| 80 | + - `scroll_length`: Distance to scroll |
| 81 | + - `event_name`: Optional event name |
| 82 | + |
| 83 | +- **`swipe(coor_x, coor_y, direction='right', swipe_length=50, event_name=None)`** |
| 84 | + Performs swipe gesture from coordinates. |
| 85 | + - `coor_x`, `coor_y`: Starting coordinates |
| 86 | + - `direction`: Swipe direction |
| 87 | + - `swipe_length`: Swipe distance |
| 88 | + - `event_name`: Optional event name |
| 89 | + |
| 90 | +- **`swipe_until_element_appears(element, direction, timeout, event_name=None)`** |
| 91 | + Swipes until element becomes visible. |
| 92 | + - `element`: Element to find |
| 93 | + - `direction`: Swipe direction |
| 94 | + - `timeout`: Maximum time to swipe |
| 95 | + - `event_name`: Optional event name |
| 96 | + |
| 97 | +- **`swipe_from_element(element, direction, swipe_length, event_name=None)`** |
| 98 | + Swipes from a specific element position. |
| 99 | + - `element`: Starting element |
| 100 | + - `direction`: Swipe direction |
| 101 | + - `swipe_length`: Swipe distance |
| 102 | + - `event_name`: Optional event name |
| 103 | + |
| 104 | +##### Text Input |
| 105 | + |
| 106 | +- **`enter_text(element, text, event_name=None)`** |
| 107 | + Enters text into the specified input element. |
| 108 | + - `element`: Input element identifier |
| 109 | + - `text`: Text to enter |
| 110 | + - `event_name`: Optional event name |
| 111 | + |
| 112 | +- **`enter_text_direct(text, event_name=None)`** |
| 113 | + Enters text directly without targeting a specific element. |
| 114 | + - `text`: Text to enter |
| 115 | + - `event_name`: Optional event name |
| 116 | + |
| 117 | +### Verifier |
| 118 | + |
| 119 | +The `Verifier` class provides methods to verify elements, screens, and data integrity. |
| 120 | + |
| 121 | +#### Methods |
| 122 | + |
| 123 | +- **`validate_element(element, timeout=10, rule="all", event_name=None)`** |
| 124 | + Verifies the specified element exists. |
| 125 | + - `element`: Element identifier (Image template, OCR template, or XPath) |
| 126 | + - `timeout`: Time to wait for verification in seconds |
| 127 | + - `rule`: Verification rule ("all" or "any") |
| 128 | + - `event_name`: Optional event name |
| 129 | + |
| 130 | +- **`is_element(element, element_state, timeout, event_name=None)`** |
| 131 | + Checks if an element exists and matches the specified state. |
| 132 | + - `element`: Element identifier |
| 133 | + - `element_state`: Expected state of the element |
| 134 | + - `timeout`: Maximum wait time |
| 135 | + - `event_name`: Optional event name |
| 136 | + |
| 137 | +- **`assert_presence(element, timeout, rule, event_name=None)`** |
| 138 | + Asserts that elements are present using specified rules. |
| 139 | + - `element`: Element or list of elements |
| 140 | + - `timeout`: Time to wait |
| 141 | + - `rule`: Rule for assertion ("all" or "any") |
| 142 | + - `event_name`: Optional event name |
| 143 | + |
| 144 | +### FlowControl |
| 145 | + |
| 146 | +The `FlowControl` class manages control flow operations including loops, conditions, and data handling. |
| 147 | + |
| 148 | +#### Methods |
| 149 | + |
| 150 | +- **`condition(*args)`** |
| 151 | + Evaluates multiple conditions and executes corresponding modules if conditions are true. |
| 152 | + - `*args`: Condition parameters |
| 153 | + |
| 154 | +- **`evaluate(param1, param2)`** |
| 155 | + Evaluates mathematical or logical expressions and stores results in variables. |
| 156 | + - `param1`: First parameter for evaluation |
| 157 | + - `param2`: Second parameter for evaluation |
| 158 | + |
| 159 | +- **`read_data(input_element, file_path, index=None)`** |
| 160 | + Reads data from CSV files, API URLs, or lists and assigns to variables. |
| 161 | + - `input_element`: Variable to store the data |
| 162 | + - `file_path`: Path to data source |
| 163 | + - `index`: Optional index for specific data item |
| 164 | + |
| 165 | +- **`run_loop(target, *args)`** |
| 166 | + Runs loops either by count or by iterating over variable-value pairs. |
| 167 | + - `target`: Loop target (module or count) |
| 168 | + - `*args`: Loop parameters |
| 169 | + |
| 170 | +### AppManagement |
| 171 | + |
| 172 | +The `AppManagement` class provides functionality for launching, terminating, and modifying app settings. |
| 173 | + |
| 174 | +#### Methods |
| 175 | + |
| 176 | +- **`launch_app(event_name=None)`** |
| 177 | + Launches the specified application. |
| 178 | + - `event_name`: Optional event name |
| 179 | + |
| 180 | +- **`start_appium_session(event_name=None)`** |
| 181 | + Starts an Appium session. |
| 182 | + - `event_name`: Optional event name |
| 183 | + |
| 184 | +- **`start_other_app(package_name, event_name=None)`** |
| 185 | + Starts a different application by package name. |
| 186 | + - `package_name`: Application package identifier |
| 187 | + - `event_name`: Optional event name |
| 188 | + |
| 189 | +- **`close_app(event_name=None)`** |
| 190 | + Closes the current application. |
| 191 | + - `event_name`: Optional event name |
| 192 | + |
| 193 | +- **`terminate_app(event_name=None)`** |
| 194 | + Terminates the application forcefully. |
| 195 | + - `event_name`: Optional event name |
| 196 | + |
| 197 | +## REST API Endpoints |
| 198 | + |
| 199 | +The Optics Framework also provides a REST API for remote automation control through the FastAPI server. |
| 200 | + |
| 201 | +### Session Management |
| 202 | + |
| 203 | +#### POST `/v1/sessions/start` |
| 204 | +Creates a new automation session. |
| 205 | + |
| 206 | +**Request Body:** |
| 207 | +```json |
| 208 | +{ |
| 209 | + "driver_sources": ["appium"], |
| 210 | + "elements_sources": ["appium_find_element"], |
| 211 | + "text_detection": ["easyocr"], |
| 212 | + "image_detection": ["templatematch"], |
| 213 | + "project_path": "/path/to/project", |
| 214 | + "appium_url": "http://localhost:4723", |
| 215 | + "appium_config": { |
| 216 | + "platformName": "Android", |
| 217 | + "deviceName": "emulator-5554" |
| 218 | + } |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +**Response:** |
| 223 | +```json |
| 224 | +{ |
| 225 | + "session_id": "uuid-string", |
| 226 | + "status": "created" |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +#### DELETE `/v1/sessions/{session_id}/stop` |
| 231 | +Terminates an automation session. |
| 232 | + |
| 233 | +**Response:** |
| 234 | +```json |
| 235 | +{ |
| 236 | + "status": "terminated" |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +### Action Execution |
| 241 | + |
| 242 | +#### POST `/v1/sessions/{session_id}/action` |
| 243 | +Executes a keyword action within a session. |
| 244 | + |
| 245 | +**Request Body:** |
| 246 | +```json |
| 247 | +{ |
| 248 | + "mode": "keyword", |
| 249 | + "keyword": "press_element", |
| 250 | + "params": ["element_identifier"] |
| 251 | +} |
| 252 | +``` |
| 253 | + |
| 254 | +**Response:** |
| 255 | +```json |
| 256 | +{ |
| 257 | + "execution_id": "uuid-string", |
| 258 | + "status": "SUCCESS", |
| 259 | + "data": {"result": "action_result"} |
| 260 | +} |
| 261 | +``` |
| 262 | + |
| 263 | +### Data Retrieval |
| 264 | + |
| 265 | +#### GET `/session/{session_id}/screenshot` |
| 266 | +Captures a screenshot of the current screen. |
| 267 | + |
| 268 | +#### GET `/session/{session_id}/elements` |
| 269 | +Retrieves interactive elements from the current screen. |
| 270 | + |
| 271 | +#### GET `/session/{session_id}/source` |
| 272 | +Gets the page source of the current application state. |
| 273 | + |
| 274 | +#### GET `/session/{session_id}/screen_elements` |
| 275 | +Captures screenshot and gets all screen elements in one call. |
| 276 | + |
| 277 | +### Event Streaming |
| 278 | + |
| 279 | +#### GET `/v1/sessions/{session_id}/events` |
| 280 | +Streams real-time events from the automation session using Server-Sent Events (SSE). |
| 281 | + |
| 282 | +## Usage Examples |
| 283 | + |
| 284 | +### Basic Element Interaction |
| 285 | +```python |
| 286 | +from optics_framework.api.action_keyword import ActionKeyword |
| 287 | + |
| 288 | +# Initialize action keyword instance |
| 289 | +action = ActionKeyword(builder) |
| 290 | + |
| 291 | +# Press an element |
| 292 | +action.press_element("login_button") |
| 293 | + |
| 294 | +# Enter text in a field |
| 295 | +action.enter_text("username_field", "my_username") |
| 296 | + |
| 297 | +# Scroll until element appears |
| 298 | +action.scroll_until_element_appears("submit_button", "down", 30) |
| 299 | +``` |
| 300 | + |
| 301 | +### Verification |
| 302 | +```python |
| 303 | +from optics_framework.api.verifier import Verifier |
| 304 | + |
| 305 | +# Initialize verifier |
| 306 | +verifier = Verifier(builder) |
| 307 | + |
| 308 | +# Validate element presence |
| 309 | +verifier.validate_element("welcome_message", timeout=15) |
| 310 | + |
| 311 | +# Check element state |
| 312 | +verifier.is_element("checkbox", "checked", 10) |
| 313 | +``` |
| 314 | + |
| 315 | +### Flow Control |
| 316 | +```python |
| 317 | +from optics_framework.api.flow_control import FlowControl |
| 318 | + |
| 319 | +# Initialize flow control |
| 320 | +flow = FlowControl(runner, modules) |
| 321 | + |
| 322 | +# Read data from CSV |
| 323 | +flow.read_data("user_data", "test_data.csv", 0) |
| 324 | + |
| 325 | +# Run conditional logic |
| 326 | +flow.condition("${user_data.active}", "equals", "true", "login_module") |
| 327 | +``` |
| 328 | + |
| 329 | +### App Management |
| 330 | +```python |
| 331 | +from optics_framework.api.app_management import AppManagement |
| 332 | + |
| 333 | +# Initialize app management |
| 334 | +app_mgmt = AppManagement(builder) |
| 335 | + |
| 336 | +# Launch application |
| 337 | +app_mgmt.launch_app("test_start") |
| 338 | + |
| 339 | +# Close application |
| 340 | +app_mgmt.close_app("test_end") |
| 341 | +``` |
| 342 | + |
| 343 | +## Self-Healing Features |
| 344 | + |
| 345 | +The ActionKeyword class includes self-healing capabilities through the `@with_self_healing` decorator. This feature: |
| 346 | + |
| 347 | +- Automatically tries multiple location strategies for elements |
| 348 | +- Captures screenshots for debugging when actions fail |
| 349 | +- Provides fallback mechanisms when primary element location fails |
| 350 | +- Logs detailed error information for troubleshooting |
| 351 | + |
| 352 | +## Error Handling |
| 353 | + |
| 354 | +All API methods include comprehensive error handling and logging: |
| 355 | + |
| 356 | +- Invalid element identifiers raise `ValueError` exceptions |
| 357 | +- Timeout scenarios are logged with appropriate error messages |
| 358 | +- Failed actions trigger fallback strategies when available |
| 359 | +- All operations are logged for debugging and audit purposes |
| 360 | + |
| 361 | +## Integration Notes |
| 362 | + |
| 363 | +- The framework supports multiple driver sources (Appium, screenshot-based) |
| 364 | +- Element location strategies include XPath, image templates, and OCR text |
| 365 | +- All methods support optional event naming for tracking and analytics |
| 366 | +- The API is designed for both programmatic use and CSV-based test definitions |
0 commit comments