3

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?

1 Answer 1

3

The insertion of Thread.Sleep() is typically indicative of a race condition in your test and your test is running out of sync with the thing is it testing.

One suggestion may be to insert a polling loop to wait for the redirect, or an event to indicate the redirect has happened.

See http://www.testingmentor.com/imtesty/?p=652 and read the comments for additional ideas.

1
  • Looks easy enough. Thank you for the suggestion! Oct 8, 2012 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.