Monthly Archives: June 2014

Visual Studio Tips – By Video

I just wanted to share with the world some productivity tips which I use when using Visual Studio 2013. I’ve confined the tips to Visual Studio 2013, which is not running Resharper. The tips are conveyed in this video:

If you have particular thing that you use, please share it in the comments!

ExceptionAssert.Throws instead of ExpectedException

Jimmy Bogard has already written about the perils of using the ExpectedException attribute in unit testing (MS Test and NUnit have that attribute). NUnit has dealt with this by including an Assert.Throws<T> method (it also has an Assert.Catch<T> method).

My testing framework of choice (MS Test) does not have an equivalent (disappointing). So, I set about writing one. The implementation looks like this:

public static class ExceptionAssert
{
        public static void Throws<T>(Action code, string exceptionMessage = null, string message = null, params object[] args) where T : Exception
        {
            try
            {
                code.Invoke();

                Assert.Fail("No exception was thrown by the code under test.");
            }
            catch (Exception exception)
            {
                if (string.IsNullOrWhiteSpace(exceptionMessage))
                {
                    Assert.AreEqual(exception.GetType(), typeof(T), message, args);
                }
                else
                {
                    Assert.AreEqual(exception.GetType(), typeof(T), message, args);
                    Assert.IsTrue(exception.Message.StartsWith(exceptionMessage, StringComparison.OrdinalIgnoreCase));
                }
            }
        }
}

And using it in a unit test as so:

[TestMethod]
[TestCategory(TestCategories.MembershipIntegration)]
public void CreateUserAccountWithNoTenantThrowsException()
{
	var membershipService = new MembershipService();

	ExceptionAssert.Throws<ArgumentNullException>(
		() =>
			membershipService.CreateUser(null, 
			TestFirstName, 
			TestLastName, 
			TestFirstName + " " + TestLastName,
			TestPassword, TestEmail
			));
}

Now, we won’t get the false negative in the event that the exception is thrown in the constructor of the MembershipService class, rather than in the method under test, membershipService.CreateUser.

If you want to verify the text of the exception message, you can do that too:

[TestMethod]
[TestCategory(TestCategories.MembershipIntegration)]
public void CreateUserAccountWithNoTenantThrowsException()
{
	var membershipService = new MembershipService();

	ExceptionAssert.Throws<ArgumentNullException>(
		() =>
			membershipService.CreateUser(null, 
			TestFirstName, 
			TestLastName, 
			TestFirstName + " " + TestLastName,
			TestPassword, TestEmail
			),
			"The argument was null"
			);
}

I’ve also included parameters for a failure message and formatting of that message (which is a normal overload of the Assert methods).

Selenium and Hovering Over Page Elements

Many webpages use mouse-hovering as part of their user experience. This is despite the fact that those pages may be loaded using mobile devices (which quite often employ a touch-hold gesture to get the same effect as a mouse-hover). Quite often, a menu will require a mouse to hover over it before it expands to reveal submenu items. And quite often, the submenu items are not even in the dom until that hover is effected.

It is not possible to truly hover using Selenium. However, it is possible to simulate hovering.

In browsers apart from Internet Explorer, the following code represents how you would hover over a menu and then click on one of its sub-menu items:

var firefoxBinary = new FirefoxBinary("PathToFirefox.exe");
var firefoxProfile = new FirefoxProfile();
IWebDriver webDriver = new FirefoxDriver(firefoxBinary, firefoxProfile);

//  Now for the hover magic
var elementToHoverOver = webDriver.findElement(By.Id("elementId"));    
var action = new Actions(webDriver);  
// Move to the Main Menu Element  
action.MoveToElement(elementToHoverOver).Perform();  

// Wait for the SubMenu to reveal  
var elementWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(WaitTimeout));
var subMenuItem = elementWait.Until(ExpectedConditions.ElementIsVisible(By.Id("subMenuItemId")));
subMenuItem.Click();

I’m not sure why you need to make the call to Perform() on the Actions object returned by MoveToElement. But that’s how you hover.

Now for Internet Explorer. In a nutshell, you execute some JavaScript via Selenium which would invoke a mouseenter event. So, you first cast the webDriver to IJavaScriptExecutorand call ExecuteScript to execute some JavaScript like the following:

var internetExplorerDriverService = InternetExplorerDriverService.CreateDefaultService(DriverPath);
var internetExplorerOptions = new InternetExplorerOptions();

IWebDriver webDriver = new InternetExplorerDriver(internetExplorerDriverService, internetExplorerOptions);

((IJavaScriptExecutor)webDriver).ExecuteScript("$('#main_menu ul li.parent:eq(1) a').mouseenter();");

The code for finding and executing a sub-menu item is the same as above.