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();
option = new ChromeOptions();

2. Stop this service in [TestFixtureTearDown] once all test suite executed.

Full Code:

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

Known Issue:

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

One Reply to “Google Chrome and ChromeDriver with Selenium”

  1. 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.

Leave a Reply