RFBrowser в Robot Framework
| Введение | |
| Установка | |
| Запустить браузер в видимом режиме | |
| xpath | |
| Пример: Верифицировать заголовок страницы | |
| ElementState: все возможные состояния элемента | |
| Get Element States | |
| Похожие статьи |
Введение
RFBrowser - это новый встроенный в RobotFramework драйвер для браузеров на основе
Playwright
Официальная документация
Установка
PowerShell скрипт для полной установки RFBrowser вы можете найти в конце статьи
Команда для установки с помощью pip
python -m pip install robotframework-browser
Пример установки с нуля
python -m venv venv
source venv/bin/activate
python -m pip install --upgrade pip
python -m pip install robotframework
python -m pip install robotframework-browser
rfbrowser init
Затем нужно установить
Node.js
минимум 12-й версии. О том как это сделать в
Linux
читайте в статье
«Установить последнюю версию nodejs»
Про установку в
Windows
читайте
здесь
Запустить браузер в видимом режиме
Чтобы запустить RFBrowser в видимом режиме нужно установить опцию headless=False
New Browser browser=chromium headless=False
xpath
# По именам классов Click xpath=//span[@class="name1 name2 name3"] # По url Wait For Elements State xpath=//a[@href="${your_url}"] visible
Пример: Верифицировать заголовок страницы
*** Settings *** Documentation Example that opens single page Library Browser ... enable_playwright_debug=${True} ... auto_closing_level=TEST ... retry_assertions_for=0:00:03 *** Variables *** ${url} https://eth1.ru *** Keywords *** Start Chromium Browser New Browser browser=chromium headless=False New Context viewport={'width': 1920, 'height': 1080} ignoreHTTPSErrors=True *** Test Cases *** Starting a browser with a page Start Chromium Browser New Page https://eth1.ru Get Title == eth1.ru Close Browser
ElementState
Все возможные состояния, которые может иметь элемент.
Докуметация
Allowed Values attached detached visible hidden enabled disabled editable readonly selected deselected focused defocused checked unchecked stable
Get Element States
Проверять
статус элемента
теперь нужно с помощью ключевого слова Get Element States
Например, если нужно проверить видимость элемента с классом devhops
*** Settings *** Documentation Example that opens single page Library Browser ... enable_playwright_debug=${True} ... auto_closing_level=TEST ... retry_assertions_for=0:00:03 *** Variables *** ${url} https://eth1.ru *** Keywords *** Start Chromium Browser New Browser browser=chromium headless=False New Context viewport={'width': 1920, 'height': 1080} ignoreHTTPSErrors=True Verify Element Visibility [Arguments] ${selector} ${visible} = Get Element States ${selector} then bool(value & visible) Should Be True ${visible} *** Test Cases *** Verify devhops Visibility Start Chromium Browser New Page https://eth1.ru Verify Element Visibility .devhops Close Browser
В этом примере мы передаём аргумент selector в кейворд Verify Element Visibility.
Подробнее про это можно прочитать в статье
«Передача аргументов»
Скрипт для установки RFBrowser
С помощью PowerShell скрипта можно установить все необходимые компоненты для работы RFBrowswer, включая Node.js
Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Starting rfbrowser setup" ) -ForegroundColor Cyan if (Test-Path ".\venv") { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " venv already exists" ) -ForegroundColor Yellow } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " venv does not exist, creating..." ) -ForegroundColor Cyan python -m venv venv } if ($env:VIRTUAL_ENV -eq "${PWD}\venv") { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " venv is already activated" ) -ForegroundColor Cyan echo $env:VIRTUAL_ENV echo "${PWD}\venv" } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " venv is not activated" ) -ForegroundColor Cyan echo $env:VIRTUAL_ENV echo "${PWD}\venv" .\venv\Scripts\activate } python -m pip install --upgrade pip python -m pip install robotframework python -m pip install robotframework-browser Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Trying to init rfbrowser" ) -ForegroundColor Cyan try { rfbrowser init } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " WARNING: Failed to init rfbrowser." ) -ForegroundColor Yellow } Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " To activate the venv, run:" ) -ForegroundColor Cyan Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " .\venv\Scripts\activate" ) -ForegroundColor Yellow # ----------------------------- # Node.js installer setup # ----------------------------- $nodeUrl = ( "https://nodejs.org/dist/v24.15.0/" + "node-v24.15.0-x64.msi" ) $installerPath = Join-Path (Get-Location) ` "node-v24.15.0-x64.msi" function node_is_in_path { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Verifying installation using PATH (node -v, npm -v)..." ) -ForegroundColor Cyan try { node -v npm -v Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Primary verification succeeded." ) -ForegroundColor Green return $true } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " WARNING: Primary verification failed. PATH may not be updated." ) -ForegroundColor Yellow return $false } } function node_exe_works { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Trying full-path verification: node.exe -v" ) -ForegroundColor Cyan try { & "C:\Program Files\nodejs\node.exe" -v Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Full-path verification succeeded." ) -ForegroundColor Green return $true } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " WARNING: Full-path verification failed." ) -ForegroundColor Yellow return $false } } function nodejs_dir_exists { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Checking if C:\Program Files\nodejs exists..." ) -ForegroundColor Cyan try { Get-ChildItem "C:\Program Files\nodejs" | Out-Null Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Node.js directory found." ) -ForegroundColor Cyan return $true } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " WARNING: Node.js directory not found." ) -ForegroundColor Red Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Verify that you are running PowerShell as Administrator." ) -ForegroundColor Yellow exit 1 } } function reload_path { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Reloading PATH environment variable..." ) -ForegroundColor Cyan $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " PATH successfully reloaded for this session." ) -ForegroundColor Green } # ----------------------------- # Step 1: Download installer # ----------------------------- if (Test-Path $installerPath) { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Installer already exists: $installerPath" ) -ForegroundColor Yellow } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Downloading Node.js installer..." ) -ForegroundColor Cyan try { Invoke-WebRequest -Uri $nodeUrl ` -OutFile $installerPath -ErrorAction Stop Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Download completed: $installerPath" ) -ForegroundColor Green } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " ERROR: Failed to download Node.js installer." ) -ForegroundColor Red exit 1 } } # ----------------------------- # Step 2: Install Node.js # ----------------------------- try { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Installing Node.js silently..." ) -ForegroundColor Cyan Start-Process "msiexec.exe" ` -ArgumentList "/i `"$installerPath`" /qn /norestart" ` -Wait -ErrorAction Stop Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Node.js installation completed." ) -ForegroundColor Green } catch { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " ERROR: Node.js installation failed." ) -ForegroundColor Red exit 1 } # ----------------------------- # Step 3: PATH verification # ----------------------------- $node_installed_and_in_path = node_is_in_path if ($node_installed_and_in_path) { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Installation verified successfully." ) -ForegroundColor Green exit 0 } # ----------------------------- # Step 4: Full-path fallback # ----------------------------- $node_installed = node_exe_works if ($node_installed) { reload_path } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Full-path verification failed, checking directory..." ) -ForegroundColor Yellow $node_dir_exists = nodejs_dir_exists if ($node_dir_exists) { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Node.js directory exists." ) -ForegroundColor Cyan } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " ERROR: Node.js directory missing." ) -ForegroundColor Red } exit 1 } # ----------------------------- # Step 5: Verify PATH reload # ----------------------------- $node_installed_and_in_path = node_is_in_path if ($node_installed_and_in_path) { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " Installation verified after PATH update." ) -ForegroundColor Green exit 0 } else { Write-Host ( (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " ERROR: PATH update did not help." ) -ForegroundColor Red exit 1 }
Автор статьи: Андрей Олегович
| RFBrowser | |
| Библиотеки | |
| Ошибки RFBrowser | |
| Playwright |