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...