Wednesday, 9 March 2016

Architecture,Functionality flow of WebDriver and its interaction with WebElements



Agenda for this blog: To discuss about the architecture of WebDriver and its functionality and approach that it follows to interact with WebElements.

WebDriver Architecture:

WebDriver  itself is an interface and the Selenium WebDriver API communicates between the written script/commands using the respective language bindings and the Browser engine to automate the Application,It uses a protocol called JSON wire Protocol and referred as WebDriver Protocol,Please find the image for the basic architecture of the WebDriver: 






Internal Flows of WebDriver  functionality and how it interacts with WebElements:


Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation and its JS-Engine. How these direct calls are made, and the features they support depends on the browser you are using.
It does not inject Javascript like Selenium-RC at the time of loading the browser.
The following incidents are occurred while execution of the Webdriver script:
         
         1.   Basically we write the script using any language binding (Java/C# etc) and it hits the WebDriver API.
         2.   The script/commands (ex : driver.findElement(By.Id(“xyz”))) gets  converted into Javascript.
         3.   By the help of Browser Engine it again gets converted into the JSon, which is kind of key-value pair .
         4.   After getting the respective value for the respective key it identifies the respective WebElements and                     performs the respective user actions(like click,sendkeys etc etc..).
         5.   All the communications are happened by using a protocol call JSon-Wire Protocol, which is also called                 WebDriver protocol.

         Find the below image for the communication that occurs between Script and WebElements:


         
    






Visit for More Automation Related Discussion:






Sunday, 2 August 2015

Interaction with Windows Dialog box using Selenium Web-Driver C#

Agenda for this blog:To discuss about  handling Window based Dialogue/popup box in Selenium C# .

What is Window Dialogue/Pop-Up:Its a dialogue box which is not a part of  webpage but are the domain of the Operating System.

As it does not belong the web property so Selenium web-driver is unable to interact directly with this type of popup/dialogue.However its possible to perform actions on dialog windows using 
"SendKeys" class's method "SendWait()" of name space System.Windows.Forms.

Detailed reference of SendKeys Class can be found in https://msdn.microsoft.com/en-au/library/system.windows.forms.sendkeys.aspx


Sample Scenario

1.Uploading File:


In this scenario we will handle window dialogue for uploading File in Selenium.


 static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://www.newocr.com/");
            driver.FindElement(By.XPath("//input[@id='userfile']")).Click();
            Thread.Sleep(3000);
            String pathToFile = "C:\\Users\\Admin\\Desktop\\434765";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();
         
        }


2. Saving/Downloading Files:



static void Main(string[] args)

        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^s");
            Thread.Sleep(2000);
            String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();


        }


3.Printing Page:


class Program

    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://www.newocr.com/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^p");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();
         
        }


By the above approach interaction with windows dialogue box /pop-up is handled properly with Selenium WebDriver.





Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos




Thursday, 16 July 2015

