Protractor / WebDriverJS将元素值作为对象数组返回

时间:2016-09-13 12:00:42

标签: javascript jasmine promise protractor webdriverjs

我来自Java + WebDriver背景,是Protractor,WebdriverJS和Jasmine的新手。在显示的图像中,我试图将鼠标悬停在所有气泡上并获取工具提示值(城市,销售,连接)并将它们作为对象分配给数组并将其返回给调用函数。谁能告诉我如何在这种情况下创建一个对象数组?我试图从我的spec文件中声明它。

当我调用此函数时, return arr; 会在其余代码之前运行。似乎是由于异步行为。

this.getSalesVolumeDistribution = function() {
var arr = [];
var icons = element.all(by.css('#map-container svg>circle'));
icons.map(function(elm) {
        browser.actions().mouseMove(elm).perform();
        var toolTipCity = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(2)'));
        var toolTipUnitsSold = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(3)'));
        var toolTipUnitsConnceted = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(4)'));

        toolTipCity.getText().then(function(text) {
            var cityVal = text.replace('City: ', '').replace(',', '');
            console.log(text.replace('City: ', '').replace(',', ''));
            var soldVal = toolTipUnitsSold.getText().then(function(text) {
                return text.replace('Units Sold: ', '').replace(',', '');
            });
            var connVal = toolTipUnitsConnceted.getText().then(function(text) {
                return text.replace('Units Connected: ', '');
            });

            arr.push({
                city: cityVal,
                sold: soldVal,
                conn: connVal
            });

            });
    });
return arr;
};

enter image description here

2 个答案:

答案 0 :(得分:2)

你应该返回http://gerardvivancos.com/2016/04/12/Single-and-Bulk-Renaming-of-Objects-in-Amazon-S3/的结果,其中将是一个解析为对象数组的承诺

this.getSalesVolumeDistribution = function() {
    var icons = element.all(by.css('#map-container svg>circle'));

    // CHANGE WAS MADE HERE v
    return icons.map(function(elm) {
        browser.actions().mouseMove(elm).perform();
        var toolTipCity = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(2)')).getText();
        var toolTipUnitsSold = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(3)')).getText();
        var toolTipUnitsConnceted = element(by
            .css('#map-container g.highcharts-tooltip tspan:nth-of-type(4)')).getText();

        // CHANGE WAS MADE HERE v
        return protractor.promise.all([toolTipCity, toolTipUnitsSold, toolTipUnitsConnceted]).then(function(tooltips) {
            var cityVal = tooltips[0].replace('City: ', '').replace(',', '');
            var soldVal = tooltips[1].replace('Units Sold: ', '').replace(',', '');
            var connVal = tooltips[2].replace('Units Connected: ', '');

            // CHANGE WAS MADE HERE v
            return {
                city: cityVal,
                sold: soldVal,
                conn: connVal
            };
        });
    });
};

请注意我如何将return置于此处(标有注释)。并注意protractor.promise.all()有助于解决工具提示文本的多个承诺。

然后,如果你需要断言函数的结果,将它放入expect() - 它将隐式解析promise,然后进行断言,例如:

expect(myPageObject.getSalesVolumeDistribution()).toEqual([
    {city: 'El Paso', sold: '344', conn: '321'},
    {city: 'New York', sold: '500', conn: '600'}
]);

答案 1 :(得分:1)

量角器中的map函数将允许您返回值数组:http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map

所以不要推入数组而只是做: 返回 {      城市:cityVal,      卖:soldVal,      conn:connVal }