Samstag, 15. Juli 2017

Tutorial - ASP.NET Core and Selenium Webdriver

Since I'm going to do lots of web development stuff in the near future, I thought that I should take a look at the Selenium testing framework. Selenium comes either as a Firefox plugin (Selenium IDE) or as a framework (Selenium Webdriver) that supports multiple programming languages.

Preparations:

  • Get CoreCompat.Selenium.Webdriver
  • Get a plugin for the browser you want to use for testing, e. g. Selenium.Firefox.WebDriver
  • (optional) If you have issues with running the project you can place the executable for the plugin on your PATH environment variable
The code sample is for a simple console project, that will open the google website and search for racoons. Usually you would combine Selenium with the testing framework of your choice .

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace SeleniumTutorial
{
    class Program
    {
        static void Main(string[] args)
        {   
            //Create a driver for a browser
            IWebDriver driver = new FirefoxDriver();
            //navigate to the page
            driver.Navigate().GoToUrl("https://www.google.com/");
        
            //find the search bar
            IWebElement searchInput = driver.FindElement(By.Id("lst-ib"));

            //send date to the seach bar
            searchInput.SendKeys("trash panda" +Keys.Enter);            
        }
    }
}

Keine Kommentare:

Kommentar veröffentlichen