Search code examples
c#selenium-webdrivertfsautomated-tests

Selenium and TFBuild - Pages never seem to load


I have written a number of tests for an MVC application using Selenium and the webdriver. These work on my dev machine without problem. We are using the PageFactory design. Currently I have an explicit wait of 3 seconds (although I have tested up to 10 seconds without change) on page load.

A relatively simple test is below: The Test

    [TestMethod]
    public void Can_Log_In_With_Valid_Credential()
    {
        Pages.LoginPage.Goto();
        var success = Pages.LoginPage.Login(Properties.Settings.Default.UserName,
                                            Properties.Settings.Default.Password);
        Assert.IsTrue(success);
        Pages.HeaderPage.LogOut();
    }

The Page and Related Logic

public static class Pages
{
    public static LoginPage LoginPage
    {
        get
        {
            var loginPage = new LoginPage();
            PageFactory.InitElements(Browser.Driver, loginPage);
            return loginPage;
        }
    }
}

public class LoginPage : Page
{
    public static string Url = Properties.Settings.Default.DomainAddress + "/Account/Logon";
    public static string PageTitle = "Log On";

    [FindsBy(How = How.Id, Using = "UserName")]
    private IWebElement _userNameBox;

    [FindsBy(How = How.Id, Using = "Password")]
    private IWebElement _passwordBox;

    [FindsBy(How = How.ClassName, Using = "validation-summary-errors")] 
    private IWebElement _validationErrors;

    [FindsBy(How = How.CssSelector, Using = "div#LoginSubmit.signin input")]
    private IWebElement _submitButton;


    public void Goto()
    {
        Browser.Goto(Url);
        
    }

    public bool IsAt()
    {
        return Browser.Title == PageTitle;
    }

    public bool Login(string username, string password)
    {
        try
        {
            _userNameBox.SendKeys(username);
        }
        catch (Exception)
        {
            _userNameBox = ((IWebDriver) Browser.Driver).FindElement(By.Id("UserName"), 10);
            _userNameBox.SendKeys(username);
        }
        _passwordBox.SendKeys(password);
        _submitButton.Click();

        return Browser.CurrentUrl != Properties.Settings.Default.DomainAddress + "/Account/Logon";
    }
}

The problem arises on the build server. All of the tests fail with the following:

Class Initialization method KepsPortalMvc.UserAccountManagementTests.Initialize threw exception. OpenQA.Selenium.WebDriverTimeoutException: OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds ---> OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"UserName"}.

It appears that Firefox is not even loading the page. I have verified (via Task Manager) that both the webdriver and Firefox are being started by the BuildServiceAccount, however I have not yet found a way of monitoring the network activity (Fiddler is not showing me any HTTP traffic related to that Firefox instance at the very least).

Is there a way (short of putting something like Wireshark on the build server) to let me monitor the WebDriver-Firefox? It doesn't pop-up as it is run as a different account (I am assuming this is why).

Clarification

This is working on my Dev machine, but not on the Team Foundation Build server. We are running Team Foundation on a single server. The TFBuild Service account is what is currently running the Webdriver and Firefox. A process entry for Firefox appears in the Task Manager when it runs as does an entry for WebDriver. A WINDOW for Firefox does NOT. Fiddler does not show me any traffic for that Firefox instance (but I am unsure if that is because nothing is being loaded at all or it just doesn't show me traffic that is loaded in another users session).


Solution

  • A bit of an educated guess here, but it might be that your Test Environment is not set for UI tests. Look at this article on MSDN to see how it should be done.

    Secondly, from my experience such a behavior might occur if the build agent runs a different version of msbuild. Make sure they are the same. For example building with VS 2010 and VS 2012 will use different assemblies, and thus provoking different results at runtime.