5

i'm trying to test an application that displays graphs using rickshaw and d3. tests are implemented using protractor and jasmine. as a side note, i believe the question is not really specific to this use case and is more generic.

so, the test is supposed to hover a mouse over a graph and collect the text that is shown for each point (example). this array is then to be matched against a given array.

i hope this pseudocode 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 the size to resolve first, i don't have a way to wait for all promises to resolve and return an array of text promises that can later be used with expect().

EDIT: explicitly mentioned protractor/jasmine.

1 Answer 1

3

can't you simply use selenium webdriver's 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
Sign up to request clarification or add additional context in comments.

3 Comments

is it any different from protractor.promise.all?
can it be achieved without the callback?
you mean without then? you are doing an async call, you are going to need a callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.