puppeteer / puppeteer

JavaScript API for Chrome and Firefox
https://pptr.dev
Apache License 2.0
88.25k stars 9.06k forks source link

return object on page.on('response') not working as expected #5424

Closed AzySir closed 4 years ago

AzySir commented 4 years ago

Steps to reproduce

Tell us about your environment:

What steps will reproduce the problem?

Running the automation script will through the error. The "return obj" is not being returned to the parent obj. The object being returned is the response object...

Please include code that reproduces the issue.

let myobj = await page.on('response', async (response) => {
            const response_json = await response.json();
            let obj = { unit_price: response_json.unit_price, carton_price: response_json.carton_price };
            return obj;
      });

      console.log(myobj)

What is the expected result? Expecting result should be -

 { 
  unit_price: '$62.02',
  carton_price: '$372.12',
  stock_code: '67236'
 }

What happens instead?

Page {
  _events: [Object: null prototype] {
    response: [
      [AsyncFunction],
      [AsyncFunction],
      [AsyncFunction],
      [AsyncFunction],
      [AsyncFunction]
    ]
  },
  _eventsCount: 1,
  _maxListeners: undefined,
  _closed: false,
  _client: CDPSession {
    _events: [Object: null prototype] {
      'Fetch.requestPaused': [Function: bound _onRequestPaused],
      'Fetch.authRequired': [Function: bound _onAuthRequired],
      'Network.requestWillBeSent': [Function: bound _onRequestWillBeSent],
      'Network.requestServedFromCache': [Function: bound _onRequestServedFromCache],
      'Network.responseReceived': [Function: bound _onResponseReceived],
      'Network.loadingFinished': [Function: bound _onLoadingFinished],
      'Network.loadingFailed': [Function: bound _onLoadingFailed],
      'Page.frameAttached': [Function],
      'Page.frameNavigated': [Function],
      'Page.navigatedWithinDocument': [Function],
      'Page.frameDetached': [Function],
      'Page.frameStoppedLoading': [Function],
      'Runtime.executionContextCreated': [Function],
      'Runtime.executionContextDestroyed': [Function],
      'Runtime.executionContextsCleared': [Function],
      'Page.lifecycleEvent': [Function],
      'Target.attachedToTarget': [Function],
      'Target.detachedFromTarget': [Function],
      'Page.domContentEventFired': [Function],
      'Page.loadEventFired': [Function],
      'Runtime.consoleAPICalled': [Function],
      'Runtime.bindingCalled': [Function],
      'Page.javascriptDialogOpening': [Function],
      'Runtime.exceptionThrown': [Function],
      'Inspector.targetCrashed': [Function],
      'Performance.metrics': [Function],
      'Log.entryAdded': [Function],
      'Page.fileChooserOpened': [Function]
    },
    _eventsCount: 28,
    _maxListeners: undefined,
    _callbacks: Map {},
    _connection: Connection {
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      _url: 'ws://127.0.0.1:51556/devtools/browser/a3ce2d1f-5c33-4126-bfe6-627365e4f81b',
      _lastId: 118,
      _callbacks: Map {},
      _delay: 0,
      _transport: [WebSocketTransport],
      _sessions: [Map],
      _closed: false,
      [Symbol(kCapture)]: false
    },
    _targetType: 'page',
    _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
    [Symbol(kCapture)]: false
  },
  _target: Target {
    _targetInfo: {
      targetId: '426F4124BBC3CD1571B31D5964AFD6B8',
      type: 'page',
      title: 'https://www.paramountliquor.com.au/customers/orders',
      url: 'https://www.paramountliquor.com.au/customers/orders',
      attached: true,
      browserContextId: '3DE25901B30FFCD9AFF41460F4708E06'
    },
    _browserContext: BrowserContext {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      _connection: [Connection],
      _browser: [Browser],
      _id: null,
      [Symbol(kCapture)]: false
    },
    _targetId: '426F4124BBC3CD1571B31D5964AFD6B8',
    _sessionFactory: [Function],
    _ignoreHTTPSErrors: false,
    _defaultViewport: { width: 800, height: 600 },
    _screenshotTaskQueue: TaskQueue { _chain: [Promise] },
    _pagePromise: Promise { [Circular] },
    _workerPromise: null,
    _initializedCallback: [Function],
    _initializedPromise: Promise { true },
    _closedCallback: [Function],
    _isClosedPromise: Promise { <pending> },
    _isInitialized: true
  },
  _keyboard: Keyboard {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _modifiers: 0,
    _pressedKeys: Set {}
  },
  _mouse: Mouse {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _keyboard: Keyboard {
      _client: [CDPSession],
      _modifiers: 0,
      _pressedKeys: Set {}
    },
    _x: 1157.59375,
    _y: 619.09375,
    _button: 'none'
  },
  _timeoutSettings: TimeoutSettings {
    _defaultTimeout: null,
    _defaultNavigationTimeout: null
  },
  _touchscreen: Touchscreen {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _keyboard: Keyboard {
      _client: [CDPSession],
      _modifiers: 0,
      _pressedKeys: Set {}
    }
  },
  _accessibility: Accessibility {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    }
  },
  _frameManager: FrameManager {
    _events: [Object: null prototype] {
      [Symbol(Events.FrameManager.FrameAttached)]: [Function],
      [Symbol(Events.FrameManager.FrameDetached)]: [Function],
      [Symbol(Events.FrameManager.FrameNavigated)]: [Function]
    },
    _eventsCount: 3,
    _maxListeners: undefined,
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _page: [Circular],
    _networkManager: NetworkManager {
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      _client: [CDPSession],
      _ignoreHTTPSErrors: false,
      _frameManager: [Circular],
      _requestIdToRequest: [Map],
      _requestIdToRequestWillBeSentEvent: Map {},
      _extraHTTPHeaders: {},
      _offline: false,
      _credentials: null,
      _attemptedAuthentications: Set {},
      _userRequestInterceptionEnabled: false,
      _protocolRequestInterceptionEnabled: false,
      _userCacheDisabled: false,
      _requestIdToInterceptionId: Map {},
      [Symbol(kCapture)]: false
    },
    _timeoutSettings: TimeoutSettings {
      _defaultTimeout: null,
      _defaultNavigationTimeout: null
    },
    _frames: Map { '426F4124BBC3CD1571B31D5964AFD6B8' => [Frame] },
    _contextIdToContext: Map { 5 => [ExecutionContext], 6 => [ExecutionContext] },
    _isolatedWorlds: Set { '__puppeteer_utility_world__' },
    _mainFrame: Frame {
      _frameManager: [Circular],
      _client: [CDPSession],
      _parentFrame: null,
      _url: 'https://www.paramountliquor.com.au/customers/orders',
      _id: '426F4124BBC3CD1571B31D5964AFD6B8',
      _detached: false,
      _loaderId: '140425F2329D96B297A765D58E762725',
      _lifecycleEvents: [Set],
      _mainWorld: [DOMWorld],
      _secondaryWorld: [DOMWorld],
      _childFrames: Set {},
      _name: undefined,
      _navigationURL: 'https://www.paramountliquor.com.au/customers/orders'
    },
    [Symbol(kCapture)]: false
  },
  _emulationManager: EmulationManager {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _emulatingMobile: false,
    _hasTouch: false
  },
  _tracing: Tracing {
    _client: CDPSession {
      _events: [Object: null prototype],
      _eventsCount: 28,
      _maxListeners: undefined,
      _callbacks: Map {},
      _connection: [Connection],
      _targetType: 'page',
      _sessionId: 'B2111B1D66AE5F4EA608D478753FE74A',
      [Symbol(kCapture)]: false
    },
    _recording: false,
    _path: ''
  },
  _pageBindings: Map {},
  _coverage: Coverage {
    _jsCoverage: JSCoverage {
      _client: [CDPSession],
      _enabled: false,
      _scriptURLs: Map {},
      _scriptSources: Map {},
      _eventListeners: [],
      _resetOnNavigation: false
    },
    _cssCoverage: CSSCoverage {
      _client: [CDPSession],
      _enabled: false,
      _stylesheetURLs: Map {},
      _stylesheetSources: Map {},
      _eventListeners: [],
      _resetOnNavigation: false
    }
  },
  _javascriptEnabled: true,
  _viewport: { width: 1920, height: 1080 },
  _screenshotTaskQueue: TaskQueue {
    _chain: Promise {
      <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 07 80 00 00 04 38 08 06 00 00 00 e8 d3 c1 43 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 20 00 ... 240361 more bytes>
    }
  },
  _workers: Map {},
  _fileChooserInterceptors: Set {},
  [Symbol(kCapture)]: false
}
AzySir commented 4 years ago

Events ftw (eye roll) - Fixed issue with below code

Also important to note that page.on should not have an "await" before page.on (note the issue vs the solution)

      var myobj = 0;
      const log_result = (value) => {
        console.log(value)
      }

      page.on('response', async response => {
            const response_json = await response.json();
            myobj = { unit_price: response_json.unit_price, carton_price: response_json.carton_price };
            log_result(myobj)
      });