Showing posts with label CodedUI blog. Show all posts
Showing posts with label CodedUI blog. Show all posts

Tuesday, 13 December 2016

Data Source Types and Attributes


  • CSV
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
  • Excel
    DataSource("System.Data.Odbc", "Dsn=ExcelFiles;Driver={Microsoft Excel Driver (*.xls)};dbq=|DataDirectory|\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet1$", DataAccessMethod.Sequential), DeploymentItem("Sheet1.xls"), TestMethod]
  • Test case in Team Foundation Server
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://vlm13261329:8080/tfs/DefaultCollection;Agile", "30", DataAccessMethod.Sequential), TestMethod]
  • XML
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\data.xml", "Iterations", DataAccessMethod.Sequential), DeploymentItem("data.xml"), TestMethod]
  • SQL Express
    [DataSource("System.Data.SqlClient", "Data Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated Security=True", "Data", DataAccessMethod.Sequential), TestMethod]

Thursday, 1 December 2016

How to Click or find control by scrolling page in CodedUI


How to click on control if it is visible on page and we need to scroll the page to click on it.

In CodedUI we have UITestingUtilities dll that helps us a lot in finding control or verifying control existance by providing many extended methods.

In below code sample, we have one button control on page. To click on this control manually, we need to scroll the page and then we can click on control. This can be handled through code by using EnsureClickable() method. 
Here we are first making sure that the control exists by using TryFind() method and then trying to click control.

While loop added to check if more page scroll action required. Once the control is clicked the "isClickable" variable set to true, so that to break the While() loop. The exception  is thrown if the control is still hidden on blocked by some other control , To handle this condition we have to catch the exception and instead of throwing it we are scrolling the page down.


Code below

            BrowserWindow bw = new BrowserWindow();
            HtmlButton but = new HtmlButton(bw);
            //define properities for the button here

            bool isClickable = false;
            if(but.TryFind())
            {
                while (!isClickable)
                {
                    try
                    {
                        // It ensures control is visible and clickable
                        but.EnsureClickable();
                        Mouse.Click(but);
                        isClickable = true;
                    }
                    catch(FailedToPerformActionOnHiddenControlException)
                    {
                        //We dont through an exception instead we scroll the page down and try again
                        Mouse.MoveScrollWheel(-1);
                    }
                    catch (FailedToPerformActionOnBlockedControlException)
                    {
                        {
                            //We dont through an exception instead we scroll the page down and try again
                            Mouse.MoveScrollWheel(-1);
                        }
                    }
                }
            }

***Here we can add while loop break logic to avoid the situation like - the method will be continuously searching for control and scrolling page down. To do this we can count the Page scroll action and if it is more than certain count then we can break while loop and throw exception (like Control not found)

Thursday, 4 August 2016

Finding elements by JavaScript


While trying to use JavaScript to speed up the test execution, many tests would start first operation (such as selecting one option from a combo box, clicking on a link or button and etc.) several minutes after the pages loaded. This is hard to tolerate when running tests from my PC and anyone can only wait several minutes to run a single line of code. 

In these cases, though CodedUI cannot step forward, instead a JavaScript can be run from command line of IE browser directly. BrowserWindow would execute JavaScript i.e. ExecuteScript() only after WaitForControlReady() returning true. Fortunately, developing JavaScript to replace native operations like Mouse.Click() could still be very helpful.


From time to time, CodedUI tests would fail to perform some very basic operations like clicking on a button or entering text to a text control. Sometimes click on a line/button of a web page, “Click” sound will be heard but the browser would simply do nothing. JavaScript would then be more effective when it operates directly on the element instead of via UI.

The key scripts  are listed below:
        #region JavaScript function names
        public const string FindByCssFunction = "querySelector";
        public const string FindByIdFunction = "getElementById";
        public const string FindFirstByCssFunction = "querySelectorAll";
        public const string FindFirstByClassFunction = "getElementsByClassName";
        public const string FindFirstByNameFunction = "getElementsByName";
        public const string FindFirstByTagFunction = "getElementsByTagName";
        #endregion

        public const string GetElementByIdScript = @"
var result = document.getElementById(arguments[0]);
if (result)
    return result;

var frames = document.getElementsByTagName('frame');
if (arguments[1]) {
    var frame = frames[arguments[1]];
    if (frame.document)
        return frame.document.getElementById(arguments[0]);
    else
        return frame.contentWindow.document.getElementById(arguments[0]);
}

for(var i = 0; i < frames.length; i ++) {
    if (frames[i].document)
        result = frames[i].document.getElementById(arguments[0]);
    else
        result = frames[i].contentWindow.document.getElementById(arguments[0]);

    if (result) break;
}
return result;";

        public const string FrameGetElementByIdScript = @"
var result = arguments[0].contentDocument.getElementById(arguments[1]);
return result;";

        public const string GetFirstByCssScript = @"
var elements = document.querySelectorAll(arguments[0]);
if (elements.length)
    return elements[0];

var frames = document.getElementsByTagName('frame');
if (arguments[1]) {
    var frame = frames[arguments[1]];
    if (frame.document)
        return frame.document.querySelectorAll(arguments[0]);
    else
        return frame.contentWindow.document.querySelectorAll(arguments[0]);
}

for(var i = 0; i < frames.length; i ++) {
    if (frames[i].document)
        elements = frames[i].document.querySelectorAll(arguments[0]);
    else
        elements = frames[i].contentWindow.document.querySelectorAll(arguments[0]);
if (elements.length)
    return elements[0];
}
return null;";

        public const string GetClickablePoint = @"
var element = arguments[0];
var absoluteLeft = element.width/2;
var absoluteTop = element.height/2;

do {
absoluteLeft += element.offsetLeft;
absoluteTop += element.offsetTop;
element = element.parentElement;
}while(element)

var result = new Array();
result[0] = Math.round(absoluteLeft).toString();
result[1] = Math.round(absoluteTop).toString();
return result;";

        public const string GetAttributeScript = @"try{{return arguments[0].getAttribute('{0}');}}catch(err){{return null;}}";

        public enum FindFirstMethod
        {
            ById,
            ByCSS,
            FirstByCSS,
            FirstByClass,
            FirstByName,
            FirstByTag,
        }

The web app under test uses multiple frames as container to show different panels. To search one element with its ID, The JavaScript “GetElementByIdScript” would first search the root document, then after getting all frames, would try to iterate all frames to see if the element with specified ID could be found within one of them. The “FrameGetElementByIdScript” is used to search a single frame only give element ID. The “GetFirstByCssScript” provides a more generic means to search element with CSS selectors which however would normally return an array and actually only the first one is expected. The “GetAttributeScript” is used to retrieve attribute from a given element, notice that when there is an error catched, it shall return “null”.

To reuse the above scripts with different methods (by ID, by Class, by Name and etc.), the FindFirstMethod enum is defined and the default value ById would be used as below:
public static HtmlControl FindControl(this BrowserWindow window, string locatorKey, FindFirstMethod method = FindFirstMethod.ById, string frameName = "body")
{
    if (window == null || !window.WaitForControlReady(DefaultWaitReadyTimeMillis))
        throw new Exception("Browser is not specified or is not ready after " + DefaultWaitReadyTimeMillis / 1000 + "s.");

    string script = null;
    switch (method)
    {
        case FindFirstMethod.ById:
            script = GetElementByIdScript;
            break;
        case FindFirstMethod.ByCSS:
            script = GetElementByIdScript.Replace(FindByIdFunction, FindByCssFunction);
            break;
        case FindFirstMethod.FirstByCSS:
            script = GetFirstByCssScript;
            break;
        case FindFirstMethod.FirstByClass:
            script = GetFirstByCssScript.Replace(FindFirstByCssFunction, FindFirstByClassFunction);
            break;
        case FindFirstMethod.FirstByName:
            script = GetFirstByCssScript.Replace(FindFirstByCssFunction, FindFirstByNameFunction);
            break;
        case FindFirstMethod.FirstByTag:
            script = GetFirstByCssScript.Replace(FindFirstByCssFunction, FindFirstByTagFunction);
            break;
        default:
            throw new NotSupportedException();
    }

    object result = null;
    Stopwatch watch = new Stopwatch();
    watch.Start();
    while (result == null && watch.ElapsedMilliseconds < 20 * 1000)
    {
        result = window.ExecuteScript(script, locatorKey, frameName);

        //To cope with the bug of BrowserWindow..ExecuteScript()
        var optionList = result as IList<object>;
        if (optionList != null)
        {
            var child = optionList.FirstOrDefault(o => o != null) as HtmlControl;
            result = (child == null || !child.Exists ) ? null : child.GetParent();
        }
    }
    return result as HtmlControl;
}
The script would then be finalized by replacing some keywords and run by a BrowserWindow instance. Noticeably, when trying to get a HtmlComboBox, CodedUI would wrongly return an array of its option children and that is why there is some special treatment to get a single HtmlControl.
CodedUI only defines BrowserWindow with virtual object ExecuteScript(string script, params object[] args), to make it easier to use, there are two helper methods introduced to target one specific control with or without extra parameters:

public static object RunScript(this HtmlControl control, string script)
{
    if (control == null)
        throw new Exception("Failed to locating the control?!");

    BrowserWindow window = control.TopParent as BrowserWindow;
    if (window == null || !window.WaitForControlReady(DefaultWaitReadyTimeMillis))
        throw new Exception("Browser is not specified or is not ready after " + DefaultWaitReadyTimeMillis / 1000 + "s.");

    return window.ExecuteScript(script, control);
}

public static object RunScript(this HtmlControl control, string script, params object[] extraArguments)
{
    if (control == null)
        throw new Exception("Failed to locating the control?!");

    BrowserWindow window = control.TopParent as BrowserWindow;
    if (window == null || !window.WaitForControlReady(DefaultWaitReadyTimeMillis))
        throw new Exception("Browser is not specified or is not ready after " + DefaultWaitReadyTimeMillis / 1000 + "s.");

    var len = extraArguments.Length;
    object[] arguments = new object[len + 1];
    arguments[0] = control;
    for (int i = 0; i < len; i++)
    {
        arguments[i + 1] = extraArguments[i];
    }

    return window.ExecuteScript(script, arguments);
}

Then some helper functions are quite straightforward:
public static string AttributeByScript(this HtmlControl control, string attributename)
{
    return control.RunScript(string.Format(GetAttributeScript, attributename)) as string;
}

public static string InnerText(this HtmlControl control)
{
    return control.RunScript("return arguments[0].innerText;") as string;
}

public static string InnerHTML(this HtmlControl control)
{
    return control.RunScript("return arguments[0].innerHtml;") as string;
}

public static string OuterHTML(this HtmlControl control)
{
    return control.RunScript("return arguments[0].outerHtml;") as string;
}

public static Point ClickablePointByScript(this HtmlControl control)
{
    object result = control.RunScript(GetClickablePoint);
    List<object> position = (List<object>)result;
    return new Point(int.Parse(position[0].ToString()), int.Parse(position[1].ToString()));
}
Their meanings are explained below:
  • SomeControl.AttributeByScript(attributename): to get Attribute of a control by attribute name;
  • SomeControl.InnerText(): would retrieve everything within the opening and closing tag of the control as innerText as used in section “Evaluating rows of a table”. It could be tuned to get displayed text.
  • SomeControl.InnerHtml()/OuterHtml(): returns InnerHtml and OuterHtml respectively.
  • SomeControl.ClickablePointByScript(): I have tried this to get the clickable point to avoid waiting CodedUI to be ready to click some link/button, but it doesn’t return before the target control is really ready.
The more useful methods are listed here:
public const bool HighLightControlBeforeOperation = true;
public static void ClickByScript(this HtmlControl control)
{
    control.ShowByScript();

    if (HighLightControlBeforeOperation)
        control.HighlightByScript();

    control.RunScript("arguments[0].click();");
}

public static void SetValue(this HtmlControl control, string valueString)
{
    control.RunScript("arguments[0].value = arguments[1];", valueString);
}

public static void ShowByScript(this HtmlControl control)
{
    control.RunScript("arguments[0].scrollIntoView(true);");
}

public const string DefaultHighlightStyle = "color: green; border: solid red; background-color: yellow;";
public static void HighlightByScript(this HtmlControl control)
{
    //*/ Highlight by script: changing the style of concerned element
    var oldStyle = control.AttributeByScript("style");

    control.RunScript("arguments[0].setAttribute('style', arguments[1]);", DefaultHighlightStyle);
    System.Threading.Thread.Sleep(DefaultHighlightTimeMillis);
    if (oldStyle != null)
    {
        control.RunScript("arguments[0].setAttribute('style', arguments[1]);", oldStyle);
    }
    else
        control.RunScript(string.Format("arguments[0].removeAttribute('style');"));
}
Their meanings are explained below:
  • SomeEdit.SetValue(valueString): is used to input text to Edit control even when it is not displayed yet.
  • SomeControl.ShowByScript(): would make the control visible for further operation/observation.
  • SomeControl.HighlightByScript(): to modify the style of the target control to make it highlighted for several seconds.
  • SomeControl.ClickByScript(): might be the most useful method to fix tests. The ShowByScript() would be called to make the control visible, then HighlightByScript() would be called to mark it outstanding before perfroming “someControl.click()”. This design is out of intention: by performing operations 3 times(scrolling once, changing styles twice), it is very unlikely to miss clicking on the target control.
To use these scripts are also quite simple, taken Click() for example: for some problematic Mouse.Click(someControl), just replacing them with “someControl.ClickByScript()” would make many failed tests passed.

Source: http://www.codeproject.com/Articles/876476/Tips-to-fix-CodedUI-tests

TryFind vs WaitForControl(Ready/Exist)


TryFind() changed the test execution. The reason is that TryFind() would return true/false immediately when pages are still loading and its returned value would heavily depend on the responsiveness of the web server, so some tests would be fixed by simply replacing it with WaitForControlExist() which would block the current thread until timeout specified by default PlaybackSettings.WaitForReadyTimeout.

However prefer specifying timeout setting explicitly by calling WaitForControlReady(int millisecondsTimeout) or WaitForControlExist(int millisecondsTimeout). Somebody might accustomed with Playback.Wait(int thinkTimeMilliseconds) or Thread. Sleep(int millisecondsTimeout), but they might wait unnecessarily long. 

In addition, if combined with Assertion as be;pw:
Assert.IsTrue(someElement.WaitForControlReady(60*1000)); //Wait for 1 min
Unexpected delay could be highlighted immediately.
There are multiple WaitForControl() functions defined with UITestControl, some of them could be very helpful but are usually neglected, for example:
  • WaitForControlCondition(Predicate<UITestControl> conditionEvaluator, int millisecondsTimeout), combined with LINQ, provides a very powerful means to evaluate any status of the target control.
  • WaitForControlPropertyEqual(string propertyName, object propertyValue, int millisecondsTimeout) enables tester to monitor changes of any attribute of the control effectively.
  • bool WaitForControlNotExist(int millisecondsTimeout), combined with Assert.IsTrue(), could be used to evaluate operations have successfully caused the page changing to another state.
  • bool WaitForControlExist(int millisecondsTimeout) and bool WaitForControlReady(int millisecondsTimeout) should be used from time to time before performing solid operations like clicking, selecting or typing. Usually, the former is good enough especially when such operations would be carried out only when the browser/control is ready. But they might return different values for a specific control: usually WaitForControlExist() would return true when the target control is displayed, but in some of my projects, WaitForControlReady() would return true several minutes after WaitForControlExist() returning true.

Sunday, 10 April 2016

Database Testing with CodedUI


Lets discuss about the Database Testing with CodedUI (C#) with some sample code
Connection String plays a key role in connecting to the database
Which has a different format for different Databases like Oracle,SQL etc
//Below code will login and open the DB connection.
string connetionString = “Data Source=DBSeverName;Initial Catalog=DBName;User ID=UserName;Password=DBPassword”;
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();


Once the DB connection is setup, we need to define a sql Command for executing and Sql Datareader for reading the results of the command
Once the results are read by the data reader, it is loaded into a DataTable

//Below code will help to execute the Select query.
String Selectquery = “Select FirstName from Emp where EmpID in (01)”;
SqlCommand command = new SqlCommand(Selectquery, connection);
SqlDataReader reader = command.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(reader);
Once the data is loaded into the datatable, all we need to do is verify with the expected output

//Below code will help to verify the data which above code fetch from the particular table.
String rowText;
rowText = DB.dataTable.Rows[0][DB.dataTable.Columns[0]].ToString();
if (rowText.Contains(“Mayank”))
{
      Console.WriteLine(“FirstName is macthing with expected result”);
}
else
{
      Console.WriteLine(“Verification is not passed”);
}

After the verification, Close the connection
//It will close the connection.
connection.Close();

Thursday, 7 April 2016

Using a App.config file to have different Sections in CodedUI project

To make coded UI tests to work in different environments, or to have different configuration values for different test methods to override the source of data file, a config file can be setup rather than a data source. 

Things to be done to achieve this
  1. You’ll need to add a reference to the System.Configuration assembly for the solution
  2. Then add the custom elements to app.config file (which is added to test project) TestSiteConfigurationData section name in the app.config file
  3. Create configuration data class for a custom section in App.Config TestSiteConfigurationData.cs
  4. Create the Helper class file for reading the data from configuration data class TestSiteConfigurationHelper.cs
  5. Call the helper class in the Codedui Project and read the values from config file under your custom section

You might face an issue with some wrong value entered for the type of section in the app.config file

 <configSections>
    <section name="TestSiteConfigurationData"
             type="Configuring_Sections.TestSiteConfigurationData,Configuring Sections"/>
  </configSections>
   
Configuring_Sections.TestSiteConfigurationData - <ProjectNamespace>.<ConfigurationDataClassName>

Configuring Sections - the solution dll name i.e your CodedUI project name

Find the sample project implementing the above attached


Happy coding :)


Monday, 10 August 2015

Limit TestMethod Timeout in CodedUI


Sometimes when we have Test Methods which run for long time, we tend to see the below kind of exceptions and TestMethod execution stops.
"Test 'TestMethod_Name' exceeded execution timeout period." 
By default Execution time for a test method is 60mins.
This test execution timeout can be changed as per our requirements.
It can be either changed in the Settings file or else

First way is to Set a limit time for execution which is as shown below
[TestMethod]
[Timeout(6000)]
public void TestMethodName()
{
}

Second is to set infinite time limit when we dont know how much time the execution is going to happen or if we are expecting the execution to run completely irrespective of the time

[TestMethod]
[Timeout(TestTimeout.Infinite)]
public void TestMethodName()
{
}

Wednesday, 29 July 2015

Execute CodedUI TestMethod() through command prompt



Input the path of the dll in the Developer Command Prompt for VS2012/13..
cd C:\Users\...\Documents\Visual Studio 2012\Projects\CodedUITestProject\CodedUITestProject\bin\Debug
Using VSTest.Console.exe like this:
VSTest.Console.exe CodedUITestProject.dll /Tests:TestMethod1,testMethod2 /UseVsixExtensions:true  /Logger:trx
Note There is no /resultsfile option in VSTest.Console.exe command line. You need to use /Logger.trx if you want to generate a trx file.

Note The /Tests command line option cannot be used with the /TestCaseFilter command line option.

Usage  of /TestCaseFilter
Examples of /TestCaseFilter usage with filter as Priority
/TestCaseFilter:"Priority=1"

Examples of /TestCaseFilter usage with filter as Test Category
/TestCaseFilter:"TestCategory=Nightly

Examples of /TestCaseFilter usage with multiple filters using "|"
/TestCaseFilter:"TestCategory=Nightly|FullyQualifiedName=Namespace.ClassName.MethodName"

Monday, 20 July 2015

Schedule Coded UI Test to Execute At a Specific Time

The best way I have found to accomplish this is to create a .bat file which executes the test using the mstest command line tool, and setting this .bat to execute at a specific time using task scheduler. I currently do this daily on a project I am working on.
Here are the steps oulined to execute a .bat driven Coded UI test at a specified time.

Step1:
Create a .bat similar to the following:
set mstestPath="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"
 %mstestpath%\mstest /testcontainer:AutomatedUITest.dll  /resultsfile:TestOutput.trx
Test your script by double-clicking the .bat and assure that the test is properly executing.

Step2:
Assuming you name this .bat “TestRunner.bat”, you would then open Task Scheduler (pre-installed on Windows, type “Task” into the search bar to locate). You will see Task Scheduler (local) in the upper left of the window. Right-click, then select create Task. Give the task a name and choose appropriate security settings. Click the Triggers tab, and assign when you wish the test to auto-execute. In this screen you can set the appropriate time to run. Now click the Actions tab, click new and assign your script “TestRunner.bat” to execute and any parameters you may wish to feed to your script. Now choose settings and allow the test to run on demand. Ensure your test is executing properly by clicking the Task Scheduler Library underneath Task Scheduler (local), locate your task, right-click and choose run. If everything executes as expected, you can rest assured that your scheduled task will properly execute at the time(s) you have designated.

Monday, 15 June 2015

Invoking a TestMethod from TestCleanUp in CodedUI


While running a Test method, suppose the application went down suddenly or a time out issue came up because of which ContolNotFound exception occurred and got existed from the test method.
In this scenario in order to invoke the test method from the TestCleanup again this is how we do

[TestCleanup]
 public override void MyTestCleanup()
 {
      // Here i am writing for an generic flow checking my test failed.
      // you can check if any time out window availability or application down messages in browser
      if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
      {      
            var type = Type.GetType(TestContext.FullyQualifiedTestClassName);      
            if (type != null)
           {
                var instance = Activator.CreateInstance(type);
                var method = type.GetMethod(TestContext.TestName);
                try
               {
                    method.Invoke(instance, null);
               }
               catch
              {
            
              }
         }
     }
 }

Tuesday, 2 June 2015

selenium Webdriver script with SpecFlow and Visual Studio



SpecFlow is the .Net version of Cucumber. This is a Behavior development framework for .Net. Here we will show how we can implement our Selenium Webdriver script using SpecFlow framework.

Test Case : Verify the search Functionality of Google Search page
  • Open the browser and navigate to “www.google.com”.
  • Enter the word “SpecFlow” as Search Text.
  • Click on Search button.
  • Verify the search result.

Step One : Installing IDE integration package for SpecFlow 
  • Open Visual Studio and go to Tools>>Extension and Updates…
 
  • Extensions and Update window is displayed. Enter the word “SpecFlow” at search text box and press enter from keyboard. “SpecFlow for Visual Studio 2013” package is displayed as Search Result.
  • If you see Disable and Uninstall button then it means it is already installed with your IDE like the following.

  • If you see Download button then Click on this button.


  • It starts downloading.


  • After finishing download,click on Install button.


  • After finishing installation, click on Restart Now button.

  • Visual Studio will be restarted and if you go to Tools>>Extension and Updates… and search with “SpecFlow” then you see “SpecFlow for Visual Studio 2013” is installed.
  • In the same way search the extensions “NUnit Test Adapter” and Download it. It is used for NUnit Framework.

  • Click on Install button.

  • Click on Restart Now button to restart Visual Studio.


Step Two : Crating a new project with Unit Test Framework
  • Click on File menu and go File>>New>>Project

  • New project window is displayed.
  • Select Visual C#
  • Select Test
  • Select Unit Test Project


  • Enter project name as “GoogleSearchTest”
  • Select Location. In my case “D:\Development\”.
  • Select Solution name. In my case Solution name is same as Project name.
  • Check Create Directory for solution
  • Click on OK button.

  • “GoogleSearchTest” project is created.

Step Three : Add SpecFlow  reference from nuget.org
  • Go to Solution Explorer and Right click on Reference and click on Manage NuGet Packages…

  • Manage NuGet Packages window is displayed.
  • Enter the text SpecFlow at search text box and press enter from Keyboard.
  • SpecFlow package is displayed at Search list.
  • Click on Install button.
  • Installation Started.
  • After finishing installation, a green color icon is displayed and shown that SpecFlow is installed.
  • In the same way, we need to add SepcFlow.NUnit package.
  • Clicking on Install button will install this package.
Step Four : Add Selenium Webdriver reference from nuget.org
  • Enter the text Selenium WebDriver at search text box and press enter from Keyboard




  • Selenium WebDriver package is displayed at Search list.




  • Click on Install button.




  • Latest WebDriver will be installed and added as Reference.



  • Select Selenium WebDriver Support Classes package and click on Install button.
    • After finishing installation and click on Close button to close the Package Manager window.
    • Now verify that all the References are showing properly at the solution explorer.


    Step Five : Create a Feature file and write test case steps using gherkin syntax
    • Right click on the Project and go to Project>>Add>>New Item.
    • Add New Item window displayed.
    • Select Visual C# Items.
    • Select SpecFlow Feature File.
    • Enter file name as “GoogleSearchFeature.feature”. Note that Feature file name must have the extension “.feature”.
    • Click on Add button.

    • New Feature file is added with the project.
    • Double click on the File name “GoogleSearchFeature.feature” and File is opened at the Editor with default text.
    • Write the Feature name as “GoogleSearch”. Remove all the default text from Feature section.
    Feature: GoogleSearch
    • Write the case title as the scenario.
    Scenario: Verify the search Functionality of Google Search page
    • Write the test case steps using Gherkin syntax (Given…GoogleSearchWhen…Then).
    Given : It describes the precondition to execute test steps.
    When : It describes the steps to perform the test.
    Then : It describes the expected result. 
    In our case, Given clause will be,
    Given I navigate to the page "www.google.com"
    And I see the page is loaded
    Note that we an “And” operator to write multiple clauses. 
    When clause will be,
    When I enter Search Keyword in the Search Text box
    | Keyword  |
    | SpecFlow |
    And I click on Search Button
    Note that here we write data table for search keyword where we use “Keyword” as the table column name and “SpecFlow” is the data. 
    Then clause will be,
    Then Search items shows the items related to SpecFlow
    Now the Feature file looks like, 
    Step Six : Write code for every step
    • Here we write code for every Feature file steps.
    • In Order to write code we need Add a step definition class.
    • Right click on the Project and go to Project>>Add>>New Item. 
    • Add New Item window is displayed.
    • Select Visual C# Items.
    • Select SpecFlow Step Definition
    • Enter file name as “GoogleSearchFeatureSteps.cs”.
    • Click on Add button.
    • New class file is added with the project named “GoogleSearchFeatureSteps.cs”.
    • Go to GoogleSearchFeature.feature file.
    • Right click on Feature name and click on Generate Step Definitions
    • Generate Step Definition Skeleton window is displayed.
    • Click on Copy Methods to clipboard button and window is closed.
    • Go to GoogleSearchFeatureSteps.cs class. Remove all the sample codes from the class and it looks like the following.

    • Right click on the editor and paste the code on GoogleSearchFeatureSteps class.GoogleSearchFeatureSteps.cs class looks like the following. 
    • Now we need to write automated scripts for every steps
    • We need to add the following name space at GoogleSearchFeatureSteps.cs class.
    • Go to GoogleSearchFeature.feature file.
    • Right click on the First line of Given clause and click Go to Step Definition.

    • Note that  GoogleSearchFeatureSteps.cs file is opened and mouse cursor is blinked on the Given method. Also note that method name is the combination of all word of Given clause.

    • Write the code for GivenINavigateToThePage() method.
    Note that we do not need to write any comment here as Methods name describes the steps. This is the contribution BDD.
    • Write code for GivenISeeThePageIsLoaded() method.

    Here we just verify that appropriate page is loaded.
    • Write code for WhenIEnterSearchKeywordInTheSearchTextBox() method.


    Note that we use list of data in our feature file that SpecFlow converts to table. We can use it as normal Row/Column concept.
    • Write code for WhenIClickOnSearchButton() method.
    • Write code for ThenSearchItemsShowsTheItemsRelatedToSpecFlow() method.

    • GoogleSearchFeatureSteps.cs class looks like the following,

    Step Seven : Run the test
    • Delete default class UnitTest1.cs as it is not needed.

    • Build the solution by going to Build>>Build Solution.
    • Go to Test>>Windows>>Test Explorer
    • Note that the scenario name is the test name VerifyTheSearchFunctionalityOfGoogleSearchPage
    • Right click on the test name and click on Run Selected test.

    • Test will run using NUnit and Passed.

    • Click on Output link.
    • We see the output step by step as it is written in our Feature file.

    This is the power of SpecFlow using BDD.
    Lot more new things are coming…