Forum Discussion

SlickRick's avatar
SlickRick
Frequent Contributor
9 days ago

Web browser page URL not loading

So i have a test that verifies that when i click on a button in my WPF application my company website gets loaded.

I then verify that the browser opens on the correct link.

Basically we have over 50 applications that we are testing with this 'contact' us button.

The problem is that i'm really struggling in having this test stable. It fails randomly for some reason and i am spending countless hours thing to make it work always

For unknown reason my code would sometime not be able to retrieve the URL from the loaded page.

I have tried using the WaitPage(<myURL>, 30000) and a lot of variations. 

Here is my code that seems to be the most stable so far:
(you can see that the page is found, but will return an empty string for the URL actual value)

/**
 * Verifies whether the specified website URL is loaded in the browser and logs the result.
 * Retries up to 5 times before logging an error if the URL is not found.
 * 
 * Param {string} url - The expected URL to verify.
 */
function VerifyWebsiteURL(url) {
  const maxRetries = 5; // Maximum number of retries allowed.
  let retryCount = 0;   // Counter to track the number of retries.

  do {
    // Delay the execution for 5 seconds to allow the browser and page to load.
    aqUtils.Delay(5000);

    // Check if the browser is open and visible.
    if (Aliases.browser.WaitProperty("Exists", true) && Aliases.browser.Visible) {

      // Obtain the page object for the currently open browser page.
      let theBrowser = Aliases.browser; // Retrieve the browser so that .Page doesn't fail.
      let page = theBrowser.Page("*");

      Log.Message("Comparing URLs: Expected '" + url.toLowerCase() +"' Actual: '" + page.URL.toLowerCase() +"'");
      // Compare the current page's URL with the expected URL.
      if (page.URL.toLowerCase() == url.toLowerCase()) {
        // Capture a screenshot of the current desktop state for logging purposes.
        let desktopPicture = Sys.Desktop.Picture();

        // Log a checkpoint to indicate that the expected page is visible.
        Log.Checkpoint(
          "The " + url + " page is visible as expected.", 
          undefined, 
          undefined, 
          undefined, 
          desktopPicture
        );

        return; // Exit the function as the URL is verified successfully.
      }
      
    }

    // Increment the retry counter.
    retryCount++;

    // If the maximum retries are reached, log an error and exit the function.
    if (retryCount >= maxRetries) {
      // Capture a screenshot of the current desktop state for logging purposes.
      let desktopPicture = Sys.Desktop.Picture();

      // Log an error if the URL does not match the expected value after retries.
      Log.Error(
        "The " + url + " page was not found after " + maxRetries + " retries.", 
        undefined, 
        undefined, 
        undefined, 
        desktopPicture
      );

      return; // Exit the function as the URL verification failed after retries.
    }

  } while (retryCount < maxRetries);
}

Here is the outcome of my test:

I am getting clue less and this one is hard to debug since it is random and we have 6 agents runnings testcomplete and they all seems to be failing from time to time. And it is not always the same project...

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm using TC v15.55.53.7 and the latest version of Chrome. The following code, works without any issues.

    However, since I'm now using Windows 11, running against Edge, doesn't work!

    Since you are launching the browser externally via your WPF application, it is advisable to close  all Edge browser instances to perform the test.

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Frequent Contributor

    The right and correct condition for your test to work properly is to use a single browser, single instance, and a single tab. When you perform the check it always goes to first browser, first instance, and first tab.

    When Edge is closed, it maintains a session in memory, process keeps running. Try to force the above condition by checking for all browsers and terminating process https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/window-and-process/terminate-method-process-object.html 

    function CloseProcess()
    {
      var arr = ["iexplore", "firefox", "chrome", "MicrosoftEdge"];
      arr.forEach(function(element){
        Close_Process_Instances(element);
      } );

      function Close_Process_Instances(ProcessName)
      {
        var p = Sys.FindChild("ProcessName", ProcessName);
        while (p.Exists)
        {
          p.Terminate();
          p = Sys.FindChild("ProcessName", ProcessName);
        }
      }
    }

    Also, not sure it applies, but try to clear TestComplete browser.page cache https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/common-for-all/refreshmappinginfo-method.html?sbsearch=RefreshMappingInfo%20Method 

    • SlickRick's avatar
      SlickRick
      Frequent Contributor

      Hi Hassan, thank you for your reply!

      Sadly I already have some closing logic for the edge process and i tried so many variations of it.

      I also tried what you suggested to do a theBrowser.RefreshMappingInfo and page.RefreshMappingInfo and it doesnt work either.

      What is weird is that the actual page seems found, but the URL not loaded at all. See:

      I will continue to investigate but at this stage im pretty much getting clueless...


      FYI, i did try to follow the edge setup as stated there:
      Preparing Edge for Web Testing | TestComplete Documentation 

      • Hassan_Ballan's avatar
        Hassan_Ballan
        Frequent Contributor

        Hello SlickRick you are testing a desktop application but do you have the web module license to interact with webpages?

        When I manually navigate to the URL, in TestComplete when I inspect "Object Browser", I do see the page with the URL value. In your screen shot your page value is blank and you are viewing the BrowserWindow and not the page.