0

I am trying to execute a script in javascript using selenium(not sure whether it is possible to run that way or not). I don't know how to return the output of that script. Please provide me with some leads to store the output of the below script.

final JavascriptExecutor js = (JavascriptExecutor) driver;
       String ans =(String)  (js.executeScript("var myHeaders = new Headers();\n" +
               "myHeaders.append('client-id', 'LPDP');\n" +
               "myHeaders.append('a2z-csrf-token', 'NON_SDF');\n" +
               "myHeaders.append('x-amz-rid', 'M6507NCWPW2FVPSSRMVM');\n" +
               "let inputEntities = new Map();\n" +
               "inputEntities.set(\"Commons$customerId\", \"\\\"A2ZLDCQRXMMNLG\\\"\")\n" +
               "inputEntities.set(\"Commons$marketPlaceId\", \"\\\"A2XZLSVIQ0F4JT\\\"\")\n" +
               "inputEntities.set(\"Commons$sessionId\", \"\\\"asdb3412\\\"\")\n" +
               "inputEntities.set(\"Commons$ubId\", \"\\\"abc\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$eventId\", \"\\\"prsrohitest-1\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$clientId\", \"\\\"HFC\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$useCaseName\", \"\\\"lineItemPromotionPaymentMethodEvent\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$eventTimeStamp\",\"\\\"2022-04-20T21:21:57.934Z\\\"\" )\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$category\", \"\\\"HFC\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$subCategory\", \"\\\"PREPAID_RECHARGE\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$requestType\", \"\\\"HFCBP\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$partition\", \"\\\"useCaseName,category,subCategory\\\"\")\n" +
               "inputEntities.set(\"Rewards$APPA$Commons$benefitsToBeEvaluated\", \"[\\\"GCCashback\\\",\\\"Coupon\\\",\\\"Membership\\\",\\\"ScratchCard\\\"]\")\n" +
               "\n" +
               "let entitiesToBeResolved = [\"Rewards$APPA$GetAllPromotions$applicablePromotionDetailList\"]\n" +
               "\n" +
               "const executeInput = {\n" +
               "\"inputEntities\": Object.fromEntries(inputEntities),\n" +
               "\"entitiesToBeResolved\": entitiesToBeResolved,\n" +
               "};\n" +
               "\n" +
               "fetch(\"https://dummy url\", {\n" +
               "  method: 'POST',\n" +
               "  headers: myHeaders,\n" +
               "  body: JSON.stringify(executeInput),\n" +
               "})\n" +
               "  .then(response => response.text())\n" +
               "  .then(result => console.log(result))\n" +
               "  .catch(error => console.log('error', error));\n" +
               "\n"));

  • seems like you just want to put "return result;" in your script.... make sure to also return from the fetch closure... – pcalkins May 11 '22 at 17:40
  • I want to store the result of POST fetch call using the selenium – dazzyCrazzy May 12 '22 at 09:05
  • you'd create a variable outside of your fetch call, and set that during your callback (I think they call that a closure?) Then return that variable. Or just return results if it's in scope. – pcalkins May 12 '22 at 18:05
  • Maybe check this thread: https://stackoverflow.com/questions/50417982/js-fetch-api-access-return-value – pcalkins May 12 '22 at 18:11
  • Thanks for the suggestion. Also is there any way I can store that variable data into java varible as well ? – dazzyCrazzy May 13 '22 at 04:24
  • yes, whatever you return will be typed to string, so you've already got that right... ...ans = – pcalkins May 13 '22 at 17:20
  • Thanks for suggestion but unable to do that, can you please ping me exactly what to do ? – dazzyCrazzy May 15 '22 at 13:42

0 Answers0