I have begun to use SpecFlow and WatiN to automate UI tests in IE9, and am encountering a maddening problem which may have nothing do to with either, but hey. I have the following step:
[Then]
public void Then_I_should_see_the_model_detail_screen()
{
Regex r = new Regex(Nav.ModelView + @"1[0-9]+", RegexOptions.IgnoreCase);
bool isMatch = r.IsMatch(WebBrowser.Driver.Url);
Span span = WebBrowser.Current.Span(Find.ById("ScreenTitle"));
string title = span.Exists ? span.Text : "";
Assert.IsTrue(isMatch);
Assert.AreEqual(_DummyModel.Title, title);
}
If I place a breakpoint at the first Assert statement, title is an empty string, even though span.Exists is true and span.Text has a value. Furthermore, isMatch is false even though the pattern matches.
However, if I place a breakpoint at the top of the method and step through, isMatch is true and title is populated with the value of span.Text.
The test fails if I don't run it in debug, too, which is of course the main issue.
EDIT: If I place a Thead.Sleep(1000) at the top of this method, it works as expected. I forgot to mention that this method is running during/after a redirect. My guess is that this method is evaluating before the redirect is finished, but in debug mode I'm able to see the "live" values of the WebBrowser.
So is there a better way to wait for the redirect to finish, or to check that I've been redirected?