Click here to Skip to main content
13,901,147 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

19.1K views
15 bookmarked
Posted 4 Aug 2015
Licenced Ms-PL

Microsoft Edge WebDriver- What Everybody Ought to Know About

, 4 Aug 2015
Rate this:
Please Sign up or sign in to vote.
Examples how to use the Microsoft Edge WebDriver to create UI automated tests executed against the new Microsoft browser- Edge.

Introduction

As you might know, one of the best available frameworks for creating web automation tests is Selenium WebDriver. Microsoft publicly announced that their new Windows 10 web browser- Edge is going to support WebDriver automation (Microsoft Edge WebDriver). Of course I wanted to try it as fast as I can, so I prepared everything needed and created several tests. I’m going to present to you their source code and results.

Create Your First WebDriver Test Project

1. Create New Test Project in Visual Studio.

2. Install NuGet package manager and navigate to it.

3. Search for Selenium and install the first item in the result list

Microsoft Edge WebDriver C# Code

Test’s Test Case

The primary goal of the below tests is going to be to create a “healthy” diet menu from specially designed by me- diet generator page.

Code Examples in Firefox WebDriver

The automation of the above form using Firefox WebDriver is a trivial task.

[TestClass]
public class HealthyDietMenuGeneratorTestsFirefox
{
    private IWebDriver driver { get; set; }

    [TestInitialize]
    public void SetupTest()
    {
        this.driver = new FirefoxDriver();
    }

    [TestCleanup]
    public void TeardownTest()
    {
        this.driver.Quit();
    }

    [TestMethod]
    public void FillAwsomeDietTest()
    {
        this.driver.Navigate().GoToUrl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
        var addAdditionalSugarCheckbox = this.driver.FindElement(By.Id("ninja_forms_field_18"));
        addAdditionalSugarCheckbox.Click();
        var ventiCoffeeRadioButton = this.driver.FindElement(By.Id("ninja_forms_field_19_1"));
        ventiCoffeeRadioButton.Click();
        SelectElement selectElement = new SelectElement(this.driver.FindElement(By.XPath("//*[@id='ninja_forms_field_21']")));
        selectElement.SelectByText("7 x BBQ Ranch Burgers");
        var smotheredChocolateCakeCheckbox = this.driver.FindElement(By.Id("ninja_forms_field_27_2"));
        smotheredChocolateCakeCheckbox.Click();
        var addSomethingToDietTextArea = this.driver.FindElement(By.Id("ninja_forms_field_22"));
        addSomethingToDietTextArea.SendKeys(@"Goi cuon- This snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. It’s "meat light," with the flavors of refreshing herbs erupting in your mouth.");
        var rockStarRating = this.driver.FindElement(By.XPath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
        rockStarRating.Click();
        var firstNameTextBox = this.driver.FindElement(By.Id("ninja_forms_field_23"));
        firstNameTextBox.SendKeys("Anton");
        var lastNameTextBox = this.driver.FindElement(By.Id("ninja_forms_field_24"));
        lastNameTextBox.SendKeys("Angelov");
        var emailTextBox = this.driver.FindElement(By.Id("ninja_forms_field_25"));
        emailTextBox.SendKeys("aangelov@yahoo.com");
        var awsomeDietSubmitButton = this.driver.FindElement(By.Id("ninja_forms_field_28"));
        awsomeDietSubmitButton.Click();
    }
}

The driver instance is created in the TestSetup method and disposed in the TestCleanup. After that the test navigates to the desired page, finds the specified elements and performs the required action. If you don’t know how to use the different WebDriver methods, you might find interesting my fast-read tutorial on the matter- Getting Started with WebDriver C# in 10 Minutes.

The test was executed for ~16 seconds.

Code Examples in Firefox WebDriver + Page Objects

I also rewrote the test to use WebDriver Page Objects. I created a HealthyDietGeneratorPage class where all elements are present.

public class HealthyDietGeneratorPage
{
    public readonly string Url = @"http://automatetheplanet.com/healthy-diet-menu-generator/";

