Synthetic Monitoring : Scripting in Detect Error Text

Hi Elastic,

Need help on how can I create a script on my use case where, if we detect there is Error display in the page, we want to note it as Fail and not Complete as in success.

For example, below is the sample of error :

Screenshot 2024-06-21 at 2.37.12 PM

Below is the script that I create but its not working, it still showing as Success :

step('Error Checking', async () => {
  try{
    await page.goto('https://****.com');
    await page.locator('[placeholder="Search From Here"]').click();
    await page.locator('[placeholder="Search From Here"]').fill('test');
    await Promise.all([
      page.waitForNavigation(),
      page.locator('[placeholder="Search From Here"]').press('Enter')
    ]);
  }
  catch(e){
    await expect(page.getByText('Error', { exact: true })).toBeVisible();
    console.log("Closed the browser")
    await browser.close();
    throw e;
  }
});

I try to do the error checking by try and catch, but its not working.

It still detect as Complete :

To give you context on the script, I want to check if user did the searching, for example if 'test' query has been input, will it throw error on whether the server is error.

Not sure if I did the correct way but the wording error text is within

html tag.

Below is the inspect of the page :

Need guidance on this thanks!

Hello @aisyaharifin ,

if you want the test to fail if certain locator is present on the page you can do an assertion like this

  step('launch application', async () => {
    await page.goto('https://www.google.com');
  });

  step('it fails if there is text', async () => {
    expect(await page.locator('text=Google').count()).toEqual(0);
  });

you can read more here

Best

Hello @shahzad31 ,

Cool, thanks for the reference, let me try on this and get back to you if it still not working.

Thanks again!