Microsoft launch driver for Edge

ms_edgeMicrosoft have announced a new WebDriver server for the upcoming new Windows 10 browser, Microsoft Edge. They announced support for automated testing of Microsoft Edge through the W3C WebDriver standard. To use WebDriver with Microsoft Edge, you need the MicrosoftWebDriver server on a Windows Insiders build of 10240 or newer. You can read their blogpost here at Bringing automated testing to Microsoft Edge through WebDriver.

Microsoft also provided a sample test using C# and the the as yet unreleased Edge bindings to open a webpage in MS Edge, navigate to bing.com and search for “webdriver”:

using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace BingSearchTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            RemoteWebDriver driver = null;
            string serverPath = "Microsoft Web Driver";
            try
            {
                if (System.Environment.Is64BitOperatingSystem)
                {
                    serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
                }
                else
                {
                    serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
                }

                // location for MicrosoftWebDriver.exe
                EdgeOptions options = new EdgeOptions();
                options.PageLoadStrategy = EdgePageLoadStrategy.Eager;
                driver = new EdgeDriver(serverPath, options);

                //Set page load timeout to 5 seconds
                driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));

                // Navigate to https://www.bing.com/
                driver.Url = "https://www.bing.com/";

                // Find the search box and query for webdriver
                RemoteWebElement element = (RemoteWebElement)driver.FindElementById("sbox");
                element.SendKeys("webdriver");
                element.SendKeys(Keys.Enter);

                // Wait for search result
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
                wait.Until(x => x.Title.Contains("webdriver"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (driver != null)
                {
                    driver.Close();
                }
            }
        }
    }
}

Finding an Element by an Attribute

Sometimes it is not always possible to uniquely identify an element by using the standard ways such as By.ID or By.Name etc. And there can be any number of reasons why this would be the case. However there is an alternative way to find an element by using one of the element’s attributes:

driver.FindElement(By.CssSelector("[title='Unique Identifier']"));

This example will find the element that has “Unique Identifier” as the value for its title attribute. Using the CSS Selector is an elegant way to find this element, but there are other ways to achieve the same outcome. Using XPath you can achieve the same outcome as per this example:

driver.FindElement(By.XPath("//*[@title='Unique Identifier']"));

However I find that this XPath solution is not as readable as the previous CSS Selector version. There is a plus however to using XPath as we can fine tune the search. The //* part of the XPath indicates that we are to search ALL HTML tags for the specified title. But if you change the //* and replace the asterix with the name of an HTML tag such as img then we can search for only <img> elements that have the specified title.

driver.FindElement(By.XPath("//img[@title='Unique Identifier']"));

If you come across any similar solutions, add them to the comments below.

Ignoring IE Driver Zoom Requirement

Many times I have kicked off an automated test using InternetExplorerDriver() only for it to crash straight away as the zoom was not set to 100%. (Why is this required, it makes no sense to me). Internet ExplorerPreviously the only way I could get around this was to open a new window of Internet Explorer, reset the zoom to 100%, close the browser, reopen the browser again to verify the correct zoom. While this worked it wasn’t a very elegant solution.

Finally I found how to ignore the zoom level requirement and it is as simple as this when starting up your browser:

var IE_Options = new InternetExplorerOptions
{
    IgnoreZoomLevel = true,
    EnableNativeEvents = false
};
driver = new InternetExplorerDriver(IE_Options);

This allows you to run your test without having to worry about what the zoom is currently set to.