Discrepancies in CSS outcomes when using geckodriver versus ChromeDriver

When I run my Capybara tests, I am facing a challenge with validating CSS properties as each webdriver returns the selector value in a different format.

In one of my project pages, there is an element with a CSS property background-color specified in hexadecimal. Here's an example of the CSS:

#selected-colour-red {
    background-color: #ff0000;

However, when the tests execute, the webdrivers seem to be looking for the RGB equivalent instead of the hexadecimal value. To deal with this inconsistency, I analyze and convert the values to the corresponding RGB so that the RSpec matcher can compare against what the webdriver sees:

And(/^I should see the color "(.*?)"$/) do |color|
  case color
    when 'red'
      rgb_color = 'rgb(255, 0, 0)'
    when 'green'
      rgb_color = 'rgb(0, 255, 0)'
    when 'blue'
      rgb_color = 'rgb(0, 0, 255)'
  selected_color = page.find(:css, 'div#colors-grid div#selected-color-' + color)
  pp selected_color.style('background-color')   # debug - inspect the webdriver's returned property
  expect(selected_color.style('background-color')).to have_content(rgb_color)
end

The line with pp prints out the actual value seen by the webdriver during the test.

Tests using geckodriver pass because the expected and observed values match:

    And I should see the text "The selected color is <color>" # features/colors.feature:14

    Examples:
      | color |
{"background-color"=>"rgb(255, 0, 0)"}
      | red    |
{"background-color"=>"rgb(0, 255, 0)"}
      | green  |
{"background-color"=>"rgb(0, 0, 255)"}
      | blue   |
3 scenarios (3 passed)
15 steps (15 passed)
0m3.205s

On the other hand, chromedriver tests fail due to the returned CSS property being in rgba format:

 And I should see the text "The selected color is <color>" # features/colors.feature:14

    Examples:
      | color |
{"background-color"=>"rgba(255, 0, 0, 1)"}
      | red    |
      expected to find text "rgb(255, 0, 0)" in "{\"background-color\"=>\"rgba(255, 0, 0, 1)\"}" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/colors.rb:47:in `/^I should see the color "(.*?)"$/'
      features/colors.feature:17:13:in `I should see the color "red"'
...
3 scenarios (3 failed)
15 steps (3 failed, 3 skipped, 9 passed)
0m2.051s

I aim to avoid writing driver-specific code as it becomes challenging to maintain.

  • Should I use a regular expression as a matcher in the expectation?
  • Is there a way to change the representation format of RGB values in chromium or firefox?
  • Can the test be written in a manner that explicitly matches the hexadecimal value in the CSS?
  • Why do the drivers interpret this value differently?

Answer №1

The reason why chromedriver and geckodriver have different return values is not clearly defined, but it could be due to the flexibility of the specification at https://www.w3.org/TR/webdriver/#get-element-css-value. They have chosen to provide distinct yet valid results.

It is impossible to directly match the hexadecimal value in CSS using Capybara. A regex should be used with the match_style matcher rather than accessing style directly.

expect(selected_colour).to match_style('background-color' => /rgba?\(255, 0, 0(, 1)?\)/)

Alternatively, you can utilize the style filter for the have_css matcher to simplify the process:

expect(page).to have_css('div#colours-grid div#selected-colour-' + colour, style: { 'background-color' => /rgba?\(255, 0, 0(, 1)?\)/ } )

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

Replace the default disc icon with a more detailed icon for UL lists

I am working with an unordered list and I want to use <details> to show a sublist for one of the items: <ul> <li><details> <summary>details bullet</summary> <ul> <li>detail 1</li> ...

Customizing the appearance of individual columns in the Material-UI DataGrid

My goal is to center an IconButton within a DataGrid table, all of which are MUI components. Despite reading the documentation and trying various methods, I haven't achieved the desired result yet. In my latest attempt, I implemented the following: G ...

How can I use JavaScript to modify the style of the first unordered list (ul) element without affecting the

function displayMenu(){ var parentElement = document.getElementById("menuItem1"); var lis = parentElement.getElementsByTagName("ul"); for (var i = 0; i < lis.length; i++) { lis[i].setAttribute("style","display: block"); } } When the button is clicke ...

Here's a new version: "Strategies for deactivating a text field in a desktop application that

I am currently using WiniumDriver to automate a desktop application. My goal is to disable a text field once a value has been entered into it. // Launch the desktop application WiniumDriver driver = null; DesktopOptions option = new DesktopOptions(); o ...

`Only the latest test case result is displayed in Extent report`

Using Extent report version 3.0 in Java with TestNG classes. One of the classes is called ExtentManager.java package framewrk; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.e ...

What is the best way to ensure that an element is enabled before clicking on it using Java Selenium?

Hello, I am completely new to working with Selenium and need advice on the best way to wait for an element to become enabled. I am currently in the process of writing a test that involves downloading a report which can only be done once a specific status ...

What could be causing the issue with object-fit not functioning properly within a container with a max

My goal is to insert a video into a container that has a width of 100% and automatically adjusts its height while maintaining the aspect ratio, but with a maximum height set. I want the video to completely fill the container even if some parts are cropped ...

Customize the look and feel of your website using jQuery's

Is there a way to use an icon from the jQuery Themeroller as a toggle button in a Ruby on Rails project? I want the icon to switch colors when clicked, but I'm not sure how to accomplish this. Any advice on how to achieve this functionality? ...

How to vertically center Bootstrap 4 columns while maintaining uniform height

Having trouble with aligning bootstrap 4 columns' height and vertical alignment? Check out this JSFiddle to see the issue. I'm working on designing a layout with items in both desktop and mobile views, all within borders. The challenge lies in ...

The toggle feature in Bootstrap is stuck in the "on" position and refuses to "untoggle."

Currently, I am working on developing a basic website using Laravel and Vue.js. To ensure that the website is responsive, I integrated Bootstrap 4 into the project. For the most part, everything appears to be functioning correctly - the navbar collapses as ...

Height and width properties in JQuery not functioning properly on SVG elements in Firefox

I am attempting to create an SVG rectangle around SVG text. I have noticed that when using .width() or .height() on SVG text, Chrome provides the expected results while Firefox does not. Here is a link to a demo I created on jsfiddle. $(document).ready(fu ...

Compel the browser to initiate a reflow by adjusting the CSS

I am currently in the process of building a responsive image slider without relying on jQuery, using CSS3 transitions. The setup is quite simple: there's a viewport with a UL inside, containing relatively positioned LIs that are left floated. Howeve ...

How to transform the bootstrap 4 hamburger icon into a three dots menu icon using css only

The standard bootstrap 4 navigation bar button showcases a classic hamburger icon. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="T ...

The rendering of the font appears distinct when viewed on an iPad compared to when displayed

Visit this website. The menu displays correctly on desktop, but on iPads, the font appears larger leading to a line break. Is it because the selected font isn't loading and defaulting to Times New Roman instead? It seems likely since I experience the ...

I'm trying to find the correct path link to access salary information on Glassdoor's website. Can anyone help me out?

I am attempting to extract the salary information from Glassdoor's salary data website. However, the XPath link I have provided below does not seem to be functioning. Could you verify if I have done it correctly? salary_estimate = driver.find_element ...

Guide to implementing proportional height for an element using JQuery 3.3.1

I am currently working on dynamically setting the heights of elements with a specific class using JQuery in order to achieve a 16:10 ratio based on their width. However, I'm encountering an issue where the height doesn't seem to be applied and re ...

Switch the div's class when the parent element is chosen

I have encountered a complex issue involving two problems for which I am unable to find a solution. In the default state without hover or selection, this div displays with a purple border when selected. My concern is: How can I change the class of the ico ...

Tips for adjusting image size using media queries

How can I ensure that the image resizes correctly on smaller screens? Currently, the image spills over the container and there are gaps between the top/left of the container and the image. Should I adjust the image size in the media query or expand the con ...

Utilizing JavaScript/jQuery to activate a Bootstrap navbar post-page load

Having trouble triggering Bootstrap's navbar with plain JavaScript/jQuery while using Bootstrap 3.3.6 and jQuery 1.11.3. The data structure provided for my application cannot be modified in terms of adding classes or ids. <ul> <li ...

Create a background for a dropdown list that appears unreachable, positioned above all other elements

I am encountering an issue with my autocomplete function where the drop down list appears unreadable as it is overlapping other elements on the page. The problem lies in the fact that I did not manually create the drop down list, but rather it is generate ...