无角度网站 - 检查结果已排序

时间:2015-06-05 17:57:30

标签: javascript arrays selenium protractor e2e-testing

请您告知,如何验证非角度网站搜索结果是按排序顺序显示的吗?

我试图捕获数组中列出的所有项目,然后验证它们是否按顺序排序。但是无法将测试结果推送到阵列中。请问您能看到以下代码片段并就此提出建议。

//element locator for all funds.
var fundingOpportunityList = element.all(by.css('#search-results-list li a article h3'));

//page object
var fundingOpportunityPage = function(){

this.checkFundingItemsSorted = function() {
        //temporary array to store results
         var temp = [];

        //check 10 funding opportunities shown in first pagination tab
        fundingOpportunityList.then(function(items) {
          //Following section working i.e. 10 items shown in each page.
          expect(items.length).toBe(10);  

        }); 

        //This section is not working, unable to save results in Array
        fundingOpportunityList.each(function(elem,index){
            //retrieve all funding opportunities and store in an array
              elem.getText().then(function (text) {
                expect(text).toMatch('junk');  //this line added for debugging
                temp.push(text);    //trying to push text into an array

            });


        }); 

        expect(temp.length).toBe(10);  //length shown as 0
        expect(temp).toEqual('hello'); 

    }

以下是测试结果输出。

  

1)测试网站 - 资助机会清单 - 检查资金清单是   按排序顺序显示消息:        预期0为10. Stacktrace:        错误:预期0为10。

     

2)测试网站 - 资助机会清单 - 查看资金清单是   按排序顺序显示消息:        期望[]等于“你好”。堆栈跟踪:        错误:期望[]等于'#hello'。    3)测试网站 - 资助机会列表 - 检查资金列表按排序顺序显示消息:        预计' 2015年'匹配' junk'。    4)测试网站 - 资助机会列表 - 检查资金列表按排序顺序显示消息:        预期 3Rs奖'匹配' junk'。    5)测试网站 - 资助机会列表 - 检查资金列表按排序顺序显示消息:        预期的3Rs研究资助计划 - 项目拨款'匹配' junk'。

1 个答案:

答案 0 :(得分:0)

而不是each(),请使用map()

var fundingOpportunityList = element.all(by.css('#search-results-list li a article h3'));

var opportunities = fundingOpportunityList.map(function(elem) {
    return elem.getText();
});

expect(opportunities).toEqual(['hello1', 'hello2']);