Differences between getSize().height, getAttribute("clientHeight"), and getCssValue("height")

Discover 3 unique methods for determining an element's size in Protractor:

  1. elm.getSize().height;
  2. elm.getAttribute("clientHeight")
  3. elm.getCssValue("height")

What sets these methods apart from each other, or are they comparable in outcomes?

Don't forget that you can also retrieve the element's height value using execute_script().

Answer №1

If you think this answer is incorrect, feel free to downvote. I wanted to leave it as a comment, but it was too lengthy.

In essence, all the methods are essentially wrappers for WebDriverJS's requests, which are part of a complex chain ultimately wrapped up by ProtractorJS:

  1. ProtractorJS executes a command of WebDriverJS.
  2. WebDriverJS sends a GET request to the Selenium server (or Selenium remote control?).
  3. Selenium manages all communication with the browser, providing specific APIs for each browser to use during communication.
  4. Selenium requests information from the browser.
  5. After the browser responds to Selenium, the response is sent back to WebDriverJS as a result of the initial GET request. This response is then transferred to ProtractorJS.

Ultimately, browser behavior plays a crucial role in all of this. Understanding the browser's engine can aid in analyzing aspects like DOM manipulation and viewport rendering.

Upon reviewing the WebdriverIO API documentation, here is a summary highlighting the differences between the three methods:

  • elm.getSize().height involves a GET request to Selenium with the URL
    '/session/:sessionId/element/:id/size'
    . It locates the element first, then retrieves its size. This method relies on the viewport and rendering, requiring them to be calculated for accurate results.
  • elm.getAttribute("clientHeight") corresponds to a GET request to Selenium with the URL
    '/session/:sessionId/element/:id/attribute/:name'
    . It simply accesses the height attribute without necessitating full rendering, as long as the HTML structure is available.
  • elm.getCssValue("height") parallels a GET request to Selenium with the URL
    '/session/:sessionId/element/:id/css/:propertyName'
    . Similarly, it locates the element and looks for the corresponding CSS property in the computed style. This method does not rely on viewport rendering as the computed CSS is accessible before rendering occurs.

However, it's crucial to note that Selenium communicates with the browser even when it's not actively rendering.

  • For instance, if there is a redirect followed by locating an input element, Selenium must wait until the input is available for location. The performance order of the three methods in such scenarios would be:

    1. Retrieving HTML and CSS resources (attribute available) using elm.getAttribute("clientHeight").
    2. Computing CSS (computed CSS available) with elm.getCssValue("height").
    3. Rendering the viewport (computed size available) with elm.getSize().height.
  • Continuing the example, once the above steps are completed and everything is rendered, subsequent requests can be made without waiting for DOM manipulation cycles. At this point, browser engine efficiency becomes paramount. The speed at which calculations can be executed will directly impact performance. (The performance order may align with the previous ranking due to how deeply browsers need to search for responses to Selenium queries, but this might be too ambiguous for a definitive conclusion)

P.S. These are my speculations based on research and documentation. Feedback and opinions are always appreciated! 😀

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

The body onload function fails to run upon the page loading

I'm having trouble with my body onload function not executing. When I load the page, I want to display all records that are selected in the drop_1 dropdown and equal to ALL. I have a script that sends values q and p to getuser.php. The values sent are ...

Positioning 3D objects in Three.js

I am working on a 3D Scene using Three.js with an Earth shape that currently looks like this: https://i.sstatic.net/zXWki.png My goal is to modify it to resemble something like this: https://i.sstatic.net/w4ypV.jpg The coloring, stars, and texture are ...

Checking for text in an HTML table that is dynamically filled by AJAX using Selenium

Currently, I am utilizing the FireFox WebDriver for Selenium. The issue I'm encountering is that a window loads via AJAX and it's causing inconsistency in my tests. One workaround I found was to insert a 3-second delay in the thread, which seems ...

Context Provider executing SetInterval twice

For some reason, the below code in global.js is running twice when I run my react app, but I can't figure out why. I have used setInterval to test, and the intervals are always running two times. Perhaps I am not initializing correctly. Even after rem ...

``Is there a way to access the $attrs data of child DOM elements from within a controller?

Imagine having a controller and multiple children DOMs each with their unique data attributes. <!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> < ...

"Enhance gaming experience by loading game sources using jQuery Ajax and displaying them as

I am currently working with a website system that utilizes ajax to load content without refreshing pages. On one of my pages, I have multiple HTML5 game links being loaded via ajax JSON. When a game is clicked on, it opens as a pop-up displaying the game s ...

Top method to retrieve the text from an element using Selenium

Let's discuss the specific element in question: <tbody> <tr id="tableUsers_0__TR" class="active" name="Tr_tableUsers[0]_Selected"> <td> <input ...

React Native images failing to render at the same time

I have created a React Native app that loads images simultaneously from Supabase storage using a custom hook. The goal is to display these images in a list using SwipeListView: const fetchImages = async (recipes) => { if (!recipes) { return; ...

Using solely JavaScript, the process of eliminating parameter values from a URL on the subsequent page

Seeking assistance in removing the values from the URL after the "?" once transitioning to the next page from the initial page. Despite multiple attempts, I have been unable to find the correct solution. Any help would be greatly appreciated. Desired URL ...

Generate a new JavaScript object and populate it with information

I am looking to create a JavaScript object in the following format: var CarList = {}; I then want to populate it using a for loop (retrieving data from a MySQL database) similar to this: for(var i = 0; i < result.length; i++) { CarList.Construct ...

Experiencing problems with CSS compatibility while transitioning from vanilla JavaScript to React

Currently, I am working on revamping the frontend of an app that was initially built with vanilla javascript and transitioning it to a React framework. However, I'm encountering some challenges with styling the HTML elements. Specifically, the textare ...

When it comes to optimizing JavaScript, what is the best approach for replacing multiple substrings in a string with various strings?

While working on the code I develop and maintain, I encountered an issue. There is a function in my code that takes a query (in the form of a string) and replaces certain substrings within that string with different ones. For instance, if a user inputs th ...

Stop the floating left div from wrapping onto a new line

My frustration is mounting as I have 4 divs set to float left, but the last one keeps wrapping onto a new line on smaller screens. I want them to scale with the screen size so they always stay on the same line, regardless of the screen size. I'm tryin ...

What is the best way to organize nestedGroups in the vis.js timeline?

Running into an issue with the nestedGroups in my code. Despite sorting the array before creating the items and nestedGroups, I'm encountering a problem where the first item is showing up in the last position on the timeline. https://i.sstatic.net/Ne ...

Using JavaScript to make an AJAX request when a dropdown menu from

Looking to update a graph created with Google Chart API using a drop-down menu. Currently facing a few issues. The dropdown is sending a request to the wrong URL. Instead of /graph/, it's sending data to the current page, which is /services/. This ...

Any suggestions for a quicker method to manage state changes between OnMouseDown and OnMouseUp events in React?

I am attempting to create a window that can only be dragged from the top bar, similar to a GUI window. Currently, I have set up state updates based on OnMouseDown and OnMouseUp events on the top bar. However, I am experiencing slow updates as it seems to ...

Error: Phonegap displaying incomplete or corrupted image

Currently, my Android application is being developed with Phonegap. Users have the ability to take photos that are then stored in a mysql database (medium-blob column) using a simple INSERT INTO query without altering the data. These images are then sent s ...

Why does setting the variable value as an argument in an AngularJS function result in the variable becoming undefined?

The following is the Main Controller implementation: angular.module("HomePageApp", ["BaseApp"]) .controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) { var self = this; self.posts = BaseSer ...

Using Jquery to Add HTML Elements Rather than Changing the Layout

One thing I'm worried about is that when I pull raw HTML data from the database as a string using ajax, everything works fine. However, when I try to add it into a DIV, it just displays the code as is without converting it into a formatted layout. T ...

Is there a way to deactivate the toggle button in my code?

<label class="switch switch-yes-no" id="disable_'+id+'" style="margin: 0;font-weight: bold;"> <input class="switch-input" type="checkbox" id="disable_'+id+'" /> <span class="switch-label" data-on="Enabled" data-off="Disab ...