Rayrun
← Back to Discord Forum

Anyone here using Playwright for automation (not testing)?

Playwright works nice for testing, but what I need right now is more in the spirit of automation. For example, being able to write a Node.js CLI script accepting some parameters and perform some actions (depending on those parameters) in the browser. Do I have to write a test without assertions for that or is there a better way?

This thread is trying to answer question "How can Playwright be used for automation, specifically for writing a Node.js CLI script that performs browser actions based on parameters, and how can something be passed to the test via command-line parameters?"

5 replies

I use something that someone has written internally which isn't a "test" per se. They've used the "test" fixture however. It just doesn't assert on anything & the last line of code pauses so we can complete the final form submission manually. It works well for me

Interesting, though a bit different than my use case, thanks.

ok, I figured out most of it – but there is one problem remaining. How to pass something to the test via command-line parameters? I can write a wrapper script which puts its parameter into an env variable, and then the test will use the env variable, but this is a very roundabout solution... Is there a simpler way?

refactoreric

Playwright for NodeJS comes both as a library and as a test framework. Here's the docs for the library: https://playwright.dev/docs/library

I could be wrong or just pointing out the blooming obvious but I believe this is what we do with npm scripts - npm run test being a common example. They pass in cli params when they run. So I don't know if you could just configure it in the package.json file? Or perhaps you could elaborate what you mean when you say, "pass something into the test"?

Answer

Sure, @playwright/test is not just for testing, it's a powerful tool for automating tasks across different browsers. It supports all modern browsers, including Chrome, Edge, Safari, and Firefox.

One of the cool things about Playwright is its auto-wait APIs. These automatically wait for elements to be ready before interacting with them, making your automation tasks more reliable and easier to write.

import { test } from '@playwright/test';

test('auto-wait example', async ({ page }) => {
  await page.goto('https://ray.run/');
  const title = await page.title();
  console.log(title);
});

Playwright also eliminates the need for sleep timeouts by receiving browser signals such as network requests, page navigations, and page load events. This reduces flakiness in your tasks.

Another great feature is its powerful network control. You can stub and mock network requests, allowing you to simulate different network conditions or responses.

import { test } from '@playwright/test';

test('network interception example', async ({ page }) => {
  await page.route('https://ray.run/*', route => {
    route.fulfill({
      status: 200,
      contentType: 'text/plain',
      body: 'Hello, world!'
    });
  });
  await page.goto('https://ray.run/');
});

Playwright can work with multiple domains, pages, and frames, and supports both headless and headed modes for all supported browsers on all platforms. So, whether you're automating complex scenarios or just need a versatile tool for various tasks, Playwright has got you covered.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].