Coder Social home page Coder Social logo

marcusnumminen / playwrepl.ts Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 339 KB

Playwright by command line. Playwrepl.ts is a REPL application that lets you control playwright browser from the command line with TypeScript

Home Page: https://github.com/marcusNumminen/playwrepl.ts

TypeScript 100.00%
chromium playwright repl typescript e2e-testing nodejs ts-node

playwrepl.ts's Introduction

playwrepl.ts

logo

Playwrepl.ts is quite simple node REPL server script that allow you controlling playwright from the command line using TypeScript code. When you start the tool in will open a node REPL server in the terminal and the open a chromium browser and expose some playwright object to the REPL server for interacting with browser using playwright api. playwrepl.ts is a node REPL server that utilizes ts-node repl service to compile Typescript code at runtime. The tool supports code completion using the tab key as well. Playwrepl.ts is a very handy tool when it comes to tryout slightly more complicated stuff than just picking a singe locator. It's a great tool when you are learning playwright because you can manually navigate browser to the place you want to explore and the start exploring different coding stuff from the command line.

ex

Getting started

  1. Clone this repo playwrepl.ts
  2. Run: npm install
  3. Run: npx playwright install chromium
  4. Start the tool by: npx ts-node ./playwrepl.mts

If you use node version 20.x.x the you should start the tool by: node --loader ts-node/esm ./playwrepl.mts

How the tool works

There are some command line options that could be used when starting the tool:

$ npx ts-node ./playwrepl.mts -h
Usage: playwrepl.ts [options]

playwright by command line

Options:
  -V, --version                  output the version number
  -u, --url <baseUrl>            The url to be opened by the browser
  -e, --extraHttpHeaders <prop>  extra http headers, use the format name:value (default: [])
  -s, --storageState <path>      path to storage state
  -h, --help                     display help for command

The command line options --url, --storageState and --extraHttpHeaders reflects the baseURL, extraHTTPHeaders and storageState options when creating new browser context (see: https://playwright.dev/docs/api/class-browser#browser-new-context)

Playwrepl.ts have the normal node REPL commands but also two additional as well:

-> .help
.break              Sometimes you get stuck, this gets you out
.clear              Break, and also clear the local context
.editor             Enter editor mode
.exit               Exit the REPL
.help               Print this help message
.load               Load JS from a file into the REPL session
.save               Save all evaluated commands in this REPL session to a file
.saveStorageState   Save current storage state 
.showLogo           Show the amazing playwrepl.ts logo

Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL

.saveStorageState will save the current sessions storage state and .showLogo will show some amazing console graphics :)

Exposed playwright objects

playwrepl.ts exposes several playwright object connected to the opened browser that could be used within the REPL:

All of these object object is tied to the this object

REPL history

playwrepl.ts store all the executed command in a .history file in the root of the repo and you can access history using the arrow keys within the repl.

Examples

Lets use saucedemo.com for a simple demo when we interact with the browser:

-> void await this.page.goto('https://www.saucedemo.com/')
-> await this.page.locator('[data-test="username"]').fill('standard_user')
-> await this.page.locator('[data-test="password"]').fill('secret_sauce')
-> await this.page.locator('[data-test="login-button"]').click();
-> await this.page.locator('.inventory_item').count()

Lets do a simple rest request using the pet store sample site:

-> const reqContext = await this.request.newContext({baseURL: 'https://petstore.swagger.io/'})
-> const resp = await reqContext.get('v2/store/inventory')
-> resp.status()
200
-> await resp.json()
{
  '3000': 1,
  '5000': 1,
  '6000': 1,
  '7000': 1,
  sold: 43,
  string: 617,
  unavailable: 7,
  availeble: 1,
  pending: 31,
  available: 280,
  avalible: 1,
  bold: 1,
  SOLD: 1,
  Available: 2,
  'Not available': 3,
  status: 1
}

Load scripts

We can create scripts and load these using the .load command from within the playwrepl.ts:

-> .load scripts/sample.mts
void await this.page.goto('https://www.saucedemo.com/')
await this.page.locator('[data-test="username"]').fill('standard_user')
await this.page.locator('[data-test="password"]').fill('secret_sauce')
await this.page.locator('[data-test="login-button"]').click();
await this.page.locator('.inventory_item').count()
... 6

Known issues

  • import within the repl do not work:
> import dotenv from "dotenv"
/home/stuffy/projects/playwrepl.ts/<repl>.ts:1
export {};
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'

See: TypeStrong/ts-node#2005

  • node 20.x.x problem. If you are using node 20.x.x the you should startb the tool with: node --loader ts-node/esm ./playwrepl.mts. See TypeStrong/ts-node#1997

playwrepl.ts's People

Contributors

marcusnumminen avatar

Stargazers

Lucas Miguel avatar Matt Bowles avatar  avatar Margarita Delikatnaya avatar Michael Demarais avatar  avatar  avatar  avatar Johan Ablett-Karlsson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.