Saturday, August 29, 2015

Introduction to Selenium Web Driver

            Using Selenium Web Driver (Firefox)

Today I would be talking about test automation using Selenium Web Driver. In this article I would be using Firefox
Steps involved
  1. Create a new unit test project in visual studio


 
2. Install Selenium drivers using nugget package manager. We need both package as shown in the snapshot 




3. Write test case as shown below.In this example I am automating a google search for my name.
  
    Steps involved:
  • Load the browser driver : FirefoxDriver
  • Specify the timeout to find the web site :5 sec
  • Specify the website name : http://google.com
  • Find the Id of the search element using Inspect element. In our example we need the Id of the text box in google search ( As shown in the snapshot after the code ) 
  • Enter the text field for search "Prathap Kudupu"
  • Hit Enter button
  • Run the test case

using System;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTestSamples
{
    [TestClass]
    public class GoogleSearchTest
    {
        private IWebDriver _driver;
        [TestMethod]
        public void GoogleNameSearchChrome()
        {
            try
            {
                _driver = new ChromeDriver();

                _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(5));
                _driver.Manage().Window.Position = new Point(0, 0);
                _driver.Manage().Window.Size = new Size(1024, 768);
                _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
                //Navigate to Google.com
                _driver.Navigate().GoToUrl("http://google.com");
                //Find the Id for entering value
                IWebElement searchElement =
                _driver.FindElement(By.Id("lst-ib"));
                //Pass values 
                searchElement.SendKeys("Prathap Kudupu");
                //Click enter key
                searchElement.SendKeys(Keys.Enter);
            }
            catch
            {
                if (_driver != null)
                {
                    _driver.Quit();
                }
                throw;
            }
        }

        [TestMethod]
        public void GoogleNameSearchFireFox()
        {
            IWebDriver driver = new FirefoxDriver();
            //Keep finding untill time out has been reached
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
            //Navigate to Google.com
            driver.Navigate().GoToUrl("http://google.com");
            //Find the Id for entering value
            IWebElement searchElement=
            driver.FindElement(By.Id("lst-ib"));
            //Pass values 
            searchElement.SendKeys("Prathap Kudupu");
            //Click enter key
            searchElement.SendKeys(Keys.Enter);


        }
    }
}





Find the Id form the web page 






















Run test case from usual studio



Test case results 




  Using Selenium Web Driver (Chrome)

You need to install nuget package for chrome as shown below. This is the only difference.




No comments:

Post a Comment