Playwright

Introduction

Installation

Let us look at the installation for various environments.

The first thing to understand is that either way you will need NodeJS and NPM

If the tests are written in JavaScript then there is almost nothing else to install.

If the tests are written in Python then Playwright can be installed with Pip , this case is covered here

The case with tests in RobotFramework is covered here

Nodejs

sudo apt -y install nodejs
sudo apt -y install npm
npm i playwright

Most likely NodeJS will be too old and you will get an error so a newer version is needed

A newer version of nodejs can be installed as follows

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

With playwright there is no need to bother with downloading webdrivers and adding them to PATH as it was with selenium

Let us look at each step separately (skip)

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

Installing RobotFrameworkBrowser

The newest browser in RobotFramework is based on 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

By default Playwright runs in background (headless) mode.

To see the browser, add the option headless=False

with sync_playwright() as p: browser = p.chromium.launch(headless=False)

All elements of a certain class

To find all elements of a certain class and print the text they contain, you need page.query_selector_all and inner_text()

websites = page.query_selector_all('[class="websites"]') for site in websites: print(site.inner_text())

An example of checking a heading in Robot

Let us use Browser based on Playwright from RobotFramework .

Read more about using Browser in RobotFramework in the article 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

Article author: Andrei Olegovich

Banner image