0

I want to use an async function call to run few scripts asynchronously but I am not able to achieve it using javaScriprtExecutor‘s executeAsyncScript() function. Here is a small snippet for your reference.

I believe the function ‘executeAsyncScript’ should run asynchronously and the function call at line #2 (being faster in nature) should get finished before the executeAsyncScript(). but this not happening. Function calls at line no 2 is getting called only when execution on of function 1 ends including 5-second delay!!

Can anyone help me in understanding what is it I am not able to see here?

1. js.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");
2. driver.findElement(By.xpath("//input")).sendKeys("Search term!!");

With this sequence, the browser wais for 5 seconds and then do sendkeys().

Xat
  • 127
  • 1
  • 3
  • 10
  • If I ask for an AsyncScript for wat for 5000 milli-sec it will wait for that time. Being async here does not mean to jump the commands until there is no response. Refer - https://automationbasicsselenuim.blogspot.com/2019/08/what-is-javascriptexecutor-in-selenium.html – Xat Aug 23 '19 at 10:44

1 Answers1

1

both functions executeAsyncScript and execteScript block the WebDriver control flow until they complete. That's why 5 seconds delay.

see this for complete explanation WebDriver executeAsyncScript vs executeScript

murali selenium
  • 3,847
  • 2
  • 11
  • 20
  • so how can I run such a scenario in asynchronously? do you have a better real-time way of using executeAsyncScript to make the script run faster? – Xat Jun 23 '19 at 10:39
  • If you using javascript executor for wait as per question then you can try webdriver wait. – murali selenium Jun 23 '19 at 14:15
  • This is for understanding how async works. wanted to see a sample code to really see something happened asynchronously. Hence those couple of lines of code. Nothing to do with Webdriver wait. I thought waiting and any other following function may be the best way to see it happening but it just didn't work. Do you have any sample that you can share? Thanks in advance. – Xat Jun 23 '19 at 16:06
  • If I ask for an AsyncScript for wat for 5000 milli-sec it will wait for that time. Being async here does not mean to jump the commands until there is no response. Please refer - https://automationbasicsselenuim.blogspot.com/2019/08/what-is-javascriptexecutor-in-selenium.html – Xat Aug 23 '19 at 10:46