Sunday, January 4, 2015

Hand Coded in Coded UI test using C# With Gmail Login Demo





Hi guys this time we are going see a demo on Coded UI test using C#(Visual Studio) With Example

This tutorial aims to help us become comfortable with CodedUI and will demonstrate how to go about creating a basic CodedUI test based on a simple application 

For the purpose of this exercise we will create the test using the CodedUI Test Builder.

During this tutorial we will cover the following areas:

1 . Creating test projects

2 . Adding CodedUI tests to that project

3 . Executing CodedUI test with Gmail login test.

Step 1 - Prepaing the test environment

We need to ensure that we have the following  pre-requisites deployed onto our test machine.

* Visual studio 2013 ultimate.

Step 2 - Creating a new Test Project

2.1. To create a test project do the following:

* Launch Visual Studio 2013 ultimate

* Click File -> New, and select Project from the menu.



*  Click on Installed Templates, choose the language of your   choice (C#.net for this exercise), and click on Test.



*  Select CodedUI Test Project from the results menu.



Thereafter you need to complete Name of the project which will automatically populate the solution name.

Click the OK button and you will have a brand spanking new Test Project.

After that your project look like below


All we have to do is .We need to write test script on  [TestMethod]



In this demo am going to write test on Gmail Login page.

My test is :

  • I Need to open Browser and Navigate to www.gmail.com
        For this am using the following code :
        BrowserWindow browser = BrowserWindow.Launch("www.gmail.com");
    
    This will open the browser and navigate to gmail
  • After that i need to fill detail the Username text box
        For this am using UITestControl
     //UITestControl is the class which has the ablity to locate controls on UI

     //Creating object for Usernametextbox
            //sending username from keyboard
            UITestControl username = new UITestControl(browser);
         username.TechnologyName = "web";
         username.SearchProperties.Add("Controltype", "Edit");
         username.SearchProperties.Add("Id", "Email");
         Keyboard.SendKeys(username, "kk.prashanth65@gmail.com");
     
      I need to locate elements in the webpage to do this 

         Rightclick - > Click on Generate code for codedUI test -> select Use Coded UI Test Builder

      See the below images in Step by Step :

    Right Click on VS and Select Generate code for codedUI test




     select Use Coded UI Test Builder


You will get UI Map- Code UI Test Builder on the bottom


Click on the Circle and drag and drop to Webelement


  You will get all the Property which we will use i our script like

TechnologyName = "web";
SearchProperties.Add("Controltype", "Edit");
SearchProperties.Add("Id", "Email");




  • After that i need to Fill details in Password textbox
       //Creating object for Passwordtextbox

            //sending password from keyboard
            UITestControl password = new UITestControl(browser);
          password.TechnologyName = "web";
          password.SearchProperties.Add("Controltype", "Edit");
          password.SearchProperties.Add("Id", "Passwd");
          Keyboard.SendKeys(password, "test123");

  • Click on Sign in button
           UITestControl click_signin = new UITestControl(browser);

          click_signin.TechnologyName = "web";
          click_signin.SearchProperties.Add("Controltype", "Button");
          click_signin.SearchProperties.Add("Id", "signIn");

            //Click button
           Mouse.Click(click_signin);
      



Source Code :

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


namespace CodedUIDemo
{
    /// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class CodedUITest1
    {
        public CodedUITest1()
        {
        }

        [TestMethod]
        public void GmailLoginTest()
        {
            //open browser
            BrowserWindow browser = BrowserWindow.Launch("www.gmail.com");

            //UITestControl is the class which has the ablity to locate controls on UI

            //Creating object for Usernametextbox
            //sending username from keyboard
            UITestControl username = new UITestControl(browser);
            username.TechnologyName = "web";
            username.SearchProperties.Add("Controltype", "Edit");
            username.SearchProperties.Add("Id", "Email");
            Keyboard.SendKeys(username, "kk.prashanth65@gmail.com");

            //Creating object for Passwordtextbox
            //sending password from keyboard
            UITestControl password = new UITestControl(browser);
            password.TechnologyName = "web";
            password.SearchProperties.Add("Controltype", "Edit");
            password.SearchProperties.Add("Id", "Passwd");
            Keyboard.SendKeys(password, "test123");

            //Clicking Sign-in button on gmail

            UITestControl click_signin = new UITestControl(browser);
            click_signin.TechnologyName = "web";
            click_signin.SearchProperties.Add("Controltype", "Button");
            click_signin.SearchProperties.Add("Id", "signIn");

            //Click button
            Mouse.Click(click_signin);

            //Make test to wait 10 sec

            Playback.Wait(10000);


        }

     

    }
}

Run the above code your test will pass.



You also check in TestExplorer for Passed Test and time taken to complete test.




Hope you Like this post.



16 comments:

  1. Hi Prashant! Hope you are doing well.
    Great article! Very simple to understand and very informative.
    I only got one basic question. Are those testing classes/namespaces included in Visual Studio since which version?
    I mean, I worked with visual studio several years ago and I'm sure it has changed a lot since then. SO, I'm asking you if this is a new feature in VS included recently by MS? Or is it part of a separated bundle we have to buy separately?

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TestTools.UITest.Extension;
    using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;

    Thanks a lot!

    ReplyDelete
  2. Hi Marcus,

    Am using VS 2013 ultimate, Coded UI also available in VS 2010 ultimate and no need to install any seperate bundle for CodedUI :)

    ReplyDelete
  3. Hi Prashanth,
    This is great, I'm looking for some tutorial for hand-coding CodedUI for so long :)
    I have some questions as following:
    1/ Can we separate the interfaces (capture or define controls, windows,...) with functions and test step in test method? Like keyword driven framework.

    2/ How can you search controls which have some parts varied and need to wildcard the stable part?

    Thanks

    ReplyDelete
  4. Hi,
    For the Great information you write it very clean. I am very lucky to get this tips from you.very well information you write it very clean. I’m very lucky to get this information from you .

    www.gmail.com login page

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I am using coded UI for automation. In my application there is a browse button for attaching file .how to attach file for any application ? i wanted to do hand coding for it . Please give suggestions. Thanks

    ReplyDelete
  7. Simply dial any of these sans toll Gmail Help Numbers for Gmail related Technical Assistance and get live help immediately from a Qualified Support Technician with parcel of Technical Support Experience. https://robetbuckner.quora.com/How-We-Can-Change-Our-Gmail-Password-Easily

    ReplyDelete
  8. Thank you for your sharing
    Can you tell me where the wrong user or password is using assert gives me a message telling me to pass or fail?

    ReplyDelete
  9. I have problems with the test in multibrowser, can you help me with examples

    ReplyDelete
  10. I am getting the error " A project with the output type of class library cannot be started". I have also setup as the default start up project and it does not work

    ReplyDelete
  11. can you tell me how to write the validation logic?if the user name or password entered is wrong,test case should fail..plz help me on this.

    ReplyDelete
  12. I recently bought a book on Amazon (Hand Coding Coded UI) which clearly defines how to setup the framework and create an easily maintainable code. Not sure if it is available in any book store but here is the link on Amazon if you want to have a look. cheap mobile phones for sale

    ReplyDelete
  13. Very nice article, I enjoyed reading your post, very share, I want to twit this to my followers. Thanks!.
    https://www-expresshr.us/

    ReplyDelete
  14. I really loved reading your blog. It was well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!
    https://wvw-greatpeople.me/

    ReplyDelete
  15. Great survey, I'm sure you' re getting a great response.
    seo tools

    ReplyDelete
  16. Thanks for providing recent updates regarding the concern, I look to read more.
    survey monkey usa

    ReplyDelete