Rayrun
← Back to Discord Forum

is there a module api in playwright?

Hey y’all, i was wondering if there is a way to launch playwright test via module api, could not find it in docs. What i have in mind is something like

node runTest.js

Where i could run my own logic of running the script

Instead of doing

npx playwright test

This thread is trying to answer question "Is there a way to launch playwright test via module API?"

3 replies

Perhaps you will find something useful here, https://github.com/microsoft/playwright/issues/7275

Not sure if that's what you after, but maybe you want to use Library instead of Playwright Test? https://playwright.dev/docs/library#library-example You won't get "web-first assertion" though.

that could be it, thank you!

Answer

Sure, you can launch Playwright tests via the module API. Playwright provides a method to launch a browser instance using the playwright module. You can import the necessary browser types like chromium, firefox, and webkit from the playwright module and use them to create an instance of a browser.

Here's a quick example:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  // Perform actions on the page
  
  await browser.close();
})();

In this example, we're importing the chromium object from the playwright module and using it to launch a Chromium browser instance. We then create a new page in that browser using the newPage() method. After performing any necessary actions on the page, we close the browser using the close() method.

You can also use other available browsers like Firefox or WebKit by importing their respective objects (firefox, webkit) from the playwright module.

Remember, Playwright APIs are asynchronous and return Promise objects. We're using async/await syntax for better readability and wrapping our code in an async arrow function that invokes itself immediately.

So, to launch Playwright tests via module API, import necessary objects from the Playwright library (like Chromium) and use them to create instances of browsers. Then perform various actions on pages within those browsers before closing them when done with testing.

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