You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Postman桌面端脚本能否打开外部浏览器获取临时PIN作为请求参数?

Can Postman Desktop Scripts Open External Browsers and Fetch Data (Temporary PIN) as Request Parameters?

Great question! Let’s break this down clearly, since the built-in Postman sandbox has some limitations but there’s a reliable workaround:

First, the direct answer: You can’t do this using only Postman’s native pre-request/test scripts—but you can make it work by pairing Postman’s command-line tool (Newman) with browser automation libraries like Puppeteer or Playwright. Here’s why, and how to implement it:

Why Postman’s Built-in Scripts Can’t Do This Alone

Postman’s desktop app runs its scripts in a restricted sandbox environment. While it does support opening external browser links (as you noted, since v6.1.2), this only launches the browser—there’s no way for the Postman script to read or interact with the content of that external window. The sandbox blocks access to external processes and cross-context DOM manipulation, so direct data extraction from the browser isn’t possible here.

Workaround: Newman + Browser Automation

Newman (Postman’s official CLI tool) lets you run Postman collections programmatically via Node.js. This opens the door to combining it with browser automation tools to:

  1. Launch an external browser, navigate to your target site, and extract the temporary PIN.
  2. Pass that PIN as an environment variable to your Postman collection.
  3. Use the PIN in your Postman requests as a parameter.

Step-by-Step Implementation

  1. Write a Node.js script to fetch the PIN with Puppeteer
    First, install Puppeteer:

    npm install puppeteer
    

    Then create a get-pin.js file (customize the selector to match your site’s PIN element):

    const puppeteer = require('puppeteer');
    
    async function fetchTemporaryPin() {
      // Launch a visible browser (remove `headless: false` for headless mode)
      const browser = await puppeteer.launch({ headless: false });
      const page = await browser.newPage();
    
      // Navigate to your PIN-generating website
      await page.goto('https://your-pin-site.com');
    
      // Example: Extract PIN from an element with ID "temp-pin-display"
      const pin = await page.$eval('#temp-pin-display', element => element.textContent.trim());
    
      await browser.close();
      return pin;
    }
    
    module.exports = fetchTemporaryPin;
    
  2. Run your Postman collection with Newman and pass the PIN
    Install Newman:

    npm install newman
    

    Create a run-collection.js file:

    const newman = require('newman');
    const fetchTemporaryPin = require('./get-pin.js');
    
    async function executeCollection() {
      // Fetch the PIN first
      const tempPin = await fetchTemporaryPin();
    
      // Run your Postman collection, passing the PIN as an environment variable
      newman.run({
        collection: require('./your-postman-collection.json'), // Path to your exported collection
        environment: {
          values: [
            { key: 'tempPin', value: tempPin, enabled: true }
          ]
        },
        reporters: 'cli' // Output results to the command line
      }, (err) => {
        if (err) throw err;
        console.log('Collection executed successfully!');
      });
    }
    
    executeCollection();
    
  3. Use the PIN in your Postman request
    In your Postman collection, reference the tempPin environment variable wherever you need it—for example, as a query parameter, form field, or header value:

    {{tempPin}}
    

Alternative: Skip the Browser (If Possible)

If the temporary PIN can be retrieved via an API endpoint (instead of only through the web UI), you can skip the browser entirely. Use Postman’s built-in pm.sendRequest in a pre-request script to call that API and store the PIN in an environment variable:

pm.sendRequest('https://your-pin-api.com/get-temp-pin', (err, res) => {
  if (!err) {
    const pin = res.json().tempPin;
    pm.environment.set('tempPin', pin);
  }
});

This is cleaner if the API option is available, but the Newman + Puppeteer method works when you have to interact with the web UI directly.

内容的提问来源于stack exchange,提问作者Pawan Arora

火山引擎 最新活动