playwright

package module
v0.0.0-...-ee369fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

⚠️ This is currently an alpha version

This is not supported by the k6 team, and is worked on by a(n) individual contributor(s). It may also break in the future as both xk6 and playwright-go evolve. Any issues with the tool should be raised here.

It is not production ready yet, but definitely works as intended! Please enjoy the tool!



xk6-playwright

xk6 playwright


k6 extension that adds support for browser automation and end-to-end web testing using playwright-go

Special thanks to all the contributors over at k6 and playwright-go

Here's to open source!

GitHub license Go Report Card GitHub license




Origin Story

This project was inspired by xk6-browser. Having seen the release we were excited to play around with the tool, but while using it we ran into some issues around context, page navigation, typing and button clicks. Having previously worked with playwright-go we thought it would be a great idea to create an extension around this so we had something we know would work to our liking. Thus xk6 playwright was born!

We totally understand at the time of writing this that xk6-browser is not yet production ready, and that it is currently an early beta that has been released to the public that will evolve and get better over time. However, we still saw validity in creating this extension aiming to support something we have used and know works to our liking. Competition is not intended, we just want to make cool things that help us do our jobs!


Build from source

To build a k6 binary with this extension, first ensure you have the prerequisites:

  • Go toolchain
  • Git
  • Clone the k6 repository (NOTE: make sure you are in the k6 repo base directory before attempting the below steps)

Then:

  1. Install xk6:
go install go.k6.io/xk6/cmd/xk6@latest
  1. Build the binary:
xk6 build --output xk6-playwright --with github.com/wosp-io/xk6-playwright

on Windows:

xk6 build --output xk6-playwright.exe --with github.com/wosp-io/xk6-playwright

This will create a xk6-playwright binary file in the current working directory. This file can be used exactly the same as the main k6 binary, with the addition of being able to run xk6-playwright scripts.

  1. For the tool to work, the browsers and OS dependencies must be installed locally. You can use the following command for this:
go run github.com/playwright-community/playwright-go/cmd/playwright@latest install --with-deps
  1. Run scripts that import k6/x/playwright with the new xk6-playwright binary. On Linux and macOS make sure this is done by referencing the file in the current directory, e.g. ./xk6-playwright run <script>, or you can place it somewhere in your PATH so that it can be run from anywhere on your system.

Install and Run with Docker

Install:

docker pull nickvuono/xk6-playwright

Run Test Script:

docker run --rm -i nickvuono/xk6-playwright run script.js

Make sure to pass headless args or options when launching, and set it to true when running tests using Docker


Simplest Working Example

import pw from 'k6/x/playwright';

export default function () {
  pw.launch()
  pw.newPage()
  pw.goto("https://www.google.com/", {waitUntil: 'networkidle'})
  pw.waitForSelector("input[title='Search']", {state: 'visible'})
  pw.kill()
}

Monitor Real User Metrics

import pw from 'k6/x/playwright';

export default function () {
  pw.launch()
  pw.newPage()
  pw.goto("https://www.google.com/")
  pw.waitForSelector("input[title='Search']", {state: 'visible'})
  pw.type("input[title='Search']", "how to measure real user metrics with the xk6-playwright extension for k6?")

  //print out real user metrics of the google search page
  console.log(`First Paint: ${pw.firstPaint()}ms`)
  console.log(`First Contentful Paint: ${pw.firstContentfulPaint()}ms`)
  console.log(`Time to Minimally Interactive: ${pw.timeToMinimallyInteractive()}ms`)
  console.log(`First Input Delay: ${pw.firstInputDelay()}ms`)

  pw.kill()
}

Currently Supported Actions

Playwright API coverage is as follows:

