opened
zackr
username_0
**Context:**
- Playwright Version: 1.4.0
- Operating System: Mac
- Node.js version: 14.9.0
- Browser: All
- Extra: Nothing specific about it.

Here's a sample script showing that $$ is incorrectly returning an empty array when it should return an array of 14 elements:

const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch();

    const page = await browser.newPage();
    await page.goto('https://www.roguefitness.com/rogue-hi-temp-bumper-plates');

    let groupedItemsHandle = await page.$(`#super-product-table > div.grouped-item`);
    if (groupedItemsHandle) {
        console.log('Regular selector worked and returned : %s', await groupedItemsHandle.innerText());
    }

    const numItems = await page.evaluate(() => document.querySelectorAll('#super-product-table > div.grouped-item').length);
    groupedItemsHandle = await page.$$(`#super-product-table > div.grouped-item`);
    assert.deepEqual(numItems, groupedItemsHandle.length);

    await browser.close();
})();

```

This seems to be caused by the fact that src/server/selectors.ts:_queryAll isn't setting up the context correctly. Changing line 90 from:
const context = await frame._context(allowUtilityContext ? info.world : 'main');
to:
const context = await frame._context(info.world);
would make it work, but I haven't had the time to figure out why _query, _queryArray and _queryAll all have different ways of setting up the context so it's probably a side-effect of something else.
[BUG] $$ selector seems to be using invalid context
issue
created
dgozman
username_1
Your analysis is correct - we pass `allowUtilityContext` as an optimization to avoid adopting elements one by one - this might be slow for large results. I think correctness is more important though. We might employ some kind of a heuristic, or maybe an option to say "do it faster, in the main context".

For the record, this particular page overrides `Array.from` with some partial implementation, so that `Array.from(new Set(['foo']))` does not work.
comment
created
zackr
username_0
Thanks for looking at this! FWIW $$eval(...) is likely broken too, so with the above mentioned code:
    const numEvalItems = await page.$$eval(`#super-product-table > div.grouped-item`, (es) => es.length);
    console.log(numEvalItems);
is going to print 0, because _queryArray is unconditionally using _mainContext. 

Either it would have to get the same adoptToMain code you've added to queryAll or start using _context(info.world) plus explicit adoption like _query does. In general unifying that logic between all those three would seem like a good idea. 
I can open a separate issue for $$eval after your change lands if you'd prefer to track it there.
comment
created
dgozman
username_1
Unfortunately, we cannot do the same trick with `$$eval` because the function passed to `$$eval` should be able to access main context javascript objects.

```
await page.evaluate(() => window.expectedDivs = 42);
await page.$$eval('div', divs => divs.length === window.expectedDivs);
```
comment
closed
dgozman
username_1
issue