Home > Mobile >  How do I extract and push text from promise all strings to map or array?
How do I extract and push text from promise all strings to map or array?

Time:07-20

I was trying to extract the chrome extensions id and I was able to get the Ids of the loaded chrome extensions.

        browserPage = await browserContext.newPage();
        await browserPage.goto("chrome://extensions");
        await browserPage.locator('text=Developer mode This setting is managed by your administrator. >> #knob').click();
        
        

Working Code: It is printing the IDs into the console.

await browserPage.locator('#extension-id').allTextContents().then(id => {
            console.log(id);
        });

Output:

[
  'ID: kjkkkbelkplchpnbmhlmcbfmgjbkdpkj',
  'ID: cciedebhmaekejckbbnjedloddbkfbpd'
]

I don't know how to extract the Ids into a map or array.

Solutions tried:

const ids: never[] = []
        await browserPage.locator('#extension-id').allTextContents().then(id => {
            ids.push(id);
        });

Error: Argument of type 'string[]' is not assignable to parameter of type 'never'.

CodePudding user response:

This seems to work.

let ids: string[] = [];
await browserPage
  .locator('#extension-id')
  .allTextContents()
  .then((arrStr) => {
    arrStr.forEach((ele) => {
      ids.push(ele.substring(4));
    });
  });

CodePudding user response:

allTextContents returns a Promise<string[]>, so you only need to assign it to the variable:

const ids = await browserPage.locator('#extension-id').allTextContents();

Then ids will be inferred to be the type string[] for you.

  • Related