javascript - Jasmine testing runs code in illogical order when iterating over mutliple results in a div? -
i'm trying write test grab contents of div , compare them expectation.
i know can expect multiple results, i've been trying iterate on results 1 @ time , concatenate them 1 string can compare against expectation.
code snippets below don't seem work expected. console returns "result returned: "
no results before returns "result inside while: 1"
, "result inside while: 1 2"
.
this doesn't make sense me... i've explained enough i'm trying do. code isn't great @ moment because i've been chopping , changing bits test happening.
a 'tl;dr' summary of i'm hoping can :)
- how can iterate on each item returned
element.all
line , concatenate values string function return test? - can me understand why if/else appears true when has been set 0 beforehand (i'm sure
items.length
2 in case) - can point me place read on promises? have feeling poor understanding of promises holding me back...
test:
var resultpage = require('../page/result.page.js'); var expectedarticle1details = browser.params.articleintransitintransit[0]; var expectedarticle2details = browser.params.articlenotcompliantpossibledelay[0]; var expectedids = ' ' + expectedarticle1details.trackingid + ' ' + expectedarticle2details.trackingid expect(resultpage.alltrackingidsarevisible()).toequal(expectedids);
resultpage:
this.alltrackingidsarevisible = function () { var result = ""; element.all(by.css(trackingids)).then(function (items) { var = 0; while (i <= items.length) { if (i === items.length) { console.log("result returned: " + result); return result; } else { items[i].getattribute("innerhtml").then(function (value){result += value; console.log("result inside while: " + result)}) } = + 1; } }) };
i'll explain why happens first, suggest solution. call while loop, async (a promise), adds queue, , goes , next line synchronously, i+=1;, loop ends before promises resolve, result
didn't change yet, , returned empty.
what should use, recursive function on element.all(by.css(trackingids)), whith signature:
addtoresult(elementarray, i, result) {};
and each time do:
items[i].getattribute("innerhtml").then(function (value){ addtoresult(elementarray, + 1, result += value) })
dont forget stop recursion when i === elementarray.length
, , return result
Comments
Post a Comment