Action Encompassed Playwright Function(s) Description
launch() Run() & Launch() starts playwright client and launches Chromium browser
connect() Run() & Connect() attaches playwright client to existing browser instance
newPage() NewPage() opens up a new page within the browser
goto() Goto() navigates to a specified url
waitForSelector() WaitForSelector() waits for an element to be on the page based on the provided selector
click() Click() clicks an element on the page based on the provided selector
type() Type() types in an 'input' element on the page based on the provided selector and string to be entered
pressKey() PressKey() simulates pressing a key, types in an 'input' element on the page based on a key to be entered
sleep() Sleep() waits for a specified amount of time in milliseconds
screenshot() Screenshot() attempts to take and save a png image of the current screen
focus() Focus() focuses a spcific element based on the provided selector
fill() Fill() fills an 'input' element on the page based on the provided selector and string to be entered
selectOptions() SelectOption() selects an 'input' element from a list or dropdown of options on the page based on the provided selector and values to be selected
check() Check() checks an element on the page based on the provided selector
uncheck() Uncheck() unchecks an element on the page based on the provided selector
dragAndDrop() DragAndDrop() drag an item from one place to another based on two selectors
evaluate() Evaluate() evaluate an expresion or function and get the return value
reload() Reload() reloads the current page
cookies() Cookies() get all the cookies available for the default browser context.
firstPaint() N/A this function is unique to xk6-playwright What is First Paint? captures the first paint metric of the current page milliseconds
firstContentfulPaint() N/A this function is unique to xk6-playwright What is First Contentful Paint? captures the first contentful paint metric of the current page milliseconds
timeToMinimallyInteractive() N/A this function is unique to xk6-playwright - This is based on the first input registerd on the current page - NOTE: this is how we personally like to determine when a page is minimally interactive. captures the time to minimally interactive metric of the current page milliseconds
firstInputDelay() N/A this function is unique to xk6-playwright What is First Input Delay? captures the first input delay metric of the current page in milliseconds

The above 'Encompassed Playwright Function(s)' will link to the playwright-go package documentation to give an in-depth overview of how these functions will behave from a low-level perspective.

If you would like a high-level perspective on how these actions work you will be better served with the Playwright API Documentation


