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

How to put code into Seleno that catch js errors #197

Open sirj77 opened 9 years ago

sirj77 commented 9 years ago

Hi,

Could someone help me with putting some piece of code into Seleno? I need to catch javascript errors on the Home page at least. I found a good solution - JSErrorCollector. Below you can see some implementation of this:

// create the Firefox instance
string xpiDir = ... the directory where the file JSErrorCollector.xpi is ....;
FirefoxProfile ffProfile = new FirefoxProfile();
JavaScriptError.AddExtension(ffProfile, xpiDir);
IWebDriver wd = new FirefoxDriver(ffProfile);

// run test
...

// check for javascript errors
IEnumerable<JavaScriptError> jsErrors = JavaScriptError.ReadErrors(wd).Where(err => !ShouldIgnoreJavaScriptError(err));
if (jsErrors.Count() > 0)
{
    Assert.Fail("..."); 
}

How and where in my test or settings I can put it and then use it in my tests? My test looks like this:

[Test]
        public void CanBuyProductOnline()
        {
            this.BDDfy();
        }

        public void Given_that_I_am_not_a_logged_in_user()
        {
            _homePage = Host.Instance.NavigateToInitialPage<HomePage>()
                .ProductsMenu
                .GoToMusicPageForAnonymousUser()
                .GoToProductPage()
                .AddToCartByUsingByNowButton()
                .GoToCheckoutPage();
        }

namespace Name_ofProject
{
    public static class Host
    {
        public static readonly SelenoHost Instance = new SelenoHost();
        static Host()
        {
            Instance.Run(configure => configure
                .WithWebServer(new InternetWebServer("test_url")));
        }
    }
}
...
MehdiK commented 9 years ago

Hi @sirj77,

You could do something like this:

namespace Name_ofProject
{
    public static class Host
    {
        public static readonly SelenoHost Instance = new SelenoHost();
        static Host()
        {
            // create the Firefox instance
            string xpiDir = ... the directory where the file JSErrorCollector.xpi is ....;
            FirefoxProfile ffProfile = new FirefoxProfile();
            JavaScriptError.AddExtension(ffProfile, xpiDir);
            IWebDriver wd = new FirefoxDriver(ffProfile);

            Instance.Run(configure => configure
                .WithWebServer(new InternetWebServer("test_url"))
                .WithRemoteWebDriver(wd));
        }
    }
}

Then use the jsErrors wherever you need to make the assertion: perhaps in one of your Then steps.