Sunday, May 3, 2015

Coded UI Test : How to detect properties of an UI object in desktop and web application by using Coded UI test builder tool

Coded UI Test builder tool can help us to determine the properties of an UI object if we follow code first approach to automate our Windows/WPF/Web application. Every time when we add search properties of an object, we go to Coded UI Test builder tool and drag the icon of the UI spy button and drop it to the UI control.
Here we will explain a step by step process to detect a search properties of an UI object on our windows or Web application.
Step 1: We want to detect search properties of different UI object of our calculator application. At first create a coded UI Test project.
  • Open Visual Studio (In my case it is visual studio 2013)
  • Navigate to File>New>Project at File Menu.
SNAGHTML4ee51a7f
  • New Project window is displayed.
SNAGHTML4eefafc9
  • Select “Test” from left side Project Template category under visual C#.
  • Select “Coded UI Test Project”. Note that in my case .Net Framework 4.5.1 is selected.
  • Enter Project name as “MSUITest”.
  • Select Location.
  • Enter solution name. In my case this is the same as project name.
  • Check “Create directory for solution” option
  • Click on Ok button
image
  • A new coded UI test project is created and Recorder window (Title : Generate Code for Coded UI test) is displayed. Since we write code for every steps, we do not need recorder. Click Cancel button to close the recorder window.
image
Step 2 : We are ready to go on and now open our calculator application.
Step 3 : Go to Visual studio editor and right click on mouse under Coded UI Test method area where we have to write codes as the following.
image
Step 4 : Click on select “Generate Code for Coded UI test” and click “Use Coded UI Test Builder” like the following.
image
Step 5 : Coded UI Test builder window is displayed.
image
Step 6 : Now drag the UI spy button (Crosshair) means keep pressing left button of Mouse and move mouse pointer on the UI object where you need to detect search properties and then free the mouse left button. A blue color rectangle is drawn on the UI object.
For example if drag the UI spy button and drop it to Calculator window we see a blue color rectangle area.
[image9.png]
Step 7 : After freeing the left button of mouse on the object property list window is displayed. This is a drop down list and detect and displayed all the available properties posses on the UI object.
For example in our calculator application when drop the UI spy button on the calculator window we see a drop down list of properties.
[image12.png]
Step 8 : We can use any of these properties as search properties. There are two column on this property list - Property  and value. Property contains the property name used as parameter of SearchProperties class’s Add method and Value is also the parameter contains value of that property name.
Common Syntax to add a search property of an object.
UIObject.SearchProperties.Add(UIClass.PropertyNames.propertyName, porpertyValue);
Here,
UIObject : It is UI object that needs to be operated to automate the application. It is declared under respective UI class.
UIClass : It is the class of UI object. Coded UI test has a set of classes to handle all basic windows UI.
propertyName : It is the list of available property list of that UI object.
porpertyValue : It is the available property value.

In our example if we add search property of calculator window, the code will be
WinWindow mainWindow = new WinWindow();
mainWindow.SearchProperties.Add(WinWindow.PropertyNames.Name, "Calculator");
mainWindow.WindowTitles.Add("Calculator");  
Note that we add “Name” property as search properties parameter and also value “Calculator”.
image
Note that we add the window title name of that UI object by the following statement.
      mainWindow.WindowTitles.Add("Calculator");

Here we add title name with so that system can search the object of that window that has given title name.

In case of a button we get the following result.

image
In case of web application, to detect properties of Search button on Bing search page,
image

Happy Coded UI testing!!!

Wednesday, April 29, 2015

Coded UI Test : How to automate a web application using Code First approach

Now a days Microsoft Visual Studio Coded UI test is becoming popular as it is supporting basic windows UI object as well as WPF and Silverlight UI object. Coded UI test framework has its own search algorithm to search UI object from the window and web page. In our previous post we have shown on how to automate a web application using record and playback approach. Here I will explain how we can automate a Web application using code first approach. We do not record any steps here. We write code for every steps here.

Our example application is Microsoft’s page “http://www.microsoft.com”.

Step One : Writing Test cases
At first we write the test cases that we have to automate.

Test Case : Verify search functionality of Microsoft Page. 

Steps:
  • Navigate to http://www.microsoft.com
  • Enter the search text “Coded UI Test” in the search text box. 
  • Click on Search button
  • Verify the result.
Step Two: Create a Coded UI test Project
  • Open Visual Studio (In my case it is visual studio 2013)
  • Navigate to File>New>Project at File Menu.
SNAGHTML4ee51a7f
  • New Project window is displayed.
