If you request a URL with Selenium, the first thing you want to check is whether the page can be displayed, or whether it generates a Server Error message.
If there a server error occurred, the string “Server Error in” occurs in the generated HTML text. We then strip everything that is not inside the <BODY> tag, remove all HTML tags (they do not contain any relevant information in case of an error), and log and store the remaining error message.
selenium.Open(sUrl);
selenium.WaitForPageToLoad("30000");
string sHtml = selenium.GetHtmlSource();
if (sHtml.Contains("Server Error in "))
{
verificationErrors.AppendLine("");
verificationErrors.AppendLine(sUrl);
NUnitLog.Error(sUrl);
try { sHtml = sHtml.Substring(sHtml.IndexOf("<BODY")); } catch { }
try { sHtml = sHtml.Substring(0, sHtml.IndexOf("</BODY>")); } catch { }
try { sHtml = Regex.Replace(sHtml, "<[^>]+?>", ""); } catch { }
try { sHtml = Regex.Replace(sHtml, " +", " "); } catch { }
try { sHtml = Regex.Replace(sHtml, @"(\s\n)+", "\n"); } catch { }
NUnitLog.Error(sHtml);
verificationErrors.AppendLine(sHtml);
}
selenium is of type ISelenium, verificationErrors is a StringBuilder, conforming to the Selenium convention. NUnitLog is a custom class handling logging and error messages.
