Running a JavaScript asynchronous function and capturing the output using Selenium

Attempting to run the script below in Selenium

    result = driver.execute_script('let result; await axe.run().then((r)=> {result=r}); return result;')

Results in an error:

Javascript error: await is only valid in async function

Another attempt was made with the following code:

    result = @driver.execute_async_script('(async() => {return await axe.run();})();')

However, it produced the error message:

Selenium::WebDriver::Error::ScriptTimeoutError: script timeout: result was not received in 30 seconds

Answer №1

To successfully use execute_async_script, make sure to call the provided callback function in order to signal to the driver that you have completed the task. Failure to do so will result in the driver waiting indefinitely until a timeout occurs. The callback function should be included as the last argument:

script = "
var callback = arguments[arguments.length - 1]; // This callback is to be invoked upon completion
axe.run().then((r)=> {callback(r)});
"
result = @driver.execute_async_script(script);

For more information, please refer to this link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve a state property within a Vue template

Embarking on my Vue journey, I've been immersing myself in online videos to grasp the essence of this framework. One intriguing observation that has piqued my curiosity is the difference in behavior when I switch from a template to a render function i ...

Display JSON information in the present moment

I am looking to display only 2 results from a JSON file based on the current time. For example, at 11:00 am today, I want to show only the second and third items. JAVASCRIPT $(data.users).each(function() { var output = "<ul><li>" + this.first ...

Issue encountered when running a minification build on Angular 5

After successfully updating my Single Page Application (SPA) from Angular 4 to Angular 5 along with all dependencies, everything seemed to be working well. Both the development and production builds were functioning without any errors or warnings. However ...

Node.js and Express experiencing issues with updating cookies functionality

After successfully logging in, I created a login cookie. However, when I attempted to update the data within that cookie, an error occurred: Can't set headers after they are sent. Below is the code snippet in question: /* Logout to main user. */ /* ...

Can a string variable be passed as a file in a command line argument?

Running a command line child process to convert a local file to another format is something I need help with. Here's how it works: >myFileConversion localfile.txt convertedfile.bin This command will convert localfile.txt to the required format an ...

Trigger an action when the input text is changed, specifically when the input is populated from a different source

Is there a way to trigger an action on an input when the text is changed from another input, like a datepicker? I attempted to trigger the action when I click the date on the datepicker, but it does not seem to be working. Any suggestions? See below for my ...

Having trouble establishing a connection between socket.io client and server

I am currently attempting to establish a connection between a client and server using express and node.js. Unfortunately, I am encountering difficulties in connecting to the server. The tutorial I followed (available at https://socket.io/get-started/chat) ...

Retrieve the text inside the DIV that contains the clicked link

I'm facing an issue with my JS code: $(document).on("click", '.like', function (e) { $(this).parent().html("<a href = '#' class = 'unlike'><div class = 'heart'></div></a>"); ...

Utilizing an AngularJS custom filter twice

Experimenting with a custom Angular filter example found at: https://scotch.io/tutorials/building-custom-angularjs-filters#filters-that-actually-filter, my version looks like this: <!DOCTYPE html> <html> <script src="http://ajax.googleapi ...

Verifying Text Presence in a Listbox using Selenium

I am currently working on a scenario in Selenium with C# where I need to verify if text added to a list box has been successfully removed. Can anyone suggest the best approach to handle this type of situation? Below is the code snippet that I am using: / ...

What could be causing the issue with the functionality of third-level nested SortableJS drag-and-drop?

I am currently utilizing SortableJS to develop a drag-and-drop form builder that consists of three types/levels of draggable items: Sections, Questions, and Options. Sections can be dragged and reorganized amongst each other, Questions can be moved within ...

Unable to Achieve Full Height with Vuetify Container

Challenge: I'm facing an issue with my <v-container> component not consistently spanning the entire height of the application. Despite trying various methods such as using the fill-height property, setting height: 100%;, height: 100vh;, and expe ...

Is there a way to utilize a parameter for the user's input instead of relying on document.getElementById when incorporating a button?

let totalScore = 0; var myArray = [1, 2, 3, 4, 5]; let randomNum; function NumGuess(userInput) { var userGuess = userInput; var i; for (i = 0; i < 1; i++) { var randomNum = myArray[Math.floor(Math.random() * myArray.length)]; } if (us ...

"Enable real-time editing with inline save/submit feature in

I'm struggling to figure out how to extract the modified content from a CKEditor instance and send it to a URL. I've been referencing something like this: but I can't seem to determine how to save the changes. Is there a way to post the up ...

unable to locate the allong.es variadic function

Encountering an Error node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: undefined is not a function at /home/ubuntu/nodejs/test.js:4:10 at factorial (/home/ubuntu/nodejs/test.js:17: ...

The removeEventListener function is failing to properly remove the keydown event

Every time my component is rendered, I attach an event listener. mounted() { window.addEventListener("keydown", e => this.moveIndex(e)); } Interestingly, even when placed within the moveIndex method itself, the event listener persists and cannot ...

Utilizing Google Web Fonts in Gatsby with Custom Styled Components

While using createGlobalStyle from styled-components for global styling, everything seems to be working fine except for applying Google fonts. I have tried multiple ways but can't seem to get it to work. Here's the code snippet: import { createG ...

In a jQuery conditional statement, selecting the second child of the li element for targeting

I'm encountering an issue where I can't target the second child of an li element and use it in a conditional statement. Are jQuery conditionals not compatible with li:nth-child(2)? if($(".steps ul li:first-child").attr('aria-selected') ...

Gather information from various mongodb collections and consolidate them into a single array using Javascript

Is there a way to retrieve the most recent item from multiple collections and store them in an array based on the collection name and newest item? How can I accomplish this either sequentially or asynchronously? let dat = ["test", "test2"]; ...

Having trouble exporting CSV files with Tamil fonts. Are you experiencing an error?

We are exploring various methods to display Tamil content in a CSV file with characters like "தூதுக்கடட". Can anyone provide assistance? mysqli_set_charset($db, "utf8mb4"); $query = $db->query("$reports"); if($query->num_rows > ...