AMA: C# WebDriver Questions

omayer asks…

how to use test data from xlsx in c# webdriver, thank you in advance.

My response…

I haven’t done this as I’ve managed data/scenarios using SpecFlow, but there’s this post that should help you.

omayer also asks…

Handling popup Windows in C# WebDriver – Closing the popup window , finding elements on popup window, locating element inside popup window and frameset

My response…

Stackoverflow is a good resource for these types of questions; there’s some examples on how to do this here.

pallavi asks…

Which tool can we use for report generation in selenium with C#

My response…

This very much depends on which framework you are using to manage your tests. I have used SpecFlow which allows a formatted HTML report of test results.

pallavi also asks…

Can we use selenium with coded ui in visual studio? If I use with coded ui then what template it will follow

My response…

My experience with CodedUI tests in C# has only been negative. When I looked at it, the approach Microsoft used was to generate a huge number of lines of CodedUI test code which I couldn’t understand or make into reusable objects. I much prefer Selenium/WebDriver for any web based tests, and to use White to test any non-web based UIs. I am not familiar with CodedUI templates since it has been a long time since I looked at it.


AMA: test automation tooling for MS web stack?

Sean asks…

 Now that you’ve gained experience testing with JavaScript, do you have a preference for tooling? Would you lean more towards JS than Watin for a MS web stack?

BONUS: Any tips on testing modules that rely on dynamically created SQL? Common sense suggests testing to the nearest clearly defined “business value” and eventually separating concerns/refactoring. Any weakly held strong options?

My response…

I still think you should write your tests in the same language as your app, so for a MS web stack I would lean towards SpecFlow/WebDriver (see SpecDriver for an example). I am not sure whether Watin is actively maintained or whether it supports browsers other than IE, but I know the C# WebDriver bindings are increasingly solid.

Using Mocha in JavaScript for e2e tests continues to be painful, we’re patching lots of different aspects of it, which makes me think we would be probably better off using a different tool that does what we want.

Bonus answer: I think your idea makes sense as there’s elements of context and unpredictability, so starting with one approach and letting it evolve over time through refactoring is often the best outcome.

AMA: SpecDriver license?

Erick Hagstrom asks…

I just stumbled across SpecDriver. Looks promising, but I don’t see a license file on github. Under what terms is it offered? (I’m hoping that it is free for commercial use. I am investigating options for test automation on behalf of my company.)

My response…

Yes, everything I write on Github publicly is free for any use.

I have added an explicit GPLv3 license to alleviate any licensing concerns.

Aside: I can’t believe it’s been over five years since I created that repo on a whim, it seems to have helped a lot of people and I’ve used that project pattern subsequently many times myself.

C#: Avoiding the WebDriverException: No response from server for url

When it comes to automated testing, there’s not much worse than intermittent failures, especially when they stem from the driver itself. The current version of the C# WebDriver bindings has such a failure, but I worked out a reasonable way to avoid it happening. Basically it involves creating a WebDriver extension method that I use instead of Driver.FindElement, which tries a number of times to find the element, ignoring the exception that is intermittently raised.

I hope you find this useful if you’re consuming WebDriver in C#.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace Extensions
    public static class WebDriverExtensions
        public static SelectElement GetSelectElement(this IWebDriver driver, By by)
            return new SelectElement(driver.GetElement(by));
        public static IWebElement GetElement(this IWebDriver driver, By by)
            for (int i = 1; i <= 5; i++ )
                    return driver.FindElement(by);
                catch (Exception e)
                    Console.WriteLine("Exception was raised on locating element: " + e.Message);
            throw new ElementNotVisibleException(by.ToString());

SpecDriver now includes examples

A fellow ThoughtWorker, Paul Hammant, recently announced his intent for QuickTest Professional. And his method? Provide some new technology tutorials for newbies, in the form of a series of open source automated tests for craft site

He asked me to assist by providing some SpecDriver examples, and since I enjoy doing this stuff I am more than happy to help out.

So, I’ve updated the SpecDriver repository on Github to include some feature files, and working tests. It was a lot of fun to write these tests, because is awesomely testable. Its nicely formatted code with proper ids and class names makes automating against it a real treat.

The benefit of doing this exercise is that it’s meaty enough for me to get some insight into how other people code real automated tests. For example, I’ve had a look into a couple of the code samples and I see a lot of XPath selectors. I personally really dislike XPath stuff, just like I dislike XML as I find it hard to read, and less intuitive than using straight identifiers. I am proud that there isn’t a single XPath used in my examples.

Another thing that stood out to me was how embedded some of the stories are. For example, in the JBehave example the features are located under //etsy-stories/src/main/resources/stories, which took me some time to locate at first, and every time I forget where they are. I much prefer having a features folder in root that makes it pretty obvious what’s in there.

One thing I am very interested in is seeing how people specify tests. I found two of Paul’s scenarios repetitious so I moved them into a single scenario outline. Moving them into a scenario outline made me think about some different ways to test, so I added a new scenario, which didn’t require much more code, but strengthened my feature.

For example, Paul’s two scenarios:

Scenario: Advanced Search for a hat
  Given I am searching on
  When I specify the Knitting sub category
  And I search for hat
  Then there are search results

Scenario: Advanced Search for a ring
  Given I am searching on
  When I specify the Jewelry sub category
  And I search for ring
  Then there are search results

became my:

Scenario Outline: Advanced Search for items in various categories that exist and don't exist
  Given I am searching on
    When I specify the <Sub Category> sub category
    And I search for <Search Term>
    Then I should see <Search Results> search results for <Search Term> 

  | Sub Category | Search Term  | Search Results |
  | Knitting     | 'hat'        | some           |
  | Jewelry      | 'necklace'   | some           |
  | Jewelry      | 'specdriver' | no             | 

While I was at it, I noticed there was a spelling correction feature of the advanced search. I also thought it was a good idea to test that this was working correctly.

Scenario: Misspelling a word corrects search automatically
  Given I am searching on
  When I specify the Knitting sub category
  And I search for 'scalf'
  Then I should see some search results for 'scarf'
  And I should see that the search was for 'scarf' instead of 'scalf'

Now I’ve done this exercise using SpecFlow and WebDriver in C#, I’m keen to do it again using Cucumber & Watir (WebDriver) in Ruby, to compare the implementation. When I am done I will post it on here for you all to see. Enjoy.