Jason在量角器中断言

时间:2016-10-06 09:42:41

标签: javascript json node.js selenium protractor

我正在尝试在我的量角器代码中执行JSON断言。我为请求生成一个id,并为该id获取一些JSON数据。我需要检查使用量角器,错误类型是10 (jsonData [0] .properties.errror)我在方法中包含了一个expect语句,它获取了jsondata未定义的错误。我的量角器代码如下:

   var obj=require('./../../bin/clientrest-wikvaya.js');
var UI = require('./../ui.js');
var co = require('co');


var ui  = new UI ();

describe("MapFeedback: road-missing", function () {
    ui.setSmallScreenSize();
    // ui.testLogger(100);

    it('test', co.wrap(function*() {
        yield browser.get(ui.createStartLink());
        yield ui.REPORT_ROAD.click();
        expect(browser.getCurrentUrl()).toContain("report_road");

        yield ui.ROAD_NEW.click();
        expect(browser.getCurrentUrl()).toContain("choose_location_road_new/road_new");

        yield ui.zoomIn (16.5);

        yield element(By.css('button[ng-click="doneChooseLocation(newLocation);"]')).click();
        expect(browser.getCurrentUrl()).toContain("road_new");

        yield ui.ROAD_NAME.sendKeys('TEST');
        yield ui.HIGHWAY_OPTION.click();
        yield ui.SUBMIT.click();
        expect(browser.getCurrentUrl()).toContain("submit");
        yield ui.waitSubmit();
        expect(yield element(By.css('div[ng-show="mapFeedBack.submitState==\'success\'"]')).isDisplayed()).toBeTruthy();

        var reportId = yield browser.executeScript('return mapFeedBack.reportId');
        console.log("road-missing id: "+reportId);
        obj.GetmapFeedbackData(function(jsonData){
            console.log("------GET CALL RESULT------");
            console.log(jsonData);

    },reportId);

        expect(jsonData[0].properties.error).toEquals(10);  
    }));
});

JsonData:

[ { previousGuid: '173171025e42f839c77c9390298b392674a70482',                                                                                                                          
    layerId: 'wikvaya/feedback/main',                                                                                                                                                  
    lastUpdateTS: 1475746314112,                                                                                                                                                       
    coordinates: [ 12.5604, 55.67653, 0 ],                                                                                                                                             
    guid: 'a81ada32fd0f4b6ee52ba2cf8659b48256c0094e',                                                                                                                                  
    id: -1253236,                                                                                                                                                                      
    type: 'Point',                                                                                                                                                                     
    nodeId: 'feedback',                                                                                                                                                                
    createdTS: 1475746313348,                                                                                                                                                          
    properties:                                                                                                                                                                        
     { zoomLevel: 20,                                                                                                                                                                  
       jobStatus: 'INPROGRESS',                                                                                                                                                        
       attachments: [],                                                                                                                                                                
       error: 10,                                                                                                                                                                      
       type: '1',                                                                                                                                                                      
       oda: 1,                                                                                                                                                                         
       jobId: -840132,                                                                                                                                                                 
       jobSolution: 'NONE',                                                                                                                                                            
       v: '2.5',                                                                                                                                                                       
       appId: 'MapFeedbackWebGeneric',                                                                                                                                                 
       speedCat: '2',                                                                                                                                                                  
       pageURL: '',                                                                                                                                                                    
       lang: 'en',                                                                                                                                                                     
       roadname: 'TEST' },                                                                                                                                                             
    states: [ null, null, null, 'WIP' ] } ]        

但是当我执行此测试时,我得到以下错误:

  Failures:                                                                                                                                                                              
1) MapFeedback: road-missing test                                                                                                                                                      
  Message:                                                                                                                                                                             
    Failed: jsonData is not defined                                                                                                                                                    
  Stack:                                                                                                                                                                               
    ReferenceError: jsonData is not defined                                                                                                                                            
        at Object.<anonymous> (c:\Users\jasharma\gitrepo\mapfeedback-test\test\road\road-missing.js:40:10)                                                                             
        at next (native)                                                                                                                                                               
        at onFulfilled (c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\co\index.js:65:19)                                                                                     
        at Promise.invokeCallback_ (c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\selenium-webdriver\lib\promise.js:1329:14)                                                 
        at TaskQueue.execute_ (c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\selenium-webdriver\lib\promise.js:2790:14)                                                      
        at TaskQueue.executeNext_ (c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\selenium-webdriver\lib\promise.js:2773:21)                                                  
        at c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\selenium-webdriver\lib\promise.js:2652:27                                                                           
        at c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\selenium-webdriver\lib\promise.js:639:7                                                                             
        at process._tickCallback (internal/process/next_tick.js:103:7)                                                                                                                 
    From: Task: Run it("test") in control flow                                                                                                                                         
        at Object.<anonymous> (c:\Users\jasharma\gitrepo\mapfeedback-test\node_modules\jasminewd2\index.js:81:14)                                                                      
    From asynchronous test:                                                                                                                                                            
    Error                                                                                                                                                                              
        at Suite.<anonymous> (c:\Users\jasharma\gitrepo\mapfeedback-test\test\road\road-missing.js:12:5)                                                                               
        at Object.<anonymous> (c:\Users\jasharma\gitrepo\mapfeedback-test\test\road\road-missing.js:8:1)                                                                               
        at Module._compile (module.js:413:34)                                                                                                                                          
        at Object.Module._extensions..js (module.js:422:10)                                                                                                                            
        at Module.load (module.js:357:32)                                                                                                                                              
        at Function.Module._load (module.js:314:12)           

请告诉我出错的地方:(

1 个答案:

答案 0 :(得分:1)

由于obj.GetmapFeedbackData()方法是异步的,您需要让脚本等待get调用完成。您需要创建一个promise并在获得jsonData后解析它。请看下面的例子,

var defer = protractor.promise.defer();
obj.GetmapFeedbackData(function(jsonData){
        console.log("------GET CALL RESULT------");
        console.log(jsonData);
        defer.fulfill(jsonData[0].properties.error);
},reportId);
expect(defer.promise()).toEqual(10); //This line will wait untill the `obj.GetmapFeedbackData()` is completed.