Rayrun
← Back to Discord Forum

Use a collection from playwright using newman

I would like to know if there is any way to use newman with playwright. My idea is to be able to use the Newman library to call some collection that I have in Postman and from there capture some values of its response. I know that playwright has its own module to make API calls, but the company where I am currently wants to use the requests that already exist in postman.

Could anyone help me with this integration, how could it be done, any ideas? I would greatly appreciate your help.

This thread is trying to answer question "Is there a way to use Newman with Playwright to call a collection from Postman and capture some values from its response?"

5 replies
dirvinautomation_16636

Does it need to be playwright? Seems like you could achieve the desired result with a shell script and the newman CLI.

Otherwise, playwright can execute any (node) javascript that you want. Capture responses from the newman library and make assertions with playwright matchers.

The flow that I want to carry out is the following:

Start the automated test execution with Playwright

  1. The first step of the test would be to execute a Newman collection that serves as setup, from said execution, I capture some data
  2. The second step of the test is executed, which is to log in with some data obtained from the response of the Newman execution
  3. The third step is some actions that playwright does in the UI
  4. The fourth step is to run another collection with newman and validate some data from said run.

Hi, I think it'd be easier to rewrite your postman collection to playwright. But if trying to integrate playwright with the old API calls I guess you can spawn child processes in your setup/teardown scripts. Not sure it'll work correctly but there's a possibility. It needs to test.

dirvinautomation_16636

@elbudaoes3 I think that should be possible with playwright + newman without much coercing.

Here's how I would imagine that going. This is a little contrived and you will have to, of course, adapt it to your use case:

import newman from "newman"
import { test, expect } from "@playwright/test"

test("some test", async ({ page }) => {
  const loginData = await new Promise((resolve, reject) => {
    newman.run(runOpts).on("done", (error, results) => {
      if (error) reject(error)
      else resolve(results)
    })
  })

  await expect(loginData).toHaveProperty("email")

  await page.goto("/sign-in")
  await page.locator("#email").fill(loginData.email)
  await page.locator("#password").fill(loginData.password)
  // the rest of your tests and other newman runs, etc.
})

It's a little funky with the Promise syntax, but that's just so that we can await the newman results at the outer test scope and not have to write the rest of the test nested inside a callback function (or other funkier solutions).

DISCLAIMER: I am not an expert with the newman library, so this example is untested and derived from their docs, but the principle is sound.

Thank you all very much, I have followed the example of @dirvinautomation_16636 and it has worked perfectly for me, very graceful.

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.