Get an array of different values โ€‹โ€‹of the same element using a protractor

I am trying to test an application that displays graphs using a rickshaw and d3. tests are carried out using protractor and jasmine. as a side note, I believe that this issue is not particularly relevant to this use case and is more general.

therefore, the test should hover over the graph and collect the text that is displayed for each point ( example ), this array should then be mapped to this array.

Hope this pseudo code illustrates the problem:

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

so the problem is that since I need to decide the size first, I have no way to wait for all the promises to resolve and return an array of promises text that can later be used with waiting ().

EDIT: explicitly mentioned protractor / jasmine.

+5
source share
1 answer

Can't you just use selenium webdriver promise.all ?

 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'); }); }); 

cleaner way:

declare it:

 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); }); }); 

and use it:

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

All Articles