Google Chrome and ChromeDriver with Selenium

Google Chrome and ChromeDriver with Selenium

Before going in details about google chrome and ChromeDriver lets learn some terminology.

Wire Protocol: defined as RESTful web services using JSON over HTTP that is being used by webdriver/RemoteWebDriver, to communicate with browser.

ChromeDriver is a standalone server developed by Chromium team(Google) which implements wire protocol.
ChromeDriver consist of three separate pieces,
a. Chrome Browser.
b. Selenium Project code (driver) AND
c.  An Executable that helps code to communicate with chrome browser (lets call it server for ease)

server expects you to have chrome installed in the default location of computer.

Before start, make sure that you add the location of ChromeDriver.exe aka server into the PATH system variable so you do not need to specify during the execution.
WebDriver driver = new ChromeDriver();
WebDriver driver = new ChromeDriver(“PATH To ChromeDriver.exe”);

if you execute your test with ChromeDriver, you will notice that server will start and shut down when driver.quit is being called. server is small and light weighted but this will start and stop every time while executing multiple tests which may add noticeable delay to a larger test suite. To handle this, you can control the life and death of the server by ChromeDriverService.

1. Start ChromeDriverService in [TestFixtureSetup] before any test starts.
service = ChromeDriverService.CreateDefaultService();
service.Start();
option = new ChromeOptions();

2. Stop this service in [TestFixtureTearDown] once all test suite executed.
service.Dispose();

Full Code:

using System;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Chrome;
namespace NUnitTest1 {
    [TestFixture]
    public class TestBasicSteps {
        OpenQA.Selenium.IWebDriver driver = null;
        ChromeDriverService service = null;
        ChromeOptions option = null;
        [TestFixtureSetUp]
        public void TestFixtureSetup() {
            service = ChromeDriverService.CreateDefaultService();
            service.Start();
            option = new ChromeOptions();
			}
        [SetUp]
        public void setup() {
            driver = new ChromeDriver(service, option);
			}
        [Test]
        public void TestCase1() {
            driver.Navigate().GoToUrl("http: //www.google.com");
            System.Threading.Thread.Sleep(5000);
			}
        [Test]
        public void TestCase2() {
            driver.Navigate().GoToUrl("http: //www.learnseleniumtesting.com");
            System.Threading.Thread.Sleep(5000);
			}
        [TearDown]
        public void CleanUp() {
            driver.Quit();
            }
        [TestFixtureTearDown]
        public void dispose() {
            service.Dispose();
            }
        }
    }

Known Issue:

Typing does not work with rich-text enabled page.
Cannot specify a custom profile.
HTML 5 API not implemented yet.

One Comments

  • Mohideen

    June 17, 2016

    Thansk a lot i have tried codes from many post but most of them failed. But your code really helped me a lot.

    Thanks for all your efforts.

    Reply

Leave a Reply