javascriptseleniumfetchrequest-promisesetscripttimeout

script time out with executeAsyncScript() for fetch post call


I am trying to run a script using executeAsyncScript() method having a fetch call. since fetch call returns a promise, hence on console it is taking some time to fulfill the promise but using selenium java script executer it is throwing error saying script time out,hence I am getting null as output. how can I achive the expected result using selenium executeAsyncScript method.

String str = (String) js.executeAsyncScript("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" +
                "\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" +
                "var obj \n" +
                "\n" +
                "fetch("url", {\n" +
                "  method: 'POST',\n" +
                "  headers: myHeaders,\n" +
                "  body: JSON.stringify(executeInput),\n" +
                "})\n" +
                "  .then(response => response.text())\n" +
                "  .then(result => obj = result)\n" +
                "  .then(()=> console.log(obj))\n" +
                "  .catch(error => console.log('error', error));\n" +
                "\n" +
                "  return obj;");

I am getting null in str variable. Thanks in advance for any help


Solution

  • Note: I'm not in the habit of using java so I don't know how to escape strings properly.

    The general way to do this is:

    js.executeAsyncScript("arguments[0]('foo')")
    

    You can put that inside a promise's then and it will still work.