Need to Refresh the page by Deleting Cookie Unless Expected value is present in Cookie

I have a scenario where, Every time a cookie value will be 0 or 1 or Cookie will not be present

I wanted to refresh the page unless Cookie value is 0 or Cookie not present.

If cookie value is 1, Delete all the cookie and refresh the page and do the check again.

This has to happen till cookie value is 0 or Cookie itself is not present

Below script runs only once if cookie value is 1. How to make it run till cookie value is not 1 or cookie is not present in loop

log.step("Load Site from URL mentioned in Excel Sheet");
log.step("Load Site from URL mentioned in Excel Sheet");

$browser
  .get(startingurl) //URL is given here
  .then(function () {
    log.info("Starting URL: " + startingurl);
  }) //Access URL mentioned in the Excel Sheet

  .then(function () {
    $browser.sleep(PageLoadSleep3s);
    let track = 0;
    cookie = "test";
    do {
      console.log("Cookie:" + cookie);
      return $browser
        .manage()
        .getCookie(cookie)
        .then(function (text) {
          if (text) {
            console.log("Cookie Value is: " + JSON.stringify(text));
            if (text.value != 1) {
              track = 1;
            } else {
              return $browser
                .manage()
                .deleteAllCookies()
                .then(function () {
                  log.info("Refresh the page");
                  return $browser.navigate().refresh();
                });
            }
          } else {
            console.log("Cookie Not found");
          }
        });
    } while (track == 0);
  });

Hi @Chiddarthan.Dhavidan - Firstly, Synthetics run on a minion which is created every time a synthetic is run. Hence, no session or other data is maintained between synthetic runs.

Looking at your script, I cannot see where you are setting the cookie value, which is possibly why the page is not being refreshed.

Hi @stefan_garnham,

I am not setting the cookie. When page loads, All the cookie will be set. We have third party systems, Which injects cookie named Test. It can have value 0,1 or Test cookie absent.
Based on the values and cookie presence site experience will change.

I wanted to delete all cookies and refresh the page till i see the expected cookie value(Either Test Cookie value 0 or Test Cookie not present)

Every time I refresh the page, Test cookie value will be injected with different values or Not get injected at all.

My script is trying to identify whether cookie value is zero or cookie is not present. If cookie value is 1, I delete all the cookie and refresh the page. This has to continue till i get cookie value as 0 or cookie not present at all

Are you getting a return value from the cookie? It could be that the data types are mismatched for this line:
if (text.value != 1)

Step is getting validated Successfully.

But loop is getting executed only once. If cookie value is 1, Script delete all he cookies, Refreshes the page.

It should again run the loop from beginning. But it is not. It gets executed only once.

Have you tried moving all the logic inside the do loop? I haven’t needed this type of scenario so I am assuming that the promise chain is being broken somehow.

If this does not work then you will need assistance from New Relic support.

Thanks again for jumping in on this @stefan_garnham :slightly_smiling_face: If you could let us know if Stefan’s suggestion works out and if not, we can try and look into this further.

1 Like

Still issue is there. As Stefan said, Promise is getting broken somehow.