Jatin
Jatin

Reputation: 670

Json assertion in Protractor

I am trying to do JSON assertion in my protractor code. My generates an id for the request and for that id get some JSON data back. I need to check using protractor that the error type is 10 (jsonData[0].properties.errror) I have included an expect statement in the method which gets the jsondata not defined error. My protractor code looks like :

   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' ] } ]        

But When i perform this test I get following error :

  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)           

Please advice where I am going wrong :(

Upvotes: 1

Views: 227

Answers (1)

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

Since obj.GetmapFeedbackData() method is asynchronous,You need to make your script wait for the get call to be completed. You need to create a promise and resolve it once the jsonData is obtained. Look at below example,

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.

Upvotes: 1

Related Questions