Home » Selenium » Testing EXTjs with Selenium using C#

Testing EXTjs with Selenium using C#


For the last two years, about 99% of the work I’ve been doing has been JavaScript programming using EXTjs.  The problems I’ve encountered along the way are primarily related to testing.  Writing unit test using the “MVC Framework” out of the box is nearly impossible, but I did eventually put together an architecture that allows me to do that.  I may blog about that in the future.

Today, what I want to offer is how I overcame various hurdles related to testing EXTjs with Selenium.

The first problem nearly everyone encounters when they first use EXTjs is that the views that are created are created dynamically.  If you use the recommended itemId to uniquely identify the elements on the page, you will be left with IDs that are dynamically generated and inconsistent.  That is, just because the ID has one value today doesn’t mean it will have the same value tomorrow.  And since the controls are dynamically created based on what browser is running, XPATH queries aren’t a good solution either.

Custom FindsBy attribute

Now, you could solve this problem by creating a different Page Object for each browser and then you’d be able to use XPATH statements to go after the elements.  But, what if you could go after the itemIds directly?  By the Selenium APIs let you create your own selectors.  So, instead of writing your element properties like this:

[FindsBy(How = How.Id, Using="someId")]
private IWebElement SomeId{get; [UsedImplicitly]set;}


You could do something like:

[FindsBy(How = How.Custom, 
    CustomFinderType = typeof(ExtByComponentQuery), 
    Using = "#someId")]
private IWebElement SomeId{get; [UsedImplicitly]set;}

One of the side effects of using this extension is that you can now find any element using any valid EXTjs selector.

So, you are probably wondering what the code looks like to get this all working.  Well here you go:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Internal;

namespace ENT.Supporting
    public class ExtByComponentQuery : By
        private readonly string _locator;
        public ExtByComponentQuery(string locator)
            _locator = locator;

        public static ExtByComponentQuery Query(string locator)
            if (locator == null)
                throw new ArgumentNullException("locator", 
                    @"Cannot find elements when name locator is null.");
            return new ExtByComponentQuery(locator);

        public override IWebElement FindElement(ISearchContext context)
            var elements = FindElements(context);
            if (elements != null && elements.Count > 0)
                return elements.First();
            Console.WriteLine(@"returning null from FindElement");
            return null;

        public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext context)
            var script = string.Format(
@"var r= Ext.ComponentQuery.query('{0}');
var rarray = [];
for(e = 0;e < r.length;e++)
    if(r[e].getEl() !== undefined)
return rarray;",_locator.Replace("'","\\'"));
            var javaScriptExecutor = context as IJavaScriptExecutor ??
                                     ((IWrapsDriver)context).WrappedDriver as IJavaScriptExecutor;
            if (javaScriptExecutor == null) return null;
            object scriptReturn = null;
            for (var i = 0; i < 120 && scriptReturn == null; i++)
                if ( i > 0) Thread.Sleep(1000);
                scriptReturn = javaScriptExecutor.ExecuteScript(script);
            return scriptReturn == null ? null : new ReadOnlyCollection<IWebElement>((IList<IWebElement>)scriptReturn);

The constructor gets passed the selector string that is passed with the Using parameter in the attribute.  The FindElements() finds all of the elements that match the selector.  The FindElement() returns the first of all that it found.

You can either use this code as part of the FindsBy attribute or you can use it stand alone by calling the static Query method.

You’ll see that the bulk of the functionality is just the same JavaScript code you would normally write in EXTjs.

Get an element’s Value

One of the other problems you are likely to run into is retrieving the current value of an element.  This is because many of the controls that EXT provides to us do not use the INPUT item’s value to keep track of the value.  So, for this, and other similar scenarios, I wrote an extension method.

public static string GetExtValue(this IWebElement element)
    IWebElement realElement = null;
    if (element as IWrapsDriver == null)
        var e = element as IWrapsElement;
        if (e != null) realElement = e.WrappedElement;
         realElement = element;

    if (realElement == null) return null;

    var wrapsDriver = (IWrapsDriver)realElement;
    var id = realElement.GetAttribute("id");
    var driver = wrapsDriver.WrappedDriver;
    var val = ((IJavaScriptExecutor)driver).ExecuteScript(
        @"return Ext.getCmp(arguments[0]).getRawValue();",
    if (val == null)
        return string.Empty;
    return val.ToString();

What you’ll want to pay attention to here is the code at the top.

The if(element as IWrapsDriver) stuff is testing to see if we have the real element or if we have a proxy to the element that was created by the FindsBy attribute.  If the element is of type IWrapsDriver, we have the proxy and so need to go grab the real element before we continue.  Once we are sure we are working with a real element, we can use JavaScript to grab the raw value from EXTjs.

Other Modifications

I found that there were other modifications that I needed to make.  So I’ve also created extension methods that select items from dropdown list, Click radio buttons and checkboxes until they are actually selected (or actually unselected).  I found that they don’t always select or unselect on the first click for some reason.  Also, if your code is enabling and disabling stuff and you want to check for that, you’ll need to write some extensions to handle that because EXT uses a mask to disable.  The best way of determining if something is enabled or disabled is by writing JavaScript to ask EXT if it is enabled or disabled.  There are other things I’ve written that are somewhat project specific.  But if you run into a problem, your best bet is to create an extension method to go after EXT code directly.


I generally hate using the framework I’m using to test my code from EXTjs.  And whenever possible, I’ll look for a way to go after the elements using plain old selectors and the built in Selenium code.  But that isn’t always practical in this case.


Other post in Selenium

Related Post

  • Running Selenium In Parallel With Any .NET Unit Testing ToolRunning Selenium In Parallel With Any .NET Unit Testing Tool Running Selenium in parallel from .NET seems to be a problem because, as of the time of this writing, I’ve yet to find a viable way of running selenium test on multiple browsers using […]
  • What Browser Is Selenium Currently RunningWhat Browser Is Selenium Currently Running This probably doesn’t happen all that often, but this last week I came across the need to know which browser I was running my selenium test against.  I figured that buried deep in the […]
  • Selenium Performance ImprovementsSelenium Performance Improvements loading...As many of you know, I’ve been using Selenium to do my website testing.  And, if you’ve done any testing with Selenium yourself, you know that Selenium can be even slower […]
  • Using JavaScript to Drive Selenium TestsUsing JavaScript to Drive Selenium Tests I’ve written about using Selenium to test web applications before.  But all of those articles have assumed you are using C#.  I’ve realized that Selenium has multiple language bindings […]
  • Selenium Grid SetupSelenium Grid Setup My experience with setting up Selenium Grid was frustrated by the lack of information available about exactly what I needed to do to get this working.I’ve actually had this working for […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback