Retrieve an array containing various values of a single element with the help of Protractor

Currently, I am in the process of testing an application that showcases graphs using rickshaw and d3. The tests are being run with protractor and jasmine. It's worth noting that this question is not specific to this particular scenario but rather more general in nature.

The test involves hovering the mouse over a graph and capturing the text displayed for each point (example here). This text array is then compared against a predefined array.

This pseudo code snippet should help illustrate the issue:

var graph = ... //
var promises = [];
var promise = graphElement.getSize().then(function(size){
    _.times(size, function(i) {
        moveMouse(i, 0); 
        promises.push(graph.element(by.css('.hover-text')).getText());
    });
    return promises;
});

promise.magicallyWaitForAllOfThem();

_.each(promises, function(textPromise){
    expect(textPromise).toBe('something');
});

The problem arises because I need the size to be resolved first, leaving me without a method to wait for all promises to resolve and return an array of text promises that can later be used with expect().

EDIT: I have explicitly mentioned protractor/jasmine.

Answer №1

Is it possible to simplify this process by utilizing Selenium WebDriver's promise.all method?

var graph = ... //
var webdriver = require("selenium-webdriver");

graphElement.getSize().then(function(size){
    var promises = [];
    _.times(size, function(i) {
        moveMouse(i, 0); // move mouse to i-th pixel
        promises.push(graph.element(by.css('.hover-text')).getText());
    });
    return webdriver.promise.all(promises);
}).then(function(promiseResultArray){
    _.each(promiseResultArray, function(textPromise){
        expect(textPromise).toBe('something');
    });
});

A cleaner approach:

Firstly, declare the function:

collectHoverText: function(elem) {
  var strings = [];
  var promises = [];

  return elem.getSize().then(function(size) {
    _.times(0/*logic for number of steps*/, function(i) {
      var x = 0; // logic for step
      browser.actions().mouseMove(elem, {x: x, y: 0}).perform();
      promises.push(elem.element(by.css('.hover-text')).getText().then(function(text) {
        strings.push(text);
      }));
    });

    return protractor.promise.all(promises).then(function() {
      return _.uniq(strings);
    });
  });

Then, utilize the function like so:

var hoverTextPromise = collectHoverText(graph);
expect(hoverTextPromise).toContain('value'); // resolved array here

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

Tips for enabling the OnLoad Event for a React Widget

Hey there! I'm a beginner with React and I'm struggling to call a function once after the component is created. I tried adding an onLoad event in the component creation, but it's not working as expected. The function 'handleClick' ...

gathering information from a group of items and organizing them into a fresh set of items

I have a collection of objects called Lines nestled within the rows array, which is nested within the tabs array. My goal is to extract specific data from the lines array and transfer them into a new array named 'colors'. This is how the initial ...

Is there a way I can replicate this expandable div?

I have successfully implemented a script that expands and collapses. However, I am facing an issue when trying to use two of these scripts on the same page. I attempted to simply add "_two" to all instances of "accordion," but it doesn't seem to be f ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

Set a unique class for several elements depending on a given condition

Is there a way to assign a color class based on the element's value without looping through all elements? Check out my jsfiddle HTML <div> <ul> <li class="MyScore">90</li> <li class="MyScore"> ...

Error message "Unable to access property 'rotation' of an object that does not exist - Three.js"

I'm currently facing an issue where my code is executing as expected, but there are two errors popping up in the console saying "Cannot read property 'rotation' of undefined". It's puzzling because both variables are defined globally. I ...

Avoid triggering jQuery "change" event manually in order to prevent unintentional execution when done programm

In my invoicing system, I have implemented a feature where users can select an item using jquery select 2. Upon selection, the base price and other details are automatically filled in the form. Users also have the option to manually change this price befor ...

Tips for triggering onclick before changing to a new page?

Currently, I am developing a React application and have encountered the following situation: <a href={link} variant="primary" onClick={this.onClickDoSomething}> here. </a> My goal is for the onClick event to trig ...

Guide to adding customized CSS and JavaScript to a specific CMS page on Magento

I'm trying to incorporate a lightbox for video playback on a specific page of my CMS. I've placed my CSS file in JS/MY THEME/JQUERY/PLUGIN/VENOBOX/CSS/myfile.css and the JS files in JS/MY THEME/jquery/plugins/venobox/js/myfile.js, but it doesn&ap ...

Bootstrap Table encountering issues when displaying JSON data from Ajax response

I am currently struggling with an unusual issue. I have received a successful JSON response from the server, which I need to display in my Bootstrap Table. However, I am facing difficulty in displaying the JSON data in the Bootstrap Table while using AJAX ...

Hidden text within a webpage that remains concealed until a specific link is activated

Just getting started with HTML and wanting to add animation to my project. I have a vision of an animation where clicking on an image will split open a half page of text and stuff. It should be similar to this example: ...

Is it possible for me to test events in protractorjs/cucumberjs?

Using Cucumber.js Instead of Java I have an Angular component that incorporates a directive to adjust focus once a certain number of characters are entered into the input field. I want to verify this behavior in my Cucumber tests. 1) Is the webElement.se ...

Uncertainty about the integration of a JavaScript file into a PHP file

Having two PHP files where one is executed through a URL and makes AJAX calls to the second PHP file can present challenges. Sometimes, the AJAX results return HTML content with JavaScript events that do not work as expected. To solve this issue, I have in ...

Is there a way to automatically redirect the server URL when a file is modified?

I am currently experimenting with a function that is supposed to only display a message in the console without redirecting the actual URL of my server when a file is changed. watcher.add("/home/diegonode/Desktop/ExpressCart-master/routes/2.mk"); watche ...

I'm having trouble resolving Selenium setup problems. There seems to be an exception occurring in the main thread, saying "Failed to connect to localhost

I am facing an issue with my Selenium setup. When I try to execute Firefox tests, I encounter the following error: Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost. However, ...

Why is it that this specific ASP.NET + jQuery + JavaScript custom MVC setup isn't displaying any content?

After attempting to incorporate this JavaScript MVC example from into an ASP.NET page, I am facing issues as a beginner with jQuery. Nothing seems to show up on the page, and I'm unsure why. Update: I have included the missing HTML listbox, but now ...

Update the HTML page when switching between tabs

Currently, I am experiencing an issue with tab navigation in HTML. Within my code, I have two tabs named #tab1 and #tab2, each containing a form. The problem arises when I input data into #tab1, switch to #tab2, and then return to #tab1 - the information I ...

Optimal approach to displaying views with Express.js and EJS

Recently, I came across a website that only renders EJS templates using Express.js routing. All the routes are defined in the /routes/index.js file. As the file grows with more routes being added, I am thinking about restructuring it to make it more develo ...

List component in Angular not refreshing after sorting the array in the service

Currently, I am delving into the realm of Angular (version 5+) to enhance my skills by working on a small project. The project involves setting up basic routing functionalities to showcase the ContactList component upon selecting a navigation tab. Addition ...

disableDefault not functioning properly post-fadeout, subsequent loading, and fade-in of new content with a different URL into

After extensive searching, I still haven't found a solution to my problem. My task involves bringing in HTML pages into a div element. I managed to make the content fade out, load new href content, and then fade in the new content. However, I'm ...