Parallel Testing with Nunit for SpecFlow(Selenium C#)

Agenda for this blog: To achieve Parallel Testing using NUnit for SpecFlow- Cucumber for .NET.

Why we need parallel testing :  There are several use of Parallel testing,In case of Selenium, parallel testing gives us many benefits like:



    • Cross -browser testing.
    • Less execution time (as the script gets segregated )
    • Testing with Different platforms using GRID many more.


Does NUnit supports Parallel testing : The answer to this question in a single word is NO.
Till date it does not support parallel testing in true way ,but there are way we can achieve parallel execution with NUnit by creating category and with the help of NUnit console

How to Do:



  • Step1: Create category in Feature file:  In Spec-flow which is a BDD framework from Selenium C# we need to create feature file and write the test case in ENGLISH-LIKE language called Gherkin,there in every feature file we need to mention "@CategoryName"


NOTE: Here @CategoryName can be anything relevant like @SmokeTest or @RegressionTest or  @SuiteName.

                      Now when you build your project you can see category wise segregation in the  Test Explorer in Visual Studio.


  • Step2:  Create a Batch file and call project .dll(compiled code) file for test execution mentioning categories by using nunit-console like:



nunit-console /result:TestResults1.xml pathToDllFile /include:CategoryName1
nunit-console /result:TestResults2.xml pathToDllFile /include:CategoryName2
nunit-console /result:TestResults3.xml pathToDllFile /include:CategoryName3


You will able to see parallel execution by category wise with multiple browser instances.


Visit for More Automation Related Discussion:

https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






Monday, 13 April 2015

Selenium with OCR Technology for Image Verification With Text

Agenda for this blog:To discuss the verification of IMAGE in Selenium c# with the OCR technology.

What is OCR:Optical character recognition (OCR) is the mechanical or electronic conversion of images of typewritten or printed text into machine-encoded text.It is a common method of digitizing printed texts so that it can be electronically edited, searched, stored more compactly, displayed on-line, and used in machine processes.

Where and When OCR technology is required for Selenium:

When we are automating our application with Selenium and C# as a language.There  might be some scenario where we need to verify images,In this type of situation tampering images won't give good result,and also Selenium c# is not that much compatible with SIKULI which does a decent job for Selenium JAVA in this kind of image processing/verification etc etc. There OCR technology comes in to the picture which serves the purpose well.

What we can Do with OCR:
We can extract text from the images and can verify the same from our Selenium C# application Under test.

HOW:
           We need to download the required image locally and perform the action in following two ways:

A. Using "https://www.newocr.com/" we can upload the image and can extract the text from the image.

B. Using "tesseract-ocr"  we can achieve the purpose very well.


What is Tesseract-Ocr:

Tesseract is probably the most accurate open source OCR engine available. Combined with the Leptonica Image Processing Library it can read a wide variety of image formats and convert them to text in over 60 languages.

How: We need to install tesseract-ocr in our system and through cmd prompt we can extract text from the image.



Visit for More Automation Related Discussion:

https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






Saturday, 11 April 2015

SpecFlow-Cucumber for .NET & Selenium with C#

Agenda for this Blog: To discuss about SpecFlow which is Cucumber for .Net.

SpecFlow supports the concepts of Acceptance Test Driven Development (ATDD) and Behaviour Driven Development (BDD), which are often used synonymously filling the gap between domain experts and Developers by binding business readable behaviour specifications,it uses  Gherkin.

What is Gherkin:
Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour without detailing how that behaviour is implemented.

Gherkin Syntax example:

Feature: To Cover regression of some application
                and its business values
 
        Scenario:   To test the Login Scenario
          Given           User needs to be on the Login Page of the Application
          And               some other precondition
          When           enter text as Username and Password
          And               click on the sign in button
          Then             verification of the login (Such as Home page should be displayed)


Which Unit Test Provider is acceptable :

We can use Unit Test Provider  as per the  need and choice such as we can use NUNit,MsTest or we can use SpecRun along with SpecFlow to run our Test Scripts.


Unit Test Provider can be configured in app.config file in the Unit Test Project like:

<specFlow>
  <unitTestProvider name="SpecRun" />
</specFlow>


We need to create separate feature file and we need to use gherkin syntax there,we can generate the the step definition for the same and we need to incorporate the Selenium C# code for writing Test Script.

Sample for SpecRun Report:







Visit for More Automation Related Discussion:

https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos

Friday, 20 March 2015

C#-Selenium Code Snippets

Agenda:
The agenda for this blog is to Discuss various scenario based concept and code used in C# Selenium.

Highlighting Web Element in Script Run-Time:
There are some Scenarios where we need to verify/Identify WebElements in the Page ,We can do them in multiple approaches,We can High Light the Element which we are looking for and set the color with different colors for that element , in the Test Run time so we can Identify/verify that the same:

public void HighlightToVerify(By locator)

        {
        try{
               string AttributeValue = "border:3px solid green;";
               string Attribute = Driver.FindElement(locator).GetAttribute("style");
IJavaScriptExecutor JsDriver = ((IJavaScriptExecutor)Driver);
JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), AttributeValue);
               Thread.Sleep(100);
               JsDriver.ExecuteScript("arguments[0].setAttribute('style', arguments[1]);", Driver.FindElement(locator), Attribute);
           }
        catch(Exception){

             }
        }


SendKeys using JavaScript:

There are scenarios where we need to use JavaScript for Entering Text in the Input field,the same can be achieved in the following way:

public void SendData(By Locator, String Text, int TimePeriod = 30)
        {
            try
            {
                IJavaScriptExecutor js = ((IJavaScriptExecutor)Driver);
                js.ExecuteScript("arguments[0].innerHTML='" + Text + "';", Driver.FindElement(Locator, TimePeriod));
            }
            catch (Exception ex)
            {
                Log.Error("Unable to Enter Text using IJavaScriptExecutor for Element with Locator: "+Locator);
                
            }
        }



Presence Of Certain Text in The Page Source:

In some cases we need to check the presence of some text in the Page Source,Here we can achieve the same:

public bool IsTextPresentOnPage(String Text)

        {
            try
            {
                return Driver.PageSource.Contains(Text);
            }
            catch (Exception)
            {
                Log.Info("The Text "+ Text +"is not Present in the Page Source ");
                return false;
            }

        }



Zoom -IN and Zoom Out :


In Selenium you might face some issues like interacting with elements that goes under task bar due to improper layout. 



In this scenario WebElement will not be visible ,for this scenario you need to zoom out the page again zoom in once action is performed successfully :



class Program

    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=fxQ6VeiZNJTDuASzrYHQAg");
            driver.Manage().Window.Maximize();
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^{ADD}");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^{SUBTRACT}");
            Thread.Sleep(3000);
            driver.Quit();

            

        }
    }

Download or Save Files :



In selenium sometimes we need to download or save files,we can achieve it by the below approach:


static void Main(string[] args)

        {

            IWebDriver driver = new FirefoxDriver();

            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^s");
            Thread.Sleep(2000);
            String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();

            


        }


Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






Gallio-Automation Platform with MbUnit- C#-Selenium

Agenda Of the Blog: Introduction of  Gallio -an open source platform for .NET,  which provides run-time services and tools.

What is Gallio?

The Gallio Automation Platform is an open, extensible, and neutral system for .NET that provides a common object model, run-time services and tools (such as test runners) that may be leveraged by any number of test frameworks.

Features and use of Gallio


·         Tools Support

·         Frameworks supported by Gallio: MbUnit, NUnit, MSTest, NBehave, XUnit
·         Different Gallio Runners: GUI (Icarus), Command-line (Echo) etc.

·         Gallio can be used for the multiple purposes:




Why to use Gallio for Automation Testing with Selenium C#:


To control and manipulate the Test execution approach:    To run the Selenium C# code we need to compile the full project and get the .dll file and we need to use that generated .dll file as a input to any Test Runner (framework) to execute the automation script, and Gallio serves the purpose in robust manner with multiple approaches, like:


(a)    Running all suites as a whole binding:

(i)      Whole automation Project can be run as a whole by using “Gallio Echo” Runner tool with .dll file as input to it. Detailed Reports are generated after the execution of whole Automation project with XML and HTML format.

(b)   Execution of All or Few selected Test Suites and Test cases:

(i)      We can perform selection of Test Suites and Test cases from whole Test Automation suites and execute them. This can be achieved by using “Gallio Icarus” Runner tool which is basically an UI, the generated .dll file has to be added through the UI “Add Files” option to use the Compiled project’s Test Case and, we can select all or few test suites and test cases from the UI tree format to execute. Please refer the below screenshot for the Gallio Icarus UI with Test Report.





(i)       Detailed Reports are generated and visible from the UI (in Execution Log tab) itself and we can see the report after the execution of test case, we don’t need to wait until the Whole suite execution.


(b)     Gallio can be integrated with Build Tool like NAnt to build the full automation process.

(c)     Gallio can be integrated with CI tool like Jenkins. 







What is MbUnit?


The MbUnit is an extensible unit testing framework for the .Net Framework. MbUnit is a part of the Gallio bundle. It provides below rich features apart from the other generic features which is available for any other unit test framework for .Net Framework:

1.       We can perform Parallel Testing with MbUnit for C# Selenium with multiple instances of the browser.

2.       MbUnit Supports Retry option which is basically automatic triggering of failed test cases for the next time.


Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos