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");
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.