Verify the presence of a particular attribute in an HTML tag using Capybara and polling techniques

In my HTML code, the attributes of buttons (such as "AAAAA") will change based on external events. This real-time update is achieved through AJAX pooling.

<div class="parent">
<div class="group"><button title="AAAAA"/></div>
<div class="group"><button title="BBBBB"/></div>
<div class="group"><button title="CCCCC"/></div>
</div>
<div class="parent">
...
</div>

For example, let's say "AAAAA" transitions to "DDDDD" after an event labeled XXX. The testing script would be something like this:

scenario 'test' do
    # Before change
    page.within(first('.parent')) do
        page.within(all('.group')[0]) do
            expect(page.find('button')[:title]).to include('AAAAA')
        end
        page.within(all('.group')[1]) do
            expect(page.find('button')[:title]).to include('BBBBB')
        end
        page.within(all('.group')[2]) do
            expect(page.find('button')[:title]).to include('CCCCC')
        end
    end

    XXX

    # After change (AJAX polling)
    page.within(first('.parent')) do
        page.within(all('.group')[0]) do
            expect(page.find('button')[:title]).to include('DDDDD')
        end
    end 

However, this approach does not work because

expect(page.find('button')[:title]).to include('DDDDD')
does not wait for the AJAX polling to complete. Adding sleep(20) after XXX makes the test work, but that is not the recommended Capybara way.

I am seeking a more efficient and effective solution to handle this scenario properly. Any assistance is greatly appreciated.

Answer №1

To ensure accurate testing, it is recommended to utilize Capybara's matchers that incorporate waiting and retrying functionality, as opposed to RSpec's include.

expect(page).to have_css("button[title='DDDDDD']")

If you wish to check for substrings within a title instead of an exact match, you can employ *= rather than =, among other options.

It is advisable to use unique CSS selectors like find('.group:nth-of-type(1)') over all('.group')[0] whenever feasible. Keep in mind that the meanings of these expressions differ slightly, so there may be instances where one is not interchangeable with the other. Additionally, elements fetched by all/first are non-reloadable.

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

Manipulating variables across various methods in TypeScript

I have a simple code snippet where two variables are defined in the Main method and I need to access them from another method. However, I am encountering an issue with 'variables are not defined', even though I specified them in the declerations ...

Check to see if two sets of coordinates fall within the specified radius

I'm currently working on analyzing the collision data for major intersections in my city by aggregating it with the location information. My main goal is to determine the number of accidents that occurred within a 20-meter radius of each intersection. ...

Electron fails to display images in the compiled version

I'm currently troubleshooting an issue related to displaying images using CSS in my electron project. In the latest version of the application, the images are not showing up when linked from a CSS file. However, in a previous version, the images disp ...

Is there a javascript function that performs the reverse action of indexof()?

Is there a JavaScript function that is the opposite of indexOf()? In my code, when you press the button it will display 3 [from indexOf(".")]. However, I am looking for a function that is the opposite of indexOf(), which would show me 2 [decimal]. http: ...

Generating a JavaScript array using concealed data

var a1=$("#orderprogress").val().toFixed(2);//a1=50 var a2=$("#poprogress").val().toFixed(2); //a2=70 If I were to create an array in this format, how should I proceed? graphData = new Array( [a1 value,'#222222'],//[50,'#22222 ...

Unable to find JSON data using the Javascript Kafka Magic Tool, as no results are being

In JSON format, I have a message that contains various details. My goal is to utilize Javascript search functionality to identify if the EmailAddress matches the specific value I am looking for within hundreds of similar messages: "Message": { ...

Tips for inserting user input into an array and showcasing it

const [inputValue, setInputValue] = useState(""); return ( <input onChange={(event) => setInputValue(event.target.value)} /> <p>{inputValue}</p> ); I'm facing a problem where I need to take input from a user and store it in ...

Encountering a 404 error for core.js and browser.js while loading an Angular 2 app through system.src.js

I am new to Angular2 and have followed the Angular2 quickstart and tutorial to get started. Just to provide some context, when a user clicks on a link in the top navigation bar of my webapp, it triggers a server side request. The resulting page returned t ...

Tips for organizing AJAX code to show images and videos in HTML with a switch case approach

I have 2 tables, one for images and one for videos. Within my HTML code, I have three buttons: slide_image, video, and single_image. Using iframes, I am able to load images and videos on the same page. When I click on the slide_image button, it displays im ...

Having trouble parsing emails in Python using the "imaplib" library, dealing with HTML line character limits, and handling extra non-Unicode characters

In my Python 3 project, I am working on validating emails that are sent to my inbox. I am using imaplib library to achieve this task. While I have successfully retrieved the email content, the mail appears to be unreadable and somewhat corrupted (reference ...

"Enhancing SEO with interactive updates for search engine crawlers

Managing a website where users receive messages based on their location can be challenging. The workflow involves the user inputting an address into a text field and receiving a response through an AJAX callback, all requests to the server are made via POS ...

Modify a database entry by updating the price to include or exclude taxes based on the selected radio button option

I am working on developing a checkout form that includes radio buttons at the top. One button is meant to display prices without tax when selected, and the other to show prices with tax included. <form id="f-p" method="post" action="#####"> ...

Create fluidly changing pictures within varying div elements

Hello there! I have a form consisting of four divs, each representing a full page to be printed like the one shown here: I've successfully created all the controls using AJAX without any issues. Then, I load the images with another AJAX call, and bel ...

Sending various values to a JavaScript function

I am working with a function that looks like this: //Function Call with Single Parameter responses(baseURL); //Function Definition function responses(baseURL) { $.ajax({ url: baseURL, type: "get", cache: false, header ...

Issues with rendering in-line styles in ReactJS after a state update

I'm currently working on implementing a basic state change for a button in React. 'use strict'; class ReactButton extends React.Component { constructor(props) { super(props); this.state = {hovering: false}; } onClick() { ...

Problem identified with Vue.js: The Log in screen briefly flashes before redirecting the authenticated user (resulting in a full page refresh)

My routing is functioning properly, utilizing navigation guards to prevent users from accessing the login or register routes once they are signed in. However, when I manually type '/auth/signin' in the address bar, the login screen briefly appear ...

Optimizing CSS for printing by eliminating unnecessary white space with media queries

Appreciate your assistance! I'm currently working on a solution to print a specific div using CSS Media queries. When the user clicks on print, I want to hide all other elements except for the body div they chose. However, I'm facing an issue whe ...

Develop a container element with protective measures against external CSS styles impacting internal elements

As I work on integrating my webpage code into a large project, I have encountered an issue. The entire project code is contained within a div element inside the <main> tag. However, when I add this container div within the <main> tag, the pro ...

Exploring Angular14: A guide to efficiently looping through the controls of strictly typed FormGroups

Currently, I am working on upgrading my formGroups to be strictly typed in Angular v14. Within my FormGroup, there is a specific block of logic that iterates through all the controls and performs an action (this part is not crucial as I am facing issues be ...

Next.js 13 React Server Component not displaying updated data upon build completion

I have a React Server Component that retrieves its data at build time and does not reload it while the site is running. I expected it to fetch the data once when the server component is first rendered. Is there a way to force this server component to relo ...