TestStack / TestStack.Seleno

Seleno helps you write automated UI tests in the right way by implementing Page Objects and Page Components and by reading from and writing to web pages using strongly typed view models.
http://teststack.github.com/TestStack.Seleno/
MIT License
180 stars 60 forks source link

Can not initialize Seleno - System.TypeInitializationException #231

Closed oesolberg closed 8 years ago

oesolberg commented 8 years ago

I am new to Seleno so I might have misunderstood something,

I am trying to get Seleno to work. Following the course Automated ASP.NET MVC Testing: End to End on Pluralsight by Jason Roberts.

But for some reason I cant get Seleno to work. I create a solution with the ASP.Net MVC (.net 4.6). Works like it should. WebApplication (solution) \WebApplication \Tests

After doing the Install-packages I have the following packages-config I add a test project with the following packages: package id="Castle.Core" version="3.3.3" targetFramework="net46" package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" package id="NUnit" version="3.0.1" targetFramework="net46" package id="Selenium.Support" version="2.45.0" targetFramework="net46" package id="Selenium.WebDriver" version="2.49.0" targetFramework="net46" package id="TestStack.Seleno" version="0.9.11" targetFramework="net46"

I tried to make a little test but I always get the following error on initialising the BrowserHost.

System.TypeInitializationException : The type initializer for 'Tests.BrowserHost' threw an exception. ----> System.NullReferenceException : Object reference not set to an instance of an object.

at Tests.DudeTest.Something() in c:\dev\WebApplication\Tests\DudeTest.cs:line 12 --NullReferenceException at TestStack.Seleno.Configuration.WebServers.ProjectLocation.GetSolutionFolderPath() in c:\ConsoleBuildAgent\work\6625a30e8ee728ba\src\TestStack.Seleno\Configuration\WebServers\ProjectLocation.cs:line 42 at TestStack.Seleno.Configuration.WebServers.ProjectLocation.FromFolder(String webProjectFolderName) in c:\ConsoleBuildAgent\work\6625a30e8ee728ba\src\TestStack.Seleno\Configuration\WebServers\ProjectLocation.cs:line 33 at TestStack.Seleno.Configuration.SelenoHost.Run(String webProjectFolder, Int32 portNumber, Action`1 configure) in c:\ConsoleBuildAgent\work\6625a30e8ee728ba\src\TestStack.Seleno\Configuration\SelenoHost.cs:line 51 at Tests.BrowserHost..cctor() in c:\dev\WebApplication\Tests\BrowserHost.cs:line 17

I found that this has been a problem before, but then the solution was to have the .sln file correctly located. As it is now I think I have It right.

Is there anything I am missing or am I using the wrong .Net version?

BrowserHost.cs

using TestStack.Seleno.Configuration; namespace Tests { public static class BrowserHost { public static readonly SelenoHost Instance=new SelenoHost(); public static readonly string RootUrl; static BrowserHost() { Instance.Run("WebApplication", 12345); RootUrl = Instance.Application.Browser.Url; } } }

TestClass.cs

using NUnit.Framework; using OpenQA.Selenium;

namespace Tests { [TestFixture] public class DudeTest { [Test] public void Something() { BrowserHost.Instance.Application.Browser.Navigate().GoToUrl(BrowserHost.RootUrl+@"Lub");

        var emailBox = BrowserHost.Instance.Application.Browser.FindElement(By.Id("email"));
    }
}
oesolberg commented 8 years ago

After downloading the master source and playing with it I might have found the culprit. The samples in master works fine (after getting a webdriver since Firefox can't be found even though it is installed on my machine).

When I test with a var directory = new DirectoryInfo(Environment.CurrentDirectory); I get a directory produced by JetBrains Resharper. I don't have a well tested answer yet, but it might be the Resharper test runner causing Seleno not finding the webapplication.

mwhelan commented 8 years ago

Hi @oesolberg

The problem is that you haven't upgraded the Selenium.Support package to 2.49. It's important that both the Selenium packages - WebDriver and Support - are the same version.

oesolberg commented 8 years ago

Thanks for the tip, but it did not pan out.

I am almost 100% sure it is Resharper test runner that has every test running in its environment. And that is not the test-directory.

I ended up making my own hardcoded path to the websolution. Later I will make it a bit more dynamic. (I also had to find a solution to start Firefox since it does not start automatically anymore)


        var location = ProjectLocation.FromPath(@"C:\dev\path\to\webproject");

        Instance.Run(
            configure => configure
            .WithRemoteWebDriver(
                () => new FirefoxDriver(
                new FirefoxBinary(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"),
                new FirefoxProfile()))
            .UsingLoggerFactory(new ConsoleFactory())
            .ProjectToTest((new WebApplication(location, 49184)))
        ); 

mwhelan commented 8 years ago

I created a new class library, installed Seleno and ASP.Net MVC from NuGet, and encountered the same issue you had with System.TypeInitializationException. I upgraded Selenium WebDriver and Support to the latest versions and it fixed the problem. I am using the ReSharper runner as well.

This issue looks a lot like issue #218 . Do you want to have a look at your solution folder structure and see if it is causing the same problem that was there?

oesolberg commented 8 years ago

I am running the almost latest version of Resharper Ultimate (10.0.1) with VS2015 update 1. I know I had this running on VS2013 with an earlier version of Resharper.

Just to confirm I created a new solution with a webapplication(MVC) and a testproject. Both located default. But after doing update of both Webdriver and Support I still get the same error.

Packages.config after updates

<packages>
  <package id="Castle.Core" version="3.3.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
  <package id="NUnit" version="3.0.1" targetFramework="net461" />
  <package id="Selenium.Support" version="2.49.0" targetFramework="net461" />
  <package id="Selenium.WebDriver" version="2.49.0" targetFramework="net461" />
  <package id="TestStack.Seleno" version="0.9.11" targetFramework="net461" />
</packages>

No problem. It works as long as I insert the correct directory as shown in my previous post.

mwhelan commented 8 years ago

Hi @oesolberg . I'm glad you got it working. Specifying the location of the project is a good solution, which is catered for by the overload you have used. There is work we can do to improve the smarts of how Seleno searches for the project directory and also to throw a more meaningful exception when things don't work out so it is clear to the user what happened during the project search.

mwhelan commented 8 years ago

Hi @oesolberg . I have looked at this more closely and it seems that the issue is with changes NUnit has made and the impact it has on Environment.CurrentDirectory. (My repro of your issue used NUnit 2.64, which is why I could not replicate the issue). As I expect a lot of people to move to be using NUnit 3 I think this is an issue we will have to address in Seleno.

You can read more about the NUnit 3 changes here (and the issues it has created for people relying on the old NUnit 2 behaviour).

oesolberg commented 8 years ago

Thanks. Good to see that it is not just me (even though it is sad that this will affect everyone going over to nUnit 3). Hopefully this thread will help others if you can't fix it in Seleno.

mwhelan commented 8 years ago

Hi @oesolberg . This fix has been released to NuGet as version 0.9.56.

oesolberg commented 8 years ago

Thanks for all the support and quick update of the NuGet package!

josenbobby commented 5 years ago

I am getting this same error now, since i was following the tutorial. I am using Microsoft test. System.TypeInitializationException .... same exception when the BrowserHost.Instance.Application.Browser.Navigate().GoToUrl(BrowserHost.RootUrl); is called