Today we will see how to check if control is Visible or not on the User Interface. In CodedUI the UITestControl does not provide "Visible" property directly but it has some very useful methods.By using these methods we can determine/find the existence of the control on the User Interface.This method we have write in UIMap.cs file.
This is explained as below:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Input;
using System.CodeDom.Compiler;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
public partial class UIMap
{
//To check if control is visible or not
public void VerifyControlVisible()
{
//Namespace need to include :
//using Microsoft.VisualStudio.TestTools.UITesting;
//using Microsoft.VisualStudio.TestTools.UITest.Extension;
WinText ctrlToTest = this.UICalculatorWindow.UIItem0Window.UIItem0Text;
//UITestControl.Exists-- METHOD CHECKS IF THE CONTROL EXIST OR NOT on User Interface.
if (!ctrlToTest.Exists)
{
//If control is not visible or does not exist then throw exeception
throw new AssertInconclusiveException("The given control is not visible or does not exist.");
}
//****UITestControl.TryFind()-- METHOD CHECKS IF THE CONTROL
//EXIST OR NOT on User Interface.
//This method is MORE RELIABLE as compared to ctrlToTest.Exists .
//So we can get more reliable result from TryFind method
if(!ctrlToTest.TryFind())
{
//If control is not visible or does not exist then TryFind() will return false
//hence we can throw exeception
throw new AssertInconclusiveException("The given control is not visible or does not exist.");
}
//Another alternative approach is using UITestControl.TryGetClickablePoint
//This method finds the control on the User Interface and get the Clickable
//point(i.e. position or the co-ordinates of the control)
//Hence TryGetClickablePoint method also more reliable than the
//UITestControl.Exists method.
Point tmpPoint;
if(!ctrlToTest.TryGetClickablePoint(out tmpPoint))
{
//If control is not visible or does not exist then TryGetClickablePoint()
//will return false hence we can throw exeception
throw new AssertInconclusiveException("The given control is not visible or does not exist.");
}
}
}
These are very helpful methods to find the control existence.
Enjoy Coding!!!
The third method is most reliable to make sure that the control is present in the UI and is clickable also, thus preventing unexpected failures.
ReplyDeleteI used TryClickable to determine whether a control is visible. That worked for me in IE, but not in Chrome.
ReplyDelete(I blog about CUIT over at burdettelamar.wordpress.com.)