Reputation: 30343
I am automating the WPF application, when i record "WpfComboBox" Control and performed select index on that control it is throwing error like "Failed To Perform Action On Blocked Control Exception". Please help me to over come this problem.
WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom.SubDvsnItemTabList.SubDvsnPIPrismPrismExtensioTabPage);
customContr.SearchProperties.Add(WpfControl.PropertyNames.AutomationId, "legalFormatsControl");
WpfComboBox combLegal = new WpfComboBox(customContr);
combLegal.SearchProperties.Add(WpfComboBox.PropertyNames.AutomationId, "legalFormats");
combLegal.Find();
combLegal.SelectedIndex = 2;
the above is my code, it is throwing error at combLegal.selectedIndex =2
Upvotes: 12
Views: 1188
Reputation: 314
If the element is link or button then it works using the below line of code
Mouse.Click(element);
But, some times we might want to click on div tags or span tags instead of links. For that if the location where to click is not clear then also we get this type of error.
For this we can use the below line of code
Mouse.Click(element.BoundingRectangle.Location);
Upvotes: 0
Reputation: 21
public static void ClickCenterOfAControl(UITestControl control)
{
Point location = control.BoundingRectangle.Location;
location.Offset(control.BoundingRectangle.Width / 2, control.BoundingRectangle.Height / 2);
Mouse.Click(location);
}
Hope this will work
Upvotes: 0
Reputation: 880
This happens a lot in angular web-apps, where there's a parent control that's blocking the "real" one beneath it and also won't accept direct control. Ultimately @Rajesh-S is right on the money, and sometimes his parent-child approach is superior to this one. However it's not always the answer.
To get around it my team uses a method something like this. Use sparingly.
/// <summary>
/// Clicks any control on a page for the given area.
/// </summary>
/// <param name="controlObject">Control Object to Click</param>
public static void ClickControl(UITestControl controlObject, int offset = 5)
{
Rectangle clickArea = controlObject.BoundingRectangle;
Point clickPoint = new Point(clickArea.X + offset, clickArea.Y + offset);
Mouse.Click(clickPoint);
}
Pass in any control where you're getting the correct bounding box. Remember that the offset will be from the top left corner. Normally 5 is good to interact with most controls.
Upvotes: 0
Reputation: 21
Hi I think you have performing some other operations on the same control earlier.
Better to add the same control again and rename it with some other name and make sure it is associated with this single action or operation only.
This will help you.
Upvotes: 0
Reputation: 191
The Reason for this issue:
The control is placed inside an invisible control which is placed inside a parent control. As it is in your code, combLegal
combobox is inside the customContr
Wpfcontrol. but there is another control which blocks you accessing the combobox. The designers must have used this for some other purposes while debugging, and forgotten to remove it after they have done it.
The Possible solutions:
1. Try accessing the invisible control by its parent.
WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
foreach(WpfControl TempContr in customContr.GetChildren())
{
WpfControl ChildContr = TempContr.GetChildren().ElementAt(0);
if(ChildContr is WpfComboBox)
{
combLegal.SelectedIndex = 2;
break;
}
}
2. Try accessing control by checking its width.
WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
foreach(WpfControl TempContr in customContr.GetChildren())
{
if(TempContr.BoundingRectangle.Width>0)
{
combLegal.SelectedIndex = 2;
break;
}
}
3. Try accessing control by checking its Parent's width.
WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
foreach(WpfControl TempContr in customContr.GetChildren())
{
if(TempContr.BoundingRectangle.Width>0)
{
WpfControlCollection Collection = TempContr.GetChildren();
foreach(WpfControl combLegal in Collection)
{
if(combLegal is WpfComboBox)
{
combLegal.SelectedIndex = 2;
break;
}
}
}
}
One of these should resolve your issue. If not, do comment below, we shall give some more attempts. good luck..!!
Upvotes: 3