Contributing

  1. Fork it (https://github.com/your-github-user/xk6-playwright/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Nick Vuono - creator and maintainer

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReportError

func ReportError(err error, msg string)

ReportError reports an error if it is not nil

Types

type Playwright

type Playwright struct {
	Self           *playwright.Playwright
	Browser        playwright.Browser
	BrowserContext playwright.BrowserContext
	Page           playwright.Page
}

Playwright is the k6 extension for a playwright-go client.

func (*Playwright) Check

func (p *Playwright) Check(selector string, opts playwright.FrameCheckOptions) error

Check wrapper around playwright check page function that takes in a selector and a set of options

func (*Playwright) Click

func (p *Playwright) Click(selector string, opts playwright.PageClickOptions) error

Click wrapper around playwright click page function that takes in a selector and a set of options

func (*Playwright) Connect

func (p *Playwright) Connect(url string, args playwright.BrowserTypeConnectOverCDPOptions) error

Connect attaches Playwright to an existing browser instance

func (*Playwright) Cookies

func (p *Playwright) Cookies() []*playwright.BrowserContextCookiesResult

Cookies wrapper around playwright cookies fetch function

func (*Playwright) CountAll

func (p *Playwright) CountAll(selector string) (int32, error)

func (*Playwright) CountByState

func (p *Playwright) CountByState(selector string, state string) (int32, error)

func (*Playwright) DragAndDrop

func (p *Playwright) DragAndDrop(sourceSelector string, targetSelector string, opts playwright.FrameDragAndDropOptions) error

DragAndDrop wrapper around playwright draganddrop page function that takes in two selectors(source and target) and a set of options

func (*Playwright) Evaluate

func (p *Playwright) Evaluate(expression string, opts playwright.PageEvaluateOptions) interface{}

Evaluate wrapper around playwright evaluate page function that takes in an expresion and a set of options and evaluates the expression/function returning the resulting information.

func (*Playwright) Fill

func (p *Playwright) Fill(selector string, filledString string, opts playwright.FrameFillOptions) error

Fill wrapper around playwright fill page function that takes in a selector, text, and a set of options

func (*Playwright) FirstContentfulPaint

func (p *Playwright) FirstContentfulPaint() uint64

FirstContentfulPaint function that gathers the Real User Monitoring Metrics for First Contentful Paint of the current page

func (*Playwright) FirstInputDelay

func (p *Playwright) FirstInputDelay() uint64

FirstInputDelay function that gathers the Real User Monitoring Metrics for First Input Delay of the current page

func (*Playwright) FirstPaint

func (p *Playwright) FirstPaint() uint64

FirstPaint function that gathers the Real User Monitoring Metrics for First Paint of the current page

func (*Playwright) Focus

func (p *Playwright) Focus(selector string, opts playwright.PageFocusOptions) error

Focus wrapper around playwright focus page function that takes in a selector and a set of options

func (*Playwright) Goto

func (p *Playwright) Goto(url string, opts playwright.PageGotoOptions) error

Goto wrapper around playwright goto page function that takes in a url and a set of options

func (*Playwright) Kill

func (p *Playwright) Kill() error

Kill closes browser instance and stops puppeteer client

func (*Playwright) Launch

func (p *Playwright) Launch(args playwright.BrowserTypeLaunchOptions) error

Launch starts the playwright client and launches a browser

func (*Playwright) LaunchPersistent

func (p *Playwright) LaunchPersistent(dir string, args playwright.BrowserTypeLaunchPersistentContextOptions) error

LaunchPersistent starts the playwright client and launches a browser with a persistent context

func (*Playwright) NewPage

func (p *Playwright) NewPage() error

NewPage opens a new page within the browser

func (*Playwright) PressKey

func (p *Playwright) PressKey(selector string, key string, opts playwright.PagePressOptions) error

PressKey wrapper around playwright Press page function that takes in a selector, key, and a set of options

func (*Playwright) Reload

func (p *Playwright) Reload() error

Reload wrapper around playwright reload page function

func (*Playwright) Screenshot

func (p *Playwright) Screenshot(filename string, perm fs.FileMode, opts playwright.PageScreenshotOptions) error

Screenshot wrapper around playwright screenshot page function that attempts to take and save a png image of the current screen.

func (*Playwright) SelectOptions

func (p *Playwright) SelectOptions(selector string, values playwright.SelectOptionValues, opts playwright.FrameSelectOptionOptions) error

SelectOptions wrapper around playwright selectOptions page function that takes in a selector, values, and a set of options

func (*Playwright) Sleep

func (p *Playwright) Sleep(time float64)

Sleep wrapper around playwright waitForTimeout page function that sleeps for the given `timeout` in milliseconds

func (*Playwright) TimeToMinimallyInteractive

func (p *Playwright) TimeToMinimallyInteractive() uint64

TimeToMinimallyInteractive function that gathers the Real User Monitoring Metrics for Time to Minimally Interactive of the current page (based on the first input)

func (*Playwright) Type

func (p *Playwright) Type(selector string, typedString string, opts playwright.PageTypeOptions) error

Type wrapper around playwright type page function that takes in a selector, string, and a set of options

func (*Playwright) Uncheck

func (p *Playwright) Uncheck(selector string, opts playwright.FrameUncheckOptions) error

Uncheck wrapper around playwright uncheck page function that takes in a selector and a set of options

func (*Playwright) WaitForLoadState

func (p *Playwright) WaitForLoadState(state string)

func (*Playwright) WaitForNavigation

func (p *Playwright) WaitForNavigation(opts playwright.PageWaitForNavigationOptions) error

func (*Playwright) WaitForSelector

func (p *Playwright) WaitForSelector(selector string, opts playwright.PageWaitForSelectorOptions) error

WaitForSelector wrapper around playwright waitForSelector page function that takes in a selector and a set of options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL