问题
I'm now tearing out what little remains of my hair. And I think I've lost vision in my left knee.
I have two functions that work. Each one takes a parameter correctly. I would dearly, so very dearly, love one function to take both parameters.
I have a super simple data structure:
var stuff = [
{ name: "Stone", id: "cc45" },
{ name: "Hanley", id: "cc78" }
];
I wish to loop through the structure and run a super simple test on each. Please observe:
for (var ii = 0; ii < stuff.length; ii++) {
var aTile = element(by.id(stuff[ii].id));
aTile.getText().then(
/* magical solution */
);
}
All I'm missing the magical solution. In version A (which lacks magic) I can successfully regard the text that Protractor kindly grabbed:
aTile.getText().then((function (text) {
console.log(" --Protractor Grab--" + text);
}));
In version B (which also lacks magic) I can successfully behold the data I wish to compare the Protractor-grabbed text to:
aTile.getText().then((function (test) {
console.log(" ==Stuff test==" + test);
})(stuff[ii].name));
What I cannot do, no matter how much hair I pull, is to get Protractor to compare both. Effectively, I need to add this:
expect(protractorGrabbedText).toContain(expectedTextFromStuff);
Please kind heroes of code, help me, I implore you.
回答1:
it's very simple. expect()
method have the capability to make a promise to resolve implicitly and use the result to compare with the actual data.You can use something like below,
for (var ii = 0; ii < stuff.length; ii++) {
var aTile = element(by.id(stuff[ii].id)).getText();
expect(aTile.getText()).toContain(stuff[ii].name) //This will take care of resolving the promises.
}
Method -2 :
Since you are using for loop, you need to use closures to get the value of ii
inside the promise. try the below code.
for (var ii = 0; ii < stuff.length; ii++) {
function closure(index){
element(by.id(stuff[index].id)).getText().then(function(text){
expect(text). toContain(stuff[index].name)
})
}
closure(ii)
}
回答2:
I second the above answer. this should work
for (var ii = 0; ii < stuff.length; ii++) {
var aTile = element(by.id(stuff[ii].id));
expect(aTile.getText()).toContain(stuff[ii].name)
}
来源:https://stackoverflow.com/questions/39729393/protractors-promises-parameters-and-closures