Validating Tab Order with WebDriver
I had a spec that defined the tab order on a form. Starting with the default form field the user will be able to press the tab key to move the cursor to the next form field. Tabbing through the fields will follow a specific order. I couldn’t find much on Google or Bing to help automate this with WebDriver, maybe I’m loosing my search skills.
Below is code to implement this with WebDriver. In production I use a SpecFlow Table instead of an array to hold the expected tab order and I have a custom wrapper around WebDriver so much of this code is hidden from test code. Below is the untested gist of my production implementation. Since all of my elements have IDs, and your’s should too, we are simply validating that the active element has the ID of the current element in the array iteration.
- If the element doesn’t have an ID, fail the test.
- If the element ID doesn’t match the expected ID, fail the test.
- If the ID matches, tab to the next element and loop.
public void TestTabOrder()
{
//Code to open the page elided.
....
//This is the expected tab order. The strings are element IDs so the test assumes all of your elements have IDs.
string[] orderedElementIds = new string[] { "FirstControl", "SecondControl", "NextControl" };
foreach (var item in orderedElementIds)
{
string elementId = item;
//Get the current active element, element with focus.
IWebElement activeElement = webDriver.SwitchTo().ActiveElement();
//Get the id of the active element
string id = activeElement.GetAttribute("id");
//If the active element doesn't have an id, fail the test because all of our elements have IDs.
if (string.IsNullOrWhiteSpace(id))
{
throw new AssertionException("Element does not have expected ID: " + elementId);
}
//If the active element doesn't match the current ID in our orderedElementIds array, fail the test.
if (elementId != id)
{
throw new AssertionException("Element: " + elementId + " does not have focus.");
}
//Tab to the next element.
activeElement.SendKeys(Keys.Tab);
}
}
You don’t have to assert anything as the exceptions will fail the test (using MSTest AssertionException), hence no exception equals passing test. You get a bonus assert with this test in that it also verifies that you have a certain element with default focus (the first element in the array).
I am sure there is a better way to do this, but it works. Hope this helps someone as it wasn’t something well publicized.