C# Selenium Integration Tests using Chrome Ghost PhantomJs headless driver

A lot has changed since my last post using Selenium to test web pages. Then you had to install a few things both on the client, browser and server. Today it has been simplified a lot. All you need really is the webdriver and you are in business. You can use the webdriver on your development machine and the server machine as well to run the Selenium tests. In my case I’m using the Ghost-PhanthomJS webdriver as I need to do headless testing ( no GUI ). Since it’s a lot harder to run tests automatically on the server if there is need for GUI, it will complicate things a lot, has to do with security restrictions etc.

Lets take a look at what is needed.
Create a new C# project in VS

Install xUnit Package Manger Console in VS using nuget.
Note I’m specifically using 1.9.2 and 2.0 as I know these two work on the build machine, I had trouble with other version combinations.
PM> Install-Package xunit -Version 1.9.2
Install xUnit VS Runner
PM>Install-Package xunit.runner.visualstudio -Version 2.0.0

Install the Chrome webdriver and libraries
PM> Install-Package WebDriver.ChromeDriver

Install the PhantomJS headless driver and libraries
PM> Install-Package PhantomJS

Then we write the test that will run after the build does the web deployment to the local server. The sample below just loads google.com and checks for the title, then shuts down.

//
using System;
//
using Xunit;
//
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.PhantomJS;

namespace IntegrationTesting
{

    public class HelloIntegration
    {
        // Proofs that google can be pulled up in the browser and has the right title
        [Fact]
        public void GoogleCheckTitle()
        {
            // Init
            IWebDriver driver = new PhantomJSDriver();

            // Test
            driver.Navigate().GoToUrl(“http://www.google.com”);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
            var title = driver.Title;

            // Proof
            Assert.Equal( “Google”, title);

            // Close down
           driver.Quit();

        }
    }
}

If you are using TFS you also need to point your build agent to the xUnit runner, in my case I just check it in as part of the project and set it like that.
Path to custom test adapters
$(Build.SourcesDirectory)\TestWebSite\xunit_runner\

That’s all you need, then you can write your own tests to test specific functionality tailored to your needs. Selenium is pretty powerful for Integration testing.

msTest Initialization UnitTest Framework error – No types found implementing

I started getting the following error when running specific UnitTests on the server, and only on the server. Running the tests from Visual Studio both using msTest and TestDriven was fine. But obviously something was missing. The good part about using different runners to run tests is that you often find niche cases that you would have otherwise missed. I was able to duplicate the behavior using msTest from the command line on the development machine. Then I noticed that when msTest runs it creates a whole new directory for each run ( based on timestamp ) in order to run the tests in isolation. I started to realize my problem as the files needed for the test need to be copied to the new directory. As I was using reflection to load some of the dlls msTest would have no instructions that I needed those dlls to be copied over as well.

Initialization method Framework.Conventions.UnitTests.ConventionAdapterExtensionsTests.Initialize threw exception. Framework.FrameworkUsageException: Framework.FrameworkUsageException: No types found implementing IConventionAdapterProvider.

The trick is to declare the needed dlls as deployment items for your UnitTests test class and you are in business. In my case I needed these three dlls.

[DeploymentItem("Framework.Compiler.dll")]
[DeploymentItem("Framework.Conventions.Compiler.dll")]
[DeploymentItem("Framework.Logging.dll")]
[TestClass]
public class ConventionAdapterExtensionsTests
{

Log4Net configuration and scope extension

I had the need to setup Log4Net logging in a new project the other day. It is fairly easy to setup, just add the listeners you want in the configuration files. Then use it, of course you get most things for free the log level etc. We also wanted to make it super easy to use Scope, so I added a LogExtension that takes care of that. The idea is if you forget to close the scope it will be automatically done once the scope object gets garbage collected. Below is example code including UnitTests.

logger_sample

Could not load type Microsoft.VisualStudio.Services.Common.VssHttpClientBase

If your getting the following error on your TFS server web interface odds are that you are using incompatible version between TFS and VS or other Microsoft product. The object in question was moved from the common Dll to another. In my case I installed TFS RC and then never version of VS2013, after that I got the error.

Could not load type ‘Microsoft.VisualStudio.Services.Common.VssHttpClientBase’ from assembly ‘Microsoft.VisualStudio.Services.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

To remedy the situation I upgraded TFS 2013 with the RTM version. After the upgrade I’m not getting the error anymore ant the TFS web interface works as expected.

Fireflies

I had an idea about writing a small code to animate fireflies. On Christmas break I was bored and I figured I had a few hours to play with it. I also wanted to use some tools I hadn’t used yet and figured that was a good time to do Javascript since I haven’t done much Javascript. I fired up the editor ( Sublime ) and used the browser to run / debug, Firefox and FireBug of course. There is a bit of a difference using this setup compared to writing Java code in Eclipse which I’m used to. First off you don’t get the compilation in Sublime and secondly you have to switch to another program to load and debug. Switching between the two was what annoyed me. Later I though, what if I can find a tool that is an editor and can Ftp to a server as well and runs within the browser. If on each save you can Ftp it to a sever that is serving the Javascript then I could do it all from the browser. I went looking around and fount Net2Ftp which is supposed to at least come close to the tool I’m wishing for. I will have to try it out the next time I’m coding in Javascript. The other thing that got me is that there is no good way of declaring your own classes, you have to work with prototypes and such, why that is beats me.

As you can see from the code, it’s not pretty.

If you want to see the Javascript in action click here.

fireflies

Java Spring MVC 3.2.0 UnitTests

I had a bit of a fight today, I wanted to UnitTest some Java Spring MVC code that I’m using for a demo application. But after a lot of Googling, StackOverflow and hitting different documentation of Spring 3.0, 3.1 and 3.2 I finally came to an acceptable solution. Mind you a minimal sample to test MVC controller without having to go crazy with decelerations, attributes, injections and so on and so forth, you catch my drift. There has been a lot of movement from those three versions I mentioned and finally the spring UnitTesting lib has now been folded into Spring itself, this is good news.
Btw, I found the answer in the Spring MVC showcase on GitHub, where else ?

So lets take a look at what you need, bare minimum just the way I like it.

Using Maven, you will need the ref in your POM file

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.0.RELEASE</version>
            <scope>test</scope>
        </dependency>

The controller class I’m using comes pretty much directly from creating a new MVC Spring project from Eclipse.

package us.kristjansson.springTest;
//
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
//
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */

@Controller
public class CxHomeController
{
       
        private static final Logger logger = LoggerFactory.getLogger(CxHomeController.class);
       
        /**
         * Simply selects the home view to render by returning its name.
        */

        @RequestMapping(value = “/”, method = RequestMethod.GET)
        public String home(Locale locale, Model model)
        {
                logger.info(“Welcome home! The client locale is {}.”, locale);
                //
                Date date = new Date();
                DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
                String formattedDate = dateFormat.format(date);
                model.addAttribute(“serverTime”, formattedDate );
               
                return “home”;
        }
       
}

Then lets look at the Test itself.

//
package us.kristjansson.springTest;
//
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.springframework.test.web.servlet.MockMvc;
//
import org.junit.Before;
import org.junit.Test;

public class TxHomeController
{
         //
         private static final Logger logger = LoggerFactory.getLogger(TxSample.class);
         private MockMvc mockMvc;

         @Before
         public void setup() throws Exception
         {
                 logger.info( “Setup CxHomeController” );
                 this.mockMvc = standaloneSetup(new CxHomeController()).build();
         }

         @Test
         public void TestController() throws Exception
         {

                 //
                 logger.info( “testing CxHomeController” );
                
                 // The CxHomeController’s view is "home" without content
                 this.mockMvc.perform(
                           get(“/”))
                                 .andExpect(status().isOk())
                                 .andExpect(content().string(“”))
                                 .andExpect(view().name(“home”) 
                                 );

         }
       
}