Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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.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?

share|improve this question
up vote 3 down vote accepted

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 and read the comments for additional ideas.

share|improve this answer
Looks easy enough. Thank you for the suggestion! – Facio Ratio Oct 8 '12 at 20:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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