    public HealthyDietGeneratorPage(IWebDriver browser)
    {
        PageFactory.InitElements(browser, this);
    }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_18")]
    public IWebElement AddAdditionalSugarCheckbox { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_19_1")]
    public IWebElement VentiCoffeeRadioButton { get; set; }

    [FindsBy(How = How.XPath, Using = "//*[@id='ninja_forms_field_21']")]
    public IWebElement BurgersDropDown { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_27_2")]
    public IWebElement SmotheredChocolateCakeCheckbox { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_22")]
    public IWebElement AddSomethingToDietTextArea { get; set; }

    [FindsBy(How = How.XPath, Using = "//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a")]
    public IWebElement RockStarRating { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_23")]
    public IWebElement FirstNameTextBox { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_24")]
    public IWebElement LastNameTextBox { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_25")]
    public IWebElement EmailTextBox { get; set; }

    [FindsBy(How = How.Id, Using = "ninja_forms_field_28")]
    public IWebElement AwsomeDietSubmitButton { get; set; }
}

When the object is created for the first time, all elements are initialized through the WebDriver’s PageFactory.

The code of the test is almost identical with the only difference that the elements initializations are now a responsibility of the page object.

[TestClass]
public class HealthyDietMenuGeneratorTestsFirefox
{
    private IWebDriver driver { get; set; }

    [TestInitialize]
    public void SetupTest()
    {
        this.driver = new FirefoxDriver();
    }

    [TestCleanup]
    public void TeardownTest()
    {
        this.driver.Quit();
    }

    [TestMethod]
    public void FillAwsomeDietTest_ThroughPageObjects()
    {
        HealthyDietGeneratorPage healthyDietGeneratorPage = new HealthyDietGeneratorPage(this.driver);
        this.driver.Navigate().GoToUrl(healthyDietGeneratorPage.Url);
        healthyDietGeneratorPage.AddAdditionalSugarCheckbox.Click();
        healthyDietGeneratorPage.VentiCoffeeRadioButton.Click();
        SelectElement selectElement = new SelectElement(healthyDietGeneratorPage.BurgersDropDown);
        selectElement.SelectByText("7 x BBQ Ranch Burgers");
        healthyDietGeneratorPage.SmotheredChocolateCakeCheckbox.Click();
        healthyDietGeneratorPage.AddSomethingToDietTextArea.SendKeys(@"Goi cuon- This snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. It’s "meat light," with the flavors of refreshing herbs erupting in your mouth.");
        healthyDietGeneratorPage.RockStarRating.Click();
        healthyDietGeneratorPage.FirstNameTextBox.SendKeys("Anton");
        healthyDietGeneratorPage.LastNameTextBox.SendKeys("Angelov");
        healthyDietGeneratorPage.EmailTextBox.SendKeys("aangelov@yahoo.com");
        healthyDietGeneratorPage.AwsomeDietSubmitButton.Click();
    }
}

The execution time almost didn’t change- ~15 seconds. Anyway, it was a little bit faster.

Microsoft Edge WebDriver Prerequisites

1. Download Microsoft Edge WebDriver executable from the official Microsoft website.

2. Install Microsoft Edge WebDriver from the previously downloaded setup.

3. Create a virtual machine or upgrade your OS to Windows 10, the Microsoft Edge WebDriver is compatible only with it.

Code Examples in Microsoft Edge WebDriver

On theory, the same tests should be able to be executed through the new WebDriver, only with the exchange of the driver type. The setup for the Microsoft Edge WebDriver is a little bit more complicated. Also, there are some not supported yet methods like GoToUrl and FindElement by XPath. The navigation to a new URL is achieved through this.driver.Url assignment.

[TestClass]
public class HealthyDietMenuGeneratorTestsEdge
{
    public IWebDriver driver;
    private string serverPath = "Microsoft Web Driver";

    [TestInitialize]
    public void SetupTest()
    {
        if (System.Environment.Is64BitOperatingSystem)
        {
            serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
        }
        else
        {
            serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
        }
        EdgeOptions options = new EdgeOptions();
        options.PageLoadStrategy = EdgePageLoadStrategy.Eager;
        this.driver = new EdgeDriver(serverPath, options);

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

    [TestCleanup]
    public void TeardownTest()
    {
        this.driver.Quit();
    }

    [TestMethod]
    public void FillAwsomeDietTest()
    {
        //this.Driver.Navigate().GoToUrl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
        this.driver.Url = @"http://automatetheplanet.com/healthy-diet-menu-generator/";
        var addAdditionalSugarCheckbox = this.driver.FindElement(By.Id("ninja_forms_field_18"));
        addAdditionalSugarCheckbox.Click();
        var ventiCoffeeRadioButton = this.driver.FindElement(By.Id("ninja_forms_field_19_1"));
        ventiCoffeeRadioButton.Click();
        //SelectElement selectElement = new SelectElement(this.Driver.FindElement(By.Id("ninja_forms_field_21")));
        //selectElement.SelectByText("7 x BBQ Ranch Burgers");
        var smotheredChocolateCakeCheckbox = this.driver.FindElement(By.Id("ninja_forms_field_27_2"));
        smotheredChocolateCakeCheckbox.Click();
        var addSomethingToDietTextArea = this.driver.FindElement(By.Id("ninja_forms_field_22"));
        addSomethingToDietTextArea.SendKeys(@"Goi cuon- This snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. It’s "meat light," with the flavors of refreshing herbs erupting in your mouth.");
        //var rockStarRating = this.Driver.FindElement(By.XPath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
        //rockStarRating.Click();
        var firstNameTextBox = this.driver.FindElement(By.Id("ninja_forms_field_23"));
        firstNameTextBox.SendKeys("Anton");
        var lastNameTextBox = this.driver.FindElement(By.Id("ninja_forms_field_24"));
        lastNameTextBox.SendKeys("Angelov");
        var emailTextBox = this.driver.FindElement(By.Id("ninja_forms_field_25"));
        emailTextBox.SendKeys("aangelov@yahoo.com");
        var awsomeDietSubmitButton = this.driver.FindElement(By.Id("ninja_forms_field_28"));
        awsomeDietSubmitButton.Click();
    }
}

The same test executed with Firefox Driver finished for 16 seconds; now it was run for 4 seconds using the new Microsoft Edge WebDriver.

The page object implementation was even faster. The test execution took only 3 seconds.

[TestClass]
public class HealthyDietMenuGeneratorTestsEdge
{
    private IWebDriver driver;
    private string serverPath = "Microsoft Web Driver";

    [TestInitialize]
    public void SetupTest()
    {
        if (System.Environment.Is64BitOperatingSystem)
        {
            serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
        }
        else
        {
            serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
        }
        EdgeOptions options = new EdgeOptions();
        options.PageLoadStrategy = EdgePageLoadStrategy.Eager;
        this.driver = new EdgeDriver(serverPath, options);

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

    [TestCleanup]
    public void TeardownTest()
    {
        this.driver.Quit();
    }

    [TestMethod]
    public void FillAwsomeDietTest_ThroughPageObjects()
    {
        HealthyDietGeneratorPage healthyDietGeneratorPage = new HealthyDietGeneratorPage(this.driver);
        //this.Driver.Navigate().GoToUrl(healthyDietGeneratorPage.Url);
        this.driver.Url = healthyDietGeneratorPage.Url;
        healthyDietGeneratorPage.AddAdditionalSugarCheckbox.Click();
        healthyDietGeneratorPage.VentiCoffeeRadioButton.Click();
        //SelectElement selectElement = new SelectElement(healthyDietGeneratorPage.BurgersDropDown);
        //selectElement.SelectByText("7 x BBQ Ranch Burgers");
        healthyDietGeneratorPage.SmotheredChocolateCakeCheckbox.Click();
        healthyDietGeneratorPage.AddSomethingToDietTextArea.SendKeys(@"Goi cuon- This snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. It’s "meat light," with the flavors of refreshing herbs erupting in your mouth.");
        //healthyDietGeneratorPage.RockStarRating.Click();
        healthyDietGeneratorPage.FirstNameTextBox.SendKeys("Anton");
        healthyDietGeneratorPage.LastNameTextBox.SendKeys("Angelov");
        healthyDietGeneratorPage.EmailTextBox.SendKeys("aangelov@yahoo.com");
        healthyDietGeneratorPage.AwsomeDietSubmitButton.Click();
    }
}

So Far in the 'Pragmatic Automation with WebDriver' Series

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
Anton Angelov is an IT Consultant and Quality Assurance Architect at Innovative Lab. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.

You may also be interested in...

Comments and Discussions

 
QuestionDearly Beloved we are gathered here to celebrate the life of CodedUI. Gone in just 5 years... Pin
Mr. Javaman5-Aug-15 17:07
memberMr. Javaman5-Aug-15 17:07 
GeneralCool info, my vote up Pin
Gaurav Aroraa5-Aug-15 3:04
professionalGaurav Aroraa5-Aug-15 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 4 Aug 2015
Article Copyright 2015 by Anton Angelov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid