Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created September 9, 2011 22:28
Show Gist options
  • Save mastoj/1207490 to your computer and use it in GitHub Desktop.
Save mastoj/1207490 to your computer and use it in GitHub Desktop.
WatiN observable browser
public class NavigationObserver
{
private HttpStatusCode _statusCode;
private bool _errorOccured;
public NavigationObserver(IE ie)
{
InternetExplorer internetExplorer = (InternetExplorer)ie.InternetExplorer;
internetExplorer.NavigateError += IeNavigateError;
internetExplorer.NavigateComplete2 += internetExplorer_NavigateComplete2;
}
void internetExplorer_NavigateComplete2(object pDisp, ref object URL)
{
if (!_errorOccured)
{
_statusCode = HttpStatusCode.OK;
}
_errorOccured = false;
}
public void ShouldHave(HttpStatusCode expectedStatusCode)
{
if (!_statusCode.Equals(expectedStatusCode))
{
Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Wrong status code. Expected {0}, but was {1}", expectedStatusCode, _statusCode));
}
}
private void IeNavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
_errorOccured = true;
_statusCode = (HttpStatusCode)StatusCode;
}
}
public class ObservableBrowser : IE
{
private NavigationObserver _observer;
public ObservableBrowser()
{
_observer = new NavigationObserver(this);
}
public void ShouldHave(HttpStatusCode expectedStatusCode)
{
_observer.ShouldHave(expectedStatusCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment