Rayrun

How do I create a script using Playwright in Node.js to connect to a website and log the output in headless mode?

Answer

Connect to a Website and Log Output with Playwright

First, install the Playwright library in your Node.js project. Use npm or Yarn and run npm i -D playwright. This command downloads the Playwright NPM package and browser binaries for Chromium, Firefox, and WebKit.

import { chromium } from 'playwright';

Next, launch the Chromium browser instance in headless mode.

const browser = await chromium.launch({headless: true});

Create a new page instance.

const page = await browser.newPage();

Navigate to the ray.run website.

await page.goto('https://ray.run');

Finally, log the output of the ray.run website.

const response = await page.goto('https://ray.run');
console.log(await response.text());

Your final code should look like this:

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch({headless: true});
  const page = await browser.newPage();
  await page.goto('https://ray.run');
  const response = await page.goto('https://ray.run');
  console.log(await response.text());
  
  // Close the browser
  await browser.close();
})();

For more information on Playwright, check out Mastering Playwright Test Automation: Your Comprehensive Cheat Sheet.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Questions

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 luc@ray.run.