Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 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 http://www.testingmentor.com/imtesty/?p=652 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
add comment

Your Answer

 
discard

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.