Playwright
Введение | |
Установка | |
Установка nodejs и тесты на JS | |
Установка для Python тестов | |
Установка RobotFrameworkBrowser | |
Headed mode | |
Все элементы определённого класса | |
Пример в Robot | |
Похожие статьи |
Введение
Установка
Рассмотрим установку для различных окружений.
Первое, что нужно понять - в любом случае понадобятся
NodeJS
и
NPM
Если тесты будут написаны на
JavaScript
то дальше почти ничего устанавливать не нужно.
Если тесты будут написаны на
Python
то устанавливать Playwright можно через
Pip
, этот случай рассмотрен
здесь
Случай с тестами на
RobotFramework
рассмотрен
здесь
Nodejs
sudo apt -y install nodejs
sudo apt -y install npm
npm i playwright
Скорее всего NodeJS будет слишком старым и будет ошибка поэтому нужна более свежая версия
Более новую версию nodejs можно установить следующим образом
curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
chmod + x nodesource_setup.sh
sudo ./nodesource_setup.sh
apt install nodejs
RUN apt-get install curl \ && curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh \ && bash nodesource_setup.sh \ && apt-get install nodejs
Step 18/18 : RUN npx playwright install chromium --with-deps ---> Running in ec3f57a0348b npm WARN exec The following package was not found and will be installed: playwright ╔═══════════════════════════════════════════════════════════════════════════════╗ ║ WARNING: It looks like you are running 'npx playwright install' without first ║ ║ installing your project's dependencies. ║ ║ ║ ║ To avoid unexpected behavior, please install your dependencies first, and ║ ║ then run Playwright's install command: ║ ║ ║ ║ npm install ║ ║ npx playwright install ║ ║ ║ ║ If your project does not yet depend on Playwright, first install the ║ ║ applicable npm package (most commonly @playwright/test), and ║ ║ then run Playwright's install command to download the browsers: ║ ║ ║ ║ npm install @playwright/test ║ ║ npx playwright install ║ ║ ║ ╚═══════════════════════════════════════════════════════════════════════════════╝
Python
python -m pip install --upgrade pip
python -m pip install playwright
playwright install
C playwright не нужно заморачиваться со скачиванием webdirver-ов и добавлением из в PATH как это было в selenium
Рассмотрим каждый шаг в отдельности (пропустить)
python -m pip install --upgrade pip
Requirement already satisfied: pip in /home/andrei/test/robotframework/venv/lib/python3.9/site-packages (21.3.1)
python -m pip install playwright
Collecting playwright Downloading playwright-1.17.2-py3-none-manylinux1_x86_64.whl (33.3 MB) |████████████████████████████████| 33.3 MB 6.4 MB/s Collecting greenlet>=1.0.0 Downloading greenlet-1.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (153 kB) |████████████████████████████████| 153 kB 3.3 MB/s Collecting websockets>=8.1 Downloading websockets-10.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (110 kB) |████████████████████████████████| 110 kB 2.8 MB/s Collecting pyee>=8.0.1 Downloading pyee-8.2.2-py2.py3-none-any.whl (12 kB) Installing collected packages: websockets, pyee, greenlet, playwright Successfully installed greenlet-1.1.2 playwright-1.17.2 pyee-8.2.2 websockets-10.1
playwright install
Downloading Playwright build of chromium v939194 - 128.9 Mb [====================] 100% 0.0s Playwright build of chromium v939194 downloaded to /home/andrei/.cache/ms-playwright/chromium-939194 Downloading Playwright build of ffmpeg v1006 - 2.6 Mb [====================] 100% 0.0s Playwright build of ffmpeg v1006 downloaded to /home/andrei/.cache/ms-playwright/ffmpeg-1006 Downloading Playwright build of firefox v1304 - 72.7 Mb [====================] 100% 0.0s Playwright build of firefox v1304 downloaded to /home/andrei/.cache/ms-playwright/firefox-1304 Downloading Playwright build of webkit v1578 - 76.7 Mb [====================] 100% 0.0s Playwright build of webkit v1578 downloaded to /home/andrei/.cache/ms-playwright/webkit-1578
Установка RobotFrameworkBrowser
Новейший браузер в RobotFramework основан на Playwright
python -m pip install --upgrade pip
python -m pip install robotframework-browser
Collecting robotframework-browser Downloading robotframework_browser-11.0.0-py3-none-any.whl (205 kB) |████████████████████████████████| 205 kB 9.5 MB/s Collecting robotframework-assertion-engine>=0.2.0 Downloading robotframework_assertion_engine-0.2.0-py3-none-any.whl (6.4 kB) Collecting grpcio==1.42.0 Downloading grpcio-1.42.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB) |████████████████████████████████| 4.0 MB 27.7 MB/s Collecting wrapt>=1.12.1 Downloading wrapt-1.13.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (81 kB) |████████████████████████████████| 81 kB 11.7 MB/s Collecting overrides>=4.1.0 Downloading overrides-6.1.0-py3-none-any.whl (14 kB) Requirement already satisfied: robotframework-pythonlibcore>=3.0.0 in /home/andrei/test/robotframework/venv/lib/python3.9/site-packages (from robotframework-browser) (3.0.0) Collecting backports.cached-property>=1.0.1 Downloading backports.cached_property-1.0.1-py3-none-any.whl (5.7 kB) Collecting protobuf==3.19.1 Downloading protobuf-3.19.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB) |████████████████████████████████| 1.1 MB 21.0 MB/s Collecting typing-extensions>=3.7.4.3 Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB) Requirement already satisfied: robotframework>=4.0.3 in /home/andrei/test/robotframework/venv/lib/python3.9/site-packages (from robotframework-browser) (4.1.2) Collecting grpcio-tools==1.42.0 Downloading grpcio_tools-1.42.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB) |████████████████████████████████| 2.4 MB 31.4 MB/s Requirement already satisfied: six>=1.5.2 in /home/andrei/test/robotframework/venv/lib/python3.9/site-packages (from grpcio==1.42.0->robotframework-browser) (1.16.0) Requirement already satisfied: setuptools in /home/andrei/test/robotframework/venv/lib/python3.9/site-packages (from grpcio-tools==1.42.0->robotframework-browser) (56.0.0) Collecting typing-utils>=0.0.3 Downloading typing_utils-0.1.0-py3-none-any.whl (10 kB) Installing collected packages: typing-utils, protobuf, grpcio, wrapt, typing-extensions, robotframework-assertion-engine, overrides, grpcio-tools, backports.cached-property, robotframework-browser Successfully installed backports.cached-property-1.0.1 grpcio-1.42.0 grpcio-tools-1.42.0 overrides-6.1.0 protobuf-3.19.1 robotframework-assertion-engine-0.2.0 robotframework-browser-11.0.0 typing-extensions-4.0.1 typing-utils-0.1.0 wrapt-1.13.3
Headed mode
Playwright по умолчанию запускается в фоновом (headless) режиме.
Чтобы видеть браузер добавьте опцию headless=False
with sync_playwright() as p: browser = p.chromium.launch(headless=False)
Все элементы определённого класса
Чтобы найти все элементы определённого класса и вывести содержащийся в них текст нужны page.query_selector_all и inner_text()
websites = page.query_selector_all('[class="websites"]') for site in websites: print(site.inner_text())
Пример проверки заголовка в Robot
Применим Browser на основе Playwright из
RobotFramework
.
Подробнее про применение Browser в
RobotFramework читайте в статье
RFBrowser
*** Settings *** Documentation Testing Browser Library Browser *** Variables *** ${url}= https://topbicycle.ru *** Keywords *** Start TestCase Log To Console "\nStarting Test Case\n" Open Website New Page ${url} Get Text h1 == Online магазин велосипедов Finish TestCase Log To Console "\nFinishing Test Case\n" *** Test Cases *** Verify that browser works [Documentation] This test case verifies that url can be opened [Tags] Functional Start TestCase Open Website Finish TestCase
robot andrei.robot
============================================================================== Andrei :: Testing Browser ============================================================================== Verify that browser works :: This test case verifies that url can ... " Starting Test Case " .." Finishing Test Case " Verify that browser works :: This test case verifies that url can ... | PASS | ------------------------------------------------------------------------------ Andrei :: Testing Browser | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html