# Synthetics inline script not working per expectation

**URL:** <https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249>\
**Category:** Elastic Observability\
**Created:** [July 9, 2022, 1:52pm UTC](https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249 "2022-07-09T13:52:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wasimasif](https://avatars.discourse-cdn.com/v4/letter/w/a183cd/32.png) [@wasimasif](https://discuss.elastic.co/u/wasimasif)\
**Post date:** [July 9, 2022, 1:52pm UTC](https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249/1 "2022-07-09T13:52:50Z")

</div>

I am trying the following synthetics inline script on elastic cloud.  
The line `expect(productImages.length).toBe(9);` is causing error `Error: expect(received).toBe(expected) // Object.is equality`

Following is the inline code i am trying

```auto
const URL = 'https://elastic-synthetics-demo-ecommerce.vercel.app/';
 step('visit landing page', async () => {
    await page.goto(params.url || URL, { waitUntil: 'networkidle' });
    // check to make sure all products are loaded
    const productImages = await page.$$('.card img');
    expect(productImages.length).toBe(9);
  });

```

If we look into monitor and look at "Script executed at this step" it looks like following. You can notice on $ instead of $$ in front of `('.card img');` I suspect that is the issue as `$('.card img');` is not returning an array.

```auto
async () => {
    await page.goto(params.url || URL, { waitUntil: 'networkidle' });
    // check to make sure all products are loaded
    const productImages = await page.$('.card img');
    expect(productImages.length).toBe(9);
  }

```

---

<div class="post-metadata">

**Author:** ![Raza\_Anis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raza_anis/32/97440_2.png) [@Raza\_Anis](https://discuss.elastic.co/u/Raza_Anis)\
**Post date:** [July 9, 2022, 2:09pm UTC](https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249/2 "2022-07-09T14:09:22Z")

</div>

The Synthetics is using Playwright for browser interactions. And `$$` seems to be not a recommended way to get elements on the page. Instead, you can use the `Locator` Object.

Here's the link to the `Locator` Object's documentation.  
[Locator Class Reference](https://playwright.dev/docs/api/class-locator)

Your code will become something like this when using `Locator`:

```auto
const URL = 'https://elastic-synthetics-demo-ecommerce.vercel.app/';
 step('visit landing page', async () => {
    await page.goto(params.url || URL, { waitUntil: 'networkidle' });
    // check to make sure all products are loaded
    const productImages = await page.locator('.card img');
    expect(await productImages.count()).toBe(9);
  });

```

---

<div class="post-metadata">

**Author:** ![wasimasif](https://avatars.discourse-cdn.com/v4/letter/w/a183cd/32.png) [@wasimasif](https://discuss.elastic.co/u/wasimasif)\
**Post date:** [July 9, 2022, 2:25pm UTC](https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249/3 "2022-07-09T14:25:01Z")

</div>

Cool it worked for me. Thank you for your reply

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 8:36am UTC](https://discuss.elastic.co/t/synthetics-inline-script-not-working-per-expectation/309249/4 "2022-11-04T08:36:10Z")

</div>


