• Stars
    star
    220
  • Rank 180,422 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Playwright client for Ruby

Gem Version

🎭 Playwright client for Ruby

Docs | API

Getting Started

gem 'playwright-ruby-client'

and then 'bundle install'.

Since playwright-ruby-client doesn't include the playwright driver, we have to install playwright in advance.

npm install playwright
./node_modules/.bin/playwright install

And set playwright_cli_executable_path: './node_modules/.bin/playwright'

Prefer playwrighting without Node.js?

Instead of npm, you can also directly download playwright driver from playwright.azureedge.net/builds/. The URL can be easily detected from here

Capture a site

require 'playwright'

Playwright.create(playwright_cli_executable_path: './node_modules/.bin/playwright') do |playwright|
  playwright.chromium.launch(headless: false) do |browser|
    page = browser.new_page
    page.goto('https://github.com/YusukeIwaki')
    page.screenshot(path: './YusukeIwaki.png')
  end
end

image

Simple scraping

require 'playwright'

Playwright.create(playwright_cli_executable_path: './node_modules/.bin/playwright') do |playwright|
  playwright.chromium.launch(headless: false) do |browser|
    page = browser.new_page
    page.goto('https://github.com/')

    form = page.query_selector("form.js-site-search-form")
    search_input = form.query_selector("input.header-search-input")
    search_input.click
    page.keyboard.type("playwright")
    page.expect_navigation {
      page.keyboard.press("Enter")
    }

    list = page.query_selector("ul.repo-list")
    items = list.query_selector_all("div.f4")
    items.each do |item|
      title = item.eval_on_selector("a", "a => a.innerText")
      puts("==> #{title}")
    end
  end
end
$ bundle exec ruby main.rb
==> microsoft/playwright
==> microsoft/playwright-python
==> microsoft/playwright-cli
==> checkly/headless-recorder
==> microsoft/playwright-sharp
==> playwright-community/jest-playwright
==> microsoft/playwright-test
==> mxschmitt/playwright-go
==> microsoft/playwright-java
==> MarketSquare/robotframework-browser

Android browser automation

require 'playwright'

Playwright.create(playwright_cli_executable_path: './node_modules/.bin/playwright') do |playwright|
  devices = playwright.android.devices
  unless devices.empty?
    device = devices.last
    begin
      puts "Model: #{device.model}"
      puts "Serial: #{device.serial}"
      puts device.shell('ls /system')

      device.launch_browser do |context|
        page = context.pages.first
        page.goto('https://github.com/YusukeIwaki')
        page.click('header button')
        page.click('input[name="q"]')
        page.keyboard.type('puppeteer')
        page.expect_navigation {
          page.keyboard.press('Enter')
        }
        page.screenshot(path: 'YusukeIwaki.android.png')
      end
    ensure
      device.close
    end
  end
end

android-browser

Android native automation

We have to download android-driver for Playwright in advance.

wget https://github.com/microsoft/playwright/raw/master/bin/android-driver-target.apk -O /path/to/playwright-driver/package/bin/android-driver-target.apk
wget https://github.com/microsoft/playwright/raw/master/bin/android-driver.apk -O /path/to/playwright-driver/package/bin/android-driver.apk

(If you downloaded Playwright via npm, replace /path/to/playwright-driver/package/ with ./node_modules/playwright/ above.)

require 'playwright'

Playwright.create(playwright_cli_executable_path: ENV['PLAYWRIGHT_CLI_EXECUTABLE_PATH']) do |playwright|
  devices = playwright.android.devices
  unless devices.empty?
    device = devices.last
    begin
      device.shell('input keyevent POWER')
      device.shell('input keyevent POWER')
      device.shell('input keyevent 82')
      sleep 1
      device.shell('cmd statusbar expand-notifications')

      # pp device.tree
      # pp device.info(res: 'com.android.systemui:id/clock')
      device.tap_on(res: 'com.android.systemui:id/clock')
    ensure
      device.close
    end
  end
end

Communicate with Playwright server

If your environment doesn't accept installing browser or creating browser process, consider separating Ruby client and Playwright server.

structure

For launching Playwright server, just execute:

npx playwright install && npx playwright run-server --port 8080

and we can connect to the server with the code like this:

Playwright.connect_to_playwright_server('ws://127.0.0.1:8080') do |playwright|
  playwright.chromium.launch do |browser|
    page = browser.new_page
    page.goto('https://github.com/YusukeIwaki')
    page.screenshot(path: './YusukeIwaki.png')
  end
end

When Playwright.connect_to_playwright_server is used, playwright_cli_executable_path is not required.

For more detailed instraction, refer this article: https://playwright-ruby-client.vercel.app/docs/article/guides/playwright_on_alpine_linux

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Playwright project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

puppeteer-ruby

A Ruby port of Puppeteer
Ruby
245
star
2

capybara-playwright-driver

Playwright driver for Capybara
Ruby
74
star
3

dart-kotlin_flavor

Dart
30
star
4

playwright-python-remote

Enables us to use playwright-python on pure-Python environment
Python
18
star
5

puppeteer-ruby-example

Ruby
7
star
6

realm-java-helpers

Realm#close() を意識しないようにするためのヘルパー
Java
7
star
7

rack-test_server

Simple HTTP server launcher for Rack application (Sinatra, Rails, etc...)
Ruby
5
star
8

alwaysDRINK_MyLauncher_flutter

always DRINKでいい日々を。(Flutter版)
Dart
3
star
9

puppeteer-dart-page-walker

Dart
3
star
10

data_class_factory

Backport `Data.define` for Ruby 2.7, 3.0, 3.1
Ruby
3
star
11

GitHub-Issue-Viewer-2

Java
2
star
12

cloud-pine

HTML
2
star
13

relax2

relax2: Quick and dirty HTTP API client factory for Ruby
Ruby
2
star
14

appetize-cli

Go
1
star
15

homebrew-kotlin-native

unofficial Kotlin Native formula just for me :)
Ruby
1
star
16

realm-object-server-centos6

1
star
17

ggrks-puppeteer-js

JavaScript
1
star
18

capybara-minimum-examples

Ruby
1
star
19

e2e_testing_on_rails

An utility library for E2E testing on Rails.
Ruby
1
star
20

aac-paging-library-playground

PagedListAdapter, PageKeyedDataSource, with displaying loading progress.
Kotlin
1
star
21

freeradius-docker-playground

Dockerfile
1
star
22

always_MyLauncher

always.fan でいい日々を。
Kotlin
1
star
23

slack_cli

slack_cli(Slackボットキット)のnpm公開用リポジトリ
JavaScript
1
star
24

azurebot-playground

Ruby
1
star
25

MaterialButtonToggleGroupPlayground

https://qiita.com/YusukeIwaki/items/385dbe1abfafc3e3bf1b のお試しソース
Java
1
star
26

oreore-ios-mdm

Ruby
1
star