I am trying to detect if a server error was encountered during a watin test. I had thought the below code would do the trick however, in usage I am getting an NRE during test execution on this line: (Text.Contains("Server Error")).
Any suggestions on what to do here? A try catch block of some sort seems to not be the right thing to do here.
Thanks.
public class WatinBrowser : IE
{
public WatinBrowser(string url, bool createIn开发者_高级运维NewProcess) : base(url, createInNewProcess)
{
}
public override void WaitForComplete(int waitForCompleteTimeOut)
{
base.WaitForComplete(waitForCompleteTimeOut);
if (Text.Contains("Server Error"))
{
throw new ServerErrorException("A server error occured: " + Text);
}
}
}
public class ServerErrorException : Exception
{
public ServerErrorException(string message): base(message)
{
}
}
I think the issue is that you're overriding the version which takes a timeout parameter. This means it's possible control returns to your code without a document being loaded.
I recommend either:
- Overriding the version of the method which doesn't take a timeout parameter
- Adding a null check before calling the Contains method.
Code example for (2):
if(this.Text == null) throw new TimeoutException();
if(this.Text.Contains("Server Error"))
{
...
}
精彩评论