Nick Jones
Nick Jones

Reputation: 4475

Testing RequireJS application with FluentAutomation?

I'm attempting to write some UI tests for a RequireJS-based Backbone application, utilizing FluentAutomation.SeleniumWebDriver and NUnit. The HTML page in question contains a typical data-main attribute for loading the RequireJS module for the application. My struggle is in properly detecting when the application is fully loaded with these tools; the only thing I've gotten to work consistently so far is using an explicit wait in seconds, like so:

I.Open("http://myapp")
    .Wait(5)
    .Enter("foo").In("input[name=username]")
    .Enter("bar").In("input[name=password]")
    .Click("button")
    .Wait(5)
    .Expect.Text("Welcome").In("#welcome");

This is less than ideal -- my test as written above will always take at least 10 seconds to run, when in reality the app might be "ready" much faster than that. What I'd like to be able to do is something like this:

I.Open("http://myapp")
    .WaitUntil(() => I.Assert.Exists("input[name=username]"))
    .Enter("foo").In("input[name=username]")
    .Enter("bar").In("input[name=password]")
    .Click("button")
    .WaitUntil(() => I.Assert.Exists("#welcome"))
    .Expect.Text("Welcome").In("#welcome");

However, this doesn't work -- using WaitUntil here actually seems to prevent the app from loading, for reasons unclear to me, as I simply receive timeout exceptions after the default wait period (30 seconds), stating that it was unable to locate the element in question within that timeframe.

I see that Selenium 2 provides a WebDriverWait for this kind of scenario, and possibly that would work here, but am unsure how I would use this within FluentAutomation (and a quick search of the FluentAutomation code on GitHub doesn't seem to indicate it's in use within the library).

What can I use in FluentAutomation to properly wait for a RequireJS module (or DOM loaded by it) to be ready?

Additional details:

This might not be a RequireJS compatibility problem at all. I've looked further into the app and found that what's happening after the Click("button") is actually a window.location.replace -- not a RequireJS async module load. It's the one place in the app that this is occurring, apparently. So, is a window.location redirect a known scenario that would cause problems with WaitUntil, and is there an alternate approach (aside from a simple Wait(5)) that would properly handle this?

Upvotes: 2

Views: 354

Answers (0)

Related Questions