Tips for modifying Capybara selectors on a website with css-modules

When writing our automated tests, a typical line of code we use looks like this:

find('.edit-icon').click

As we move towards incorporating css-modules in our project, I've been informed that class names could undergo significant changes. A peculiar example of this is a website that uses emojis in its class names (seen when inspecting the page):

css modules by Glenn Maddern

How can I best prepare for such a drastic change? I anticipate many of our specifications breaking, and I am concerned about the possibility of being unable to create tests at all with this new technology in our project.

Answer №1

By utilizing custom capybara selectors, you have the ability to abstract the actual css query into a single location. There was a mention in your comments about the necessity to switch to a class attribute that begins with a specified value.

Capybara.add_selector(:class_starts_with) do
  css { |locator| "[class^=\"#{locator}\"]"
end

This snippet above achieves that functionality and can be utilized like so:

find(:class_starts_with, 'something')

Alternatively, if you set Capybara.default_selector = :class_starts_with, then you can simply use:

find('something')

Instead of modifying the default_selector, another approach could be to create a helper method like find_by_class_start that simply calls the find method with :class_starts_with as an argument (similar to how Capybara's #find_field, etc work).

It is important to note that the custom selector mentioned above may only work effectively if there is only one class name set. If multiple class names are expected, you may need to adjust the selector to include both the starts with and contains logic like this:

"[class^=\"#{locator}\"], [class*=\" #{locator}\"]"

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

Could we incorporate a backwards typewriter animation?

Is there a way to delay this animation for 2 seconds before playing in reverse? I came across this online and made some edits, but I'm still new to this (early stages). Can this be achieved using CSS, HTML, and JavaScript? I plan to use this as an ale ...

Showing a hidden div after an unsuccessful AJAX call

Encountering an issue with displaying a div notification using the code snippet below: $.ajax({ url: url, cache: false, async: false, success: function (data) { response = jQuery.parseJSON(data); }, error: functi ...

Tips for dynamically adjusting the size of a div container according to the user's

I have been working on a unique landing page design featuring a menu made up of custom hexagons. I aim to make these hexagons dynamically adjust to fill the entire screen based on the user's resolution, eliminating the need for scrolling. For a previ ...

Creating a New Section in your HTML Table

Can a page break be implemented in an HTML table? I've attempted to add page breaks to the following HTML: <html> <title>testfile</title> <meta http-equiv="expires" content="0" /> <meta http-equiv="cache-contr ...

What is the best way to extend an image to fill the width of a webpage?

Can you advise me on how to expand the image to fit the width of my webpage? If possible, could you take a look at this website: Poklanjam The specific image I am referring to is the one of the city on the left-hand side. What additional CSS code should ...

The function window.stop() using execute_script() does not function properly in chromedriver, but it runs smoothly in geckodriver

When the browser takes too long to load, I attempt to stop it using the window.stop(); method with selenium's execute_script(). Chrome Fails from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities ...

How can the carousel item numbers be adjusted for proper display?

I'm currently working on an Angular app and have encountered a task related to displaying 5 cards using mdbootstrap. I want to showcase these cards in a carousel format, but with a twist - I only want the carousel functionality to be active on mobile ...

Does the html label prevent propagation from occurring?

Can anyone explain why the toggle function is not being executed when clicking inside the box with the black border, but works when clicking outside of it (although not on the checkbox)? var checks = document.querySelectorAll("ul li"); for (var i = 0 ...

The Selenium webdriver encountered difficulty in locating an element within a modal dialog

Having trouble finding buttons on dialog pages despite trying various methods like cssselectors and xpaths. I've included a screenshot of the code for reference. Any recommendations on how to locate these buttons/texts on modal dialogs? Thanks in a ...

Can one retrieve the complete URL of an image that is referenced in CSS using Selenium IDE?

I was pondering whether it is feasible (though I suspect it's not possible..) to fetch the absolute URL of an image that is loaded by a specific CSS class. For instance: <div class="rabbit"> In this case, "rabbit" in the CSS has a background ...

Difficulties encountered with marking tests as skipped in Selenium tests involving sausages utilizing Sauce Labs

Utilizing the sausage framework for executing parallel phpunit-based Selenium web driver tests through Sauce Labs has been successful. However, I encountered an issue when attempting to mark a test as skipped using markTestSkipped(). There have been two me ...

The hover effect does not carry over to duplicated HTML

I successfully added a node, but I forgot to include the hover function of the node in my application. The hover function is not necessary, and I need it to work with ie8 compatibility. Here's my HTML: <div id="appendCell" style="color:green; colo ...

Encountering a problem while extracting information from the McMaster-Carr website

I am in the process of developing a web scraper for McMaster-Carr's website. Specifically, I am looking at extracting data from the page . When I access this page directly through a browser, I am able to view all the product information. However, due ...

Issues arise within the Docker container due to an error stating "unable to initiate a new session thread"

My web crawling solution is built using Python and Selenium, running in a Docker container on an m4.2xlarge EC2 instance with multiprocessing implemented using the Pool method. with Pool(processes=(config.no_of_cpus)) as pool: pool.map(func, items) pool. ...

Is there a way to deactivate a JavaScript function on a specific webpage using CSS?

I've implemented a JavaScript function that triggers a pop-up alert whenever a user attempts to navigate away from the site by clicking on an external link: $("a[target='_blank']").click( function() { return confirm('Please don&apo ...

Adding a Click class can cause significant disruption to the entire CSS layout

I am facing an issue with transforming the X position and appending an additional class to create a slide effect in React. It seems to be working differently compared to using vanilla JavaScript. Below is the code snippet: .inputModal { position: absolut ...

Specflow functionality seems to be intact while running tests, but encounters failures during the debugging process

I've encountered a problem with my specflow test. It runs successfully when in 'run test' mode, but fails when in 'debug' mode. The error message points to a selenium webdriver issue: OpenQA.Selenium.WebDriverTimeoutException: Ti ...

The theme font fails to take effect on the paragraph

As a novice in HTML and CSS, I attempted to use the "Roboto Condensed" font from a WordPress theme for my entire site. Although I successfully implemented this custom font for a menu, I encountered difficulties when trying to apply it to paragraph elements ...

Resize a span's width using the width of another element using only CSS

Website Development <div class="container"> <div id="setter"> <span>some unique content here</span> </div> <div class="wrap"> <span> different text goes here, different text goes here, diffe ...

The Angular 6 ngb-bootstrap modal fails to display due to conflicting bootstrap css within a div element

I've encountered numerous inquiries about modal not displaying, and although I've successfully implemented it in other scenarios, this one presents a unique challenge! In my Wordpress project, I've embedded an Angular app within the admin a ...