After conducting thorough research, I have come across various code samples to handle expected alert boxes. However, I am facing difficulty in finding a solution for handling random alerts that may or may not appear.
The website I'm working with is particularly challenging as it lacks proper IDs for elements, experiences timeouts, network failures, and other issues.
Out of all the tests I run, 98% of them are successful without encountering any alert box errors. However, the remaining 2% displays an alert box with the message "Error: jQuery not found," causing all subsequent tests to fail due to unexpected alert errors.
My initial query is whether the error is caused by a flaw in my code (see below) or if it originates from the website itself. If the latter is true, could someone provide me with an example of how to handle a potential alert box without disrupting the test? The switchTab() test runs first followed by the setDates() test. The alert box error occurs after switching tabs while the page is loading. Despite attempts to use a deferred promise to manage the alert on catch the error, the failure persists as browser.switchTo().alert() encounters an alert that typically doesn't exist before it can be caught. Any assistance provided would be greatly appreciated.
this.switchTab = function(){
browser.getAllWindowHandles().then(function(handles){
browser.switchTo().window(handles[1]);
browser.sleep(2000);
var lockBoxTitle = element(by.css('td.title'));
browser.driver.wait(EC.visibilityOf(lockBoxTitle),5000);
});
}
this.setDates = function(yesterdayDate){
browser.sleep(3000);
//handleAlert();
startDateTextBox.clear();
startDateTextBox.sendKeys(yesterdayDate);
endDateTextBox.clear();
endDateTextBox.sendKeys(yesterdayDate);
retrieveBtn.click();
browser.sleep(5000);
expect(validateStart.getText()).toEqual(yesterdayDate);
expect(validateEnd.getText()).toEqual(yesterdayDate);
}