Browse Category: Webdriver

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.

Slide-bar and slide action using Webdriver

Some websites do have slider elements like you can see in some travel websites like given below. this moment is possible using mouse event and concept would be very much similar to the old article on mouse event. I will take a sample example of working code with dummy data inside so you can pass your input and see this working. Here is detailed explanation: You need to keep few things in mind:

  1. Slider bar size may or may not vary based on browser size.
  2. You may need to slide either by number of pixels or percentage of slider bar length.

I will take very generic example of http://jqueryui.com/slider/ to explain all steps. First reason to select this example is that the size of slider bar is adjusting based on browser size and secondly its available for everyone J. In the code below, I will first try to get the size of bar at the current browser size where I want to perform slider action and then based on req I will move the slider. Make sure you don’t change the browser size in between reading the slider bar size and doing sliding action.

  1. Open url via selenium code.

  2. Switch to the container frame to reach the slider and slider bar web element.

  3. Select the bar and get the width.

  4. Now we need to move slide to 50% (231px) or 100px. We can write small java/.net program to calculate desired percentage in pix using the width of the bar as 463 in my case. But I will move only 40pixels.
  5. Get web element for the slider on bar.

  6. Now we want move the slider in right side horizontally means towards Y axis. To perform movement action we will use action class and use mouse event. To move horizontally we need to move in Y direction of screen so we can use MoveByOffset

    i.e. X axis: 0 & Y axis: 40px. So first create action, Click and hold the slider, move to some pix as follows.

    Now script has completed and you can perform the whole action in one go!!!

Happy testing!!

Jenkins and Continuous Test Execution

Continuous test execution is an essential part of automation testing specially in regression run. If you need to start execution manually means your return of investment is not as good as needed. There are multiple ways to do this. Developers in many companies use Continuous Integration (CI) tool like Hudson, Rational Team Concert for building application multiple times in a day where codes are checked in from multiple location. You can use same feature of building application to perform testing as well. You can build your own in-house application to perform the same task. I will give idea for the same at the end of the blog.


I use WebDriver for automation testing in .NET framework.  I heard that many companies use Jenkins for CI and even for testing so thought to implement my tests written in C#. After some research on internet I was able to implement a basic skeleton for continuous testing. In order to understand and to customize Jenkins we will go Step by Step.

  1. Download Jenkins.
  2. Run and verify Jenkins.
  3. Install as a Window Service.
  4. Configure Jenkins as a slave. (Optional. Read only if you want to learn.)
  5. Create Job to execute test.

1.       Download Jenkins:

In order to download Jenkins, Go to http://jenkins-ci.org/ and download ‘Latest and greatest’ version of Java web archive (.war) file and save it to some location.

2.       Run And Verify Jenkins:

Open Command window and navigate to the location where you have downloaded *.war file. Execute following command (I saved Jenkins.war file in D drive)

  • Java –jar jenkins.war

You will see following output

image001

If you read all lines you will see that HTTP Listeners started at port 8080.

image002

3.       Install Jenkins as Window Service:

Node: If you install as window service, your tests will run in background.

Perform Step 2 if not yet done. Click ‘Manage Jenkins’ you will see following scree with various option.

image003

Click on ‘Install as Windows Service link. You will see following screen.

image004

Click on Install button. Once installation complete you will see following:

image005
Go to Window Services and verify if you can see there or not. image006 From now, you will not require to start Jenkins site on command line. You can directly start http://localhost:8080/ if your Jenkins service is running. NOTE: if you are using only window OS then you can directly install native package from http://jenkins-ci.org/ image007 You need to unzip the folder and install msi file. That will install and get ready with step 3.

4.       Configure Jenkins as a Slave:

This is most interesting part of Jenkins. Here you can configure your slave attached to Master which is ready in step 3. You can assign tasks to various slave to perform like test execution etc. Start Jenkins Website and click on link ‘Manage Jenkins’. You will see following option among various other options. image008 Click on the above option which will take you to the following screen. image009 Now you need to create Node that connected to master (Just like GRID (HUB-NODE)). To do that you need to click on ‘New Node’ link that should you to following screen. image010 Give some Name of Node and Click on Dumb Slave radio button that will active the OK button. Click on OK button. This will take you to the following screen. image011 You can click on Help icon image012 to understand more about the fields. I am giving following options # of executor : 1 ,  Remote FS root:  D:\Shared. Launch method: Save this. It will take you the following screen. image013 As you can see above, you can start that node by given three options. It is up to you, to choose but I prefer first option. Click on the Orange button with Launch. This will ask you to download slave-agent.jnlp. Please save this to the location where you have created your slave i.e. D:\Slave. Open command prompt and change directory to the location where you have saved this file and execute  following command Javaws slave-agent.jnlp aafter some processing you may see following windows: image014 This will disappear in few second and you will see Jenkins slave Agent screen with ‘Connected’ text on the screen. image015 If you go back to jenkin site and refresh page in Manage Node screen, you will see following screen. image016 You can install this slave into machine as window services using File menu given in ‘Jenkin salve agent’ screen so in future you will not require doing the whole process of starting slave via jlnp image017 image018 Click ‘OK’. This may ask your permission to run the program. image019 Will see that Jenkins slave agent window disappear and if you check in ‘services’ you will be able to see slave as services. image020 Go back to Jenkins site and Click on slave link the page and make sure that you can see slave running. image021 Now your slave is connected to your master and ready to server your for execution by added execution job assigned by you as given  in next section.

5.       Create JOB to Execute Test:

Once your slave node is running, go to home page of Jenkins and click on ‘New Job’. image022 This will take you to the following window. Provide Job name as per your choice and select radio button. ‘Build a free-style software project’. And hit OK. image023

This will take you the detailed screen and you can fill information as you needed. Few points are very important which you should consider.

  1. Execute test at specific Node.

image024

2. Regression Run repletion :

How frequent you want to execute test.

image025

There are various command you can give to give frequency of test execution ie. Every hour or at ever day at some specific time frame. You can Google the commands or I can write separate article about this if needed.

3. Execute Tests from various options (batch command preferred at least in my case)

image026

4. Post Execution:

Configure to send email or prepare report as NUnit/Junit repot etc.

image027

Once you save this job this will take you to the following screen.

image028

Click on  [Back to Dashboard] link that will take you the following screen.

image029

To execute test you can click on image030  . You will see following screen.

image031

I think much detail covers the basic understanding of Jenkins configuration for test execution. I hope this will give you are direction to configure Jenkins in your project. I use Webdriver in .NET environment and configure Jenkins for continuous execution and  it was successful implementation but later we decided to build our own internal application to specific for our own application which can do continuous execution and other tasks as well which is not possible via Jenkins like customized internal website with customized graph and tables but that’s another story.

I am going to write another article specifically how to execute tests using msbuild, msTest and Nunit and another article for JUnit.