SNAGHTML4eefafc9
  • Select “Test” from left side Project Template category under visual C#.
  • Select “Coded UI Test Project”. Note that in my case .Net Framework 4.5.1 is selected.
  • Enter Project name as “MSUITest”.
  • Select Location.
  • Enter solution name. In my case this is the same as project name.
  • Check “Create directory for solution” option
  • Click on Ok button
image
  • A new coded UI test project is created and Recorder window (Title : Generate Code for Coded UI test) is displayed. Since we write code for every steps, we do not need recorder. Click Cancel button to close the recorder window.
image
Step Three: Write Codes for every step
  • Go to Visual studio editor and see that there is a namespace names “MSUITest” as same with the project name.
  • Under this namespace there is a class “CodedUITest1”.
  • Under this class there is a method named “CodedUITestMethod1”. Note that there is an attribute “[TestMethod]”.
  • We will write separate method for every test cases and multiple methods can be write in a single class.
  • Generally all the test cases for single feature writes in a single class.
  • Method name should be similar to Test case title so that we can understand about the test cases by the method name.
  • Also we write the test case title as comment before the method name.
image
  • Here we write the Test case title as comment before the method name.
//Verify search functionality of Bing Search engine.
  • Here we rename the method name as
    public void VerifySearchFunctionality()
  • Now start Writing the codes.
  • At first we have to run the Browser application and navigate to Microsoft Page. Code will be
//Navigate to http://www.microsoft.com
string AppURL = "http://www.microsoft.com";
BrowserWindow Browser = BrowserWindow.Launch(new Uri(AppURL));
Note that BrowserWindow class is used here for opening the IE browser. Coded UI test opens Internet explorer as default browser. If we run our script to other browsers like Chrome or Firefox, we need to different codes and run the script in different way. I will show you to my other posts.
Here Launch() method is used to launch the application in IE browser.
  • Enter the search text “Coded UI Test” in the search text box. 
//Enter the search text “Coded UI Test” in the search text box.  
UITestControl SearchTextBox = new UITestControl(Browser);
SearchTextBox.TechnologyName = "Web";
SearchTextBox.SearchProperties.Add(UITestControl.PropertyNames.ControlType, "Edit");
Keyboard.SendKeys(SearchTextBox, "Coded UI Test");
Note that we have UITestControl class here to detect the element from the web page. We have taken control type as Search properties for search text box.
We have taken help the coded UI test builder tool to determine the properties of an object. I will discuss it on my later post on how we can determine the properties of an object by using coded UI test builder tool. Now for this task, we just need to right click on  Visual studio editor and select “Generate Code for Coded UI test” and click “Use Coded UI Test Builder” like the following.
image
Coded UI Test builder window is displayed and now just drag the UI spy button (Crosshair) and drop it.
image
Now property list window is displayed. It has a list of properties and you can use any property as search properties. 
Common Syntax to add a search property of an object.
UIObject.SearchProperties.Add(UIClass.PropertyNames.propertyName, porpertyValue); 
Here,
UIObject : It is UI object that needs to be operated to automate the application. It is declared under respective UI class.
UIClass : It is the class of UI object. Coded UI test has a set of classes to handle all basic windows UI.
propertyName : It is the list of available property list of that UI object.
porpertyValue : It is the available property value.
  • Click on Search button
//Click on Search button 
UITestControl SearchButton = new UITestControl(Browser);
SearchButton.TechnologyName = "Web";
SearchButton.SearchProperties.Add(UITestControl.PropertyNames.Name, "ctl00$SearchButton");
Mouse.Click(SearchButton);
  • Verify the result.
//Verify the result
UITestControl SearchResult = new UITestControl(Browser);
SearchResult.TechnologyName = "Web";
SearchResult.SearchProperties.Add(UITestControl.PropertyNames.ControlType, "Hyperlink");
SearchResult.SearchProperties.Add(UITestControl.PropertyNames.FriendlyName, "Test Automation Using Visual Studio 2010 Coded UI ");
Assert.AreEqual("Hyperlink", SearchResult.ControlType.Name);         
Note that we add  AreEqual() under Assert class which is a built in class under Visual Studio Test Framework used for verifying the result. It has two argument as “Expected” and “Actual” result.
  • Now VerifySearchFunctionality() method looks like the following:
image 
Step Four: Run the test
  • Go to Build menu and click on Build Solution option.
image
  • Go to Test menu and click on Windows>Test Explorer.
image
  • In the Test Explorer, Test Name is displayed.

image
  • Right click on the Test Name and click on “Run Selected Test”.
image
  • Test will run and Test result is displayed at the Test Explorer.

image
image
Here we see how we can write the test steps by using C# code and run it. We use the full infrastructure of Coded UI test but we write the code that is in our control and we can reuse it for different purposes. It is more maintainable.
That’s all for now….
Happy Coded UI testing!!!










Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...