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().

1
  • 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 - automationbasicsselenuim.blogspot.com/2019/08/… Commented Aug 23, 2019 at 10:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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?
If you using javascript executor for wait as per question then you can try webdriver wait.
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.
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 - automationbasicsselenuim.blogspot.com/2019/08/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.