|
| 1 | +""" |
| 2 | +Robot Framework keyword library for Playwright Android (BrowserStack). |
| 3 | +
|
| 4 | +Usage in .robot files: |
| 5 | + Library ../libraries/PlaywrightAndroidLibrary.py |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import urllib.parse |
| 10 | +from playwright.sync_api import sync_playwright, expect |
| 11 | + |
| 12 | + |
| 13 | +class PlaywrightAndroidLibrary: |
| 14 | + """Playwright Android keywords for Robot Framework.""" |
| 15 | + |
| 16 | + ROBOT_LIBRARY_SCOPE = "SUITE" |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + self._pw = None |
| 20 | + self._device = None |
| 21 | + self._context = None |
| 22 | + self._page = None |
| 23 | + |
| 24 | + # ── Connection ──────────────────────────────────────────────────────────── |
| 25 | + |
| 26 | + def connect_to_browserstack(self, username, access_key, caps: dict, timeout=120_000): |
| 27 | + """Connect to a BrowserStack Android device. |
| 28 | +
|
| 29 | + Example: |
| 30 | + | ${caps}= | Create Dictionary | deviceName=Samsung Galaxy S23 | osVersion=13.0 | |
| 31 | + | Connect To BrowserStack | myuser | mykey | ${caps} | |
| 32 | + """ |
| 33 | + caps = dict(caps) |
| 34 | + caps["browserstack.username"] = username |
| 35 | + caps["browserstack.accessKey"] = access_key |
| 36 | + ws_endpoint = ( |
| 37 | + "wss://cdp.browserstack.com/playwright?caps=" |
| 38 | + + urllib.parse.quote(json.dumps(caps)) |
| 39 | + ) |
| 40 | + self._pw = sync_playwright().start() |
| 41 | + self._device = self._pw.android.connect(ws_endpoint, timeout=int(timeout)) |
| 42 | + |
| 43 | + def get_device_info(self): |
| 44 | + """Return (serial, model) of the connected device.""" |
| 45 | + self._require_device() |
| 46 | + return self._device.serial, self._device.model |
| 47 | + |
| 48 | + # ── Browser ─────────────────────────────────────────────────────────────── |
| 49 | + |
| 50 | + def launch_browser(self, pkg=None, has_touch=False): |
| 51 | + """Launch Chrome on the device and create a new page. |
| 52 | +
|
| 53 | + Example: |
| 54 | + | Launch Browser | |
| 55 | + | Launch Browser | has_touch=True | |
| 56 | + """ |
| 57 | + self._require_device() |
| 58 | + try: |
| 59 | + self._device.shell("am force-stop com.android.chrome") |
| 60 | + except Exception: |
| 61 | + pass |
| 62 | + self._context = self._device.launch_browser( |
| 63 | + pkg=pkg, has_touch=has_touch == "True" or has_touch is True |
| 64 | + ) |
| 65 | + self._page = self._context.new_page() |
| 66 | + |
| 67 | + def close_browser(self): |
| 68 | + """Close the browser context.""" |
| 69 | + if self._page: |
| 70 | + self._page.close() |
| 71 | + self._page = None |
| 72 | + if self._context: |
| 73 | + self._context.close() |
| 74 | + self._context = None |
| 75 | + |
| 76 | + # ── Navigation ──────────────────────────────────────────────────────────── |
| 77 | + |
| 78 | + def navigate_to(self, url, timeout=30_000): |
| 79 | + """Navigate to a URL. |
| 80 | +
|
| 81 | + Example: |
| 82 | + | Navigate To | https://example.com | |
| 83 | + """ |
| 84 | + self._require_page() |
| 85 | + self._page.goto(url, timeout=int(timeout)) |
| 86 | + |
| 87 | + def go_back(self): |
| 88 | + """Navigate back in browser history.""" |
| 89 | + self._require_page() |
| 90 | + self._page.go_back() |
| 91 | + |
| 92 | + def wait_for_timeout(self, milliseconds): |
| 93 | + """Wait for the given number of milliseconds.""" |
| 94 | + self._require_page() |
| 95 | + self._page.wait_for_timeout(int(milliseconds)) |
| 96 | + |
| 97 | + # ── Assertions ──────────────────────────────────────────────────────────── |
| 98 | + |
| 99 | + def page_title_should_be(self, expected_title): |
| 100 | + """Assert the current page title equals the expected value.""" |
| 101 | + self._require_page() |
| 102 | + expect(self._page).to_have_title(expected_title) |
| 103 | + |
| 104 | + def page_title_should_contain(self, text): |
| 105 | + """Assert the current page title contains the given text.""" |
| 106 | + self._require_page() |
| 107 | + actual = self._page.title() |
| 108 | + assert text in actual, f"Title {actual!r} does not contain {text!r}" |
| 109 | + |
| 110 | + def heading_should_contain(self, name, text): |
| 111 | + """Assert a heading with the given name contains text.""" |
| 112 | + self._require_page() |
| 113 | + heading = self._page.get_by_role("heading", name=name) |
| 114 | + content = heading.text_content() |
| 115 | + assert text in content, f"Heading text {content!r} does not contain {text!r}" |
| 116 | + |
| 117 | + # ── Interactions ────────────────────────────────────────────────────────── |
| 118 | + |
| 119 | + def click_link(self, name): |
| 120 | + """Click a link by its visible text. |
| 121 | +
|
| 122 | + Example: |
| 123 | + | Click Link | Checkboxes | |
| 124 | + """ |
| 125 | + self._require_page() |
| 126 | + self._page.get_by_role("link", name=name).click() |
| 127 | + |
| 128 | + def select_option_by_label(self, selector, label): |
| 129 | + """Select a dropdown option by its visible label. |
| 130 | +
|
| 131 | + Example: |
| 132 | + | Select Option By Label | #dropdown | Option 1 | |
| 133 | + """ |
| 134 | + self._require_page() |
| 135 | + self._page.locator(selector).select_option(label=label) |
| 136 | + |
| 137 | + # ── Checkboxes ──────────────────────────────────────────────────────────── |
| 138 | + |
| 139 | + def checkbox_should_be_unchecked(self, index=0): |
| 140 | + """Assert the Nth checkbox (0-based) is unchecked.""" |
| 141 | + self._require_page() |
| 142 | + checkboxes = self._page.get_by_role("checkbox") |
| 143 | + cb = checkboxes.nth(int(index)) |
| 144 | + assert not cb.is_checked(), f"Checkbox {index} should be unchecked" |
| 145 | + |
| 146 | + def checkbox_should_be_checked(self, index=0): |
| 147 | + """Assert the Nth checkbox (0-based) is checked.""" |
| 148 | + self._require_page() |
| 149 | + checkboxes = self._page.get_by_role("checkbox") |
| 150 | + cb = checkboxes.nth(int(index)) |
| 151 | + assert cb.is_checked(), f"Checkbox {index} should be checked" |
| 152 | + |
| 153 | + def check_checkbox(self, index=0): |
| 154 | + """Check the Nth checkbox (0-based).""" |
| 155 | + self._require_page() |
| 156 | + self._page.get_by_role("checkbox").nth(int(index)).check() |
| 157 | + |
| 158 | + def uncheck_checkbox(self, index=0): |
| 159 | + """Uncheck the Nth checkbox (0-based).""" |
| 160 | + self._require_page() |
| 161 | + self._page.get_by_role("checkbox").nth(int(index)).uncheck() |
| 162 | + |
| 163 | + # ── Screenshot ──────────────────────────────────────────────────────────── |
| 164 | + |
| 165 | + def take_page_screenshot(self, path="screenshot.png"): |
| 166 | + """Save a screenshot of the current page. |
| 167 | +
|
| 168 | + Example: |
| 169 | + | Take Page Screenshot | output/my_test.png | |
| 170 | + """ |
| 171 | + self._require_page() |
| 172 | + self._page.screenshot(path=path) |
| 173 | + |
| 174 | + def take_device_screenshot(self, path="device_screenshot.png"): |
| 175 | + """Save a full-device screenshot. |
| 176 | +
|
| 177 | + Example: |
| 178 | + | Take Device Screenshot | output/device.png | |
| 179 | + """ |
| 180 | + self._require_device() |
| 181 | + self._device.screenshot(path=path) |
| 182 | + |
| 183 | + # ── BrowserStack session status ─────────────────────────────────────────── |
| 184 | + |
| 185 | + def set_session_name(self, name): |
| 186 | + """Set the BrowserStack session name.""" |
| 187 | + self._require_page() |
| 188 | + self._page.evaluate( |
| 189 | + "_ => {}", |
| 190 | + f"browserstack_executor: {json.dumps({'action': 'setSessionName', 'arguments': {'name': name}})}", |
| 191 | + ) |
| 192 | + |
| 193 | + def mark_session_as_passed(self, reason="Test passed"): |
| 194 | + """Mark the BrowserStack session as passed.""" |
| 195 | + self._set_session_status("passed", reason) |
| 196 | + |
| 197 | + def mark_session_as_failed(self, reason="Test failed"): |
| 198 | + """Mark the BrowserStack session as failed.""" |
| 199 | + self._set_session_status("failed", reason) |
| 200 | + |
| 201 | + def _set_session_status(self, status, reason): |
| 202 | + self._require_page() |
| 203 | + self._page.evaluate( |
| 204 | + "_ => {}", |
| 205 | + f"browserstack_executor: {json.dumps({'action': 'setSessionStatus', 'arguments': {'status': status, 'reason': reason}})}", |
| 206 | + ) |
| 207 | + |
| 208 | + # ── Teardown ────────────────────────────────────────────────────────────── |
| 209 | + |
| 210 | + def disconnect_device(self): |
| 211 | + """Close the browser, disconnect the device, and stop Playwright.""" |
| 212 | + self.close_browser() |
| 213 | + if self._device: |
| 214 | + self._device.close() |
| 215 | + self._device = None |
| 216 | + if self._pw: |
| 217 | + self._pw.stop() |
| 218 | + self._pw = None |
| 219 | + |
| 220 | + # ── Internal ────────────────────────────────────────────────────────────── |
| 221 | + |
| 222 | + def _require_device(self): |
| 223 | + if self._device is None: |
| 224 | + raise RuntimeError("No device connected. Call 'Connect To BrowserStack' first.") |
| 225 | + |
| 226 | + def _require_page(self): |
| 227 | + if self._page is None: |
| 228 | + raise RuntimeError("No page open. Call 'Launch Browser' first.") |
0 commit comments