简体   繁体   中英

Protractor: List remains empty even after I am pushing strings

I'm trying to populate a list with Strings that I obtain from my webpage. I only want to add strings if they are not already in the list.

Here is my code (heavily simplified):

let globalList = [];
it('should populate list', () => {
     getListOfStrings().then(list => {
         list.map(listItem => {
             if(globalList.indexOf(listItem) === -1){
                  globalList.push(listItem);
             }
         });
     });
     expect(globalList).toBe(["Test", "Test2", "Test3"]);
});

The expect statement says that globalList remains empty though. I have made sure the getList() is actually returning a list of strings. And I have tried without the if statement (although I don't see how that would be an issue). I am relatively new to protractor so sorry if I'm missing anything obvious.

Anyone have any ideas?

The expect(globalList) runs before the items are pushed to globalList . To fix this I added a then like so:

let globalList = [];
it('should populate list', () => {
     getListOfStrings().then(list => {
         list.map(listItem => {
             if(globalList.indexOf(listItem) === -1){
                  globalList.push(listItem);
             }
         });
     }).then(() => {
         expect(globalList).toBe(["Test", "Test2", "Test3"]);
     })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM