Tuesday, July 1, 2014

Coded UI JQuery Selectors


Automate Your CodedUI Scripts with J Query Selectors


Make your scripts work with JQuery selectors, for web automation testing. As we know that codedui does not support either css selectors/xpath to identify elements, it would be little hard for the people who are habituated with Automation Tools which support Css selectors and Xpath mechanism. Here is a solution that we can use with CodedUI to work with JQuery selectors inplace of Css and XPath mechanism.
                                       All you need to do is to invoke the javascript on your webBrowser that uses jquery selector mechanism which is faster than regular xpath. Ofcourse, it take time in identifying the control than Regular CodedUI Identification Mechanism, but is just an alternative that you can use for Other Browsers where the execution time takes a lot.

         
          Follow the Steps: 
         This statements ensures that your webpage has jQuery capability for run your jquery selectors 
          on it.        
     bool createJSTag = (bool)Application.browser.ExecuteScript("return (typeof $ === 'undefined')");
     
     If the above statement returns false, then we should create a script tag that refers to
     jquery  '.js file'. This can be done using the below code.

                if (!createJSTag)
                {
                    Application.browser.ExecuteScript(@"
                    var scheme =  window.location.protocol;
                    if(scheme != 'https:')
                        scheme = 'http:';

                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = scheme + '//code.jquery.com/jquery-1.10.1.min.js';
                    document.getElementsByTagName('head')[0].appendChild(script);
                ");
                }
     Once, you create a jquery script tag into your webpage dynamically, call your JQuery
     Selectors, on the webpage.

     Example Selector: $(\"div[class='two columns'] input:first-child\")

     HtmlControl control= ((HtmlControl)Application.browser.ExecuteScript("return " + jquerySelector + "[0];"));

Start Your First CodedUI Test

You can start writing codedui test by using the CodedUI API. You need to follow few steps before you start writing CodedUI Tests. One, Understand the API and identify the classes that are used in identify the control objects. Second, Follow a specific programming standard to structure your CodedUI Test Code.

Now, I will walk you through how to write the Automation Test using the CodedUI API.

   Let us take a simple scenario to start our first automation test.

   Scenario: Search a blog in <yuvarajatcts.blogspot.com> and read the content in the blog.

   Design:  To start your script, let us just go by considering some manual steps to achieve our goal(reading      content).
    1. Launch Browser Window.
    2. Navigate to the specified URL.
    3. Identify the Search text box and type the query.
    4. Wait for the post to load and read the content.

   Now, let me explain how we write the code automate our manual scenario.

  Step1:
             Launch WebBrowser: CodedUI is provided with a class "BrowserWindow" which has the different              methods to perform actions on the browser. So, to launch a browser we have a method "Launch"
             which launches the browser.
             As, CodedUI has support for IE (<11), Firefox(26), and Chrome (Latest) browser, we can play-
             -back the designed scripts on different browsers by making a setting to CurrentBrowser property.

              Example: 
               BrowserWindow.CurrentBrowser="Chrome";
               BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
 
    The above code launches chrome browser with the Blog page navigated to. If you want to launch the   application in firefox then use "BrowserWindow.CurrentBrowser="firefox"" , in IE                                         BrowserWindow.CurrentBrowser="IE" (Optional to use).


Step2:
   Identify the Search text box and type the query. Now let us take the web application and get the search  text box object properties. This can be achieved with different tools/ plugins. Some of them are Firebug for Firefox, Inspect Element for chrome and Firefox, Developer Tools for IE, and Test Builder from CodedUI. So, it is up to you based on your convenience and ease.

Assume, you text box has "<input type='text" id="searchTextBox"> </input>
To identify the above text box , you can write the following code:

    HtmlDocument document=new HtmlDocument(browser);

    HtmlEdit searchEdit=new HtmlEdit(document);
    searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";

In the above lines of code, the first line of Code is mandatory which makes your code run on multibrowser.  If you omit this lines of Code, your code Script only runs in IE, but fails in Chrome and Firefox browsers.

Step3:
Once you identify the object, perform action on the control you identifies, such as Mouse Click, Keyboard Press, etc.,

In this scenario, as I want to set text to search text box, I use "Text" Property which defined in "HtmlEdit" Class.
So, I just call

       searchEdit.WaitForControlReady();
       searchEdit.Text="this is sample";
 Then, Press enter to wait till the page loads

 Keyboard.SendKeys(searchEdit,"{Enter}");

So, You can put the above code into TestMethod as follows:

CodedUI Test Example:

[TestMethod]
public void TestCodedUI()
{
    BrowserWindow.CurrentBrowser="Chrome";
    BrowserWindow browser=BrowserWindow.Launch(new Uri("http://yuvarajatcts.blogspot.com"));
    HtmlDocument document=new HtmlDocument(browser);
    HtmlEdit searchEdit=new HtmlEdit(document);
    searchEdit.SearchProperties[HtmlEdit.PropertyNames.Id]="searchTextBox";
    searchEdit.WaitForControlReady();
    searchEdit.Text="this is sample"; 
   Keyboard.SendKeys(searchEdit,"{Enter}");
}
   

Sunday, June 22, 2014

Introduction to CodedUI Test


Write CodedUI Tests in different ways.

1. Record/Playback
2. Track Object Repository
3. Advanced Coding.

1. Record/Playback: In this way, you can record your steps and playback the recorded steps.

    Cons:
     a.  Huge code generation.
     b.  No waits for page loads, and controls.
     c.  No flexibility to edit our Designer.cs class to modify our Object Repository base.
     Pros:
     a.  You need not to be a programmer to write the test code.
     b.  Very Simple to automate tests.

2. Track Object Repository: In this way, you capture your controls to UIMaps using Test Builder and call the control objects to your test methods.

   Cons:
     a.  Huge code generation.
     b.  Title, Index dependencies make your scripts failure while running the test scripts using
          captured objects using Test Builder.
     c.  No flexibility to edit our Designer.cs class to modify our Object Repository base
   Pros:
    a. Effort to Track Objects is minimized using Test Builder.

3. Advanced Coding: In this way, you capture controls into your class files using API available. And you should be having a good knowledge in C#/VB.NET to write your CodedUI Tests.

 Cons:
  a. An Extra effort is needed to track controls to separate classes.

 Pros:
  a. Can make your tests title independent.
  b. Can make extension methods to identify controls instead of using regular code to identify controls.

A good start to write a CodedUI Test is to record a scenario using Test Builder and understand what the generated coded does.

CodedUI supports Latest Chrome Browser, Firefox26 version and IE11 browsers for web Application, WPF,  WinForms, Silverlight, and Dynamics Controls. Refer msdn blog to get the CodedUI Support matrix.

In the next blog you will find how you can start writing code to generate CodedUI Tests using Advanced coding option.

Saturday, August 25, 2012

Cloud Computing

Cloud Computing
 
Cloud computing is the latest development in the technology which has attracted the attention of all the companies for a gain in all the aspects. So, every one has had  an eager to know, what is cloud computing. Here is the chance to get aware of clould computing.
 The below links gives the introduction of cloud computing. click the links
 
1. Part1
 
2. Part2
 
3. Part3
4. Part4