Handling popups in browser tests

Handling popups in browser tests

JavaScript and Basic Auth modals

Synthetic browser tests automatically handle JavaScript modals:

  • alert modals are closed.
  • prompt and confirm modals are answered Lorem Ipsum. For Basic Auth popups, specify the associated credentials in your browser test configuration, in Advanced Options > HTTP Auth:

Application popups

Anchored popups

If a popup appears at a specific point of your journey, you can record a step to close it and allow this step to fail using the corresponding option. This way, your test knows how to behave in case a popup appears. If the popup does not show up, the step fails without causing the whole test to fail.

Moving popups

If the time at which these popups appear in a session is not predictable, check with the third party providing the popup to see if they can create a rule that prevents the popup from appearing during your browser test execution. They could, for example, provide you with a cookie that you can input below the dedicated Advanced option of your test.

Alternatively, use one of these methods to ensure your popup is closed and your test is able to continue its journey:

  • Create a JavaScript assertion at the beginning of your browser test to regularly try to close the popup:

    if (document.querySelector("<ELEMENT>")) {
      return true;
    } else {
      return new Promise((resolve, reject) => {
        const isPopupDisplayed = () => {
          if (document.querySelector("<ELEMENT>")) {
            clearInterval(popup);
            resolve(true);
          }
        };
        let popup = setInterval(isPopupDisplayed, 500);
      });
    }
    
  • Record steps to close the popup, add them between all your other browser test steps, and select the Allow this step to fail option for each of them.

Further Reading