0

I have created a generic method which can be used to search for records in our AUT.

Now I have a scenario that I need to run my code multiple times. So I have created a loop and tried to execute. The first iteration it runs without any issues and page gets closed. Re-opened the page and all the controls get loaded, but object identification failed during the second iteration.

initializing the HtmlEdit object as below:

HtmlEdit medit = new HtmlEdit(objSearchPage);     
medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";

The playback failed while trying to enter text on 'medit' box during the second iteration. First iteration it succeeded: medit.Text = searchItem;

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
SSNair
  • 1
  • Does the problem occur without your code? I ask because questions about code requires you to post your code. If the question is about coded UI and your code is irrelevant then make that clear. – Lasse V. Karlsen Oct 04 '16 at 09:11
  • initializing the HtmlEdit object as below, HtmlEdit medit = new HtmlEdit(objSearchPage); medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains); medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT"; medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit"; The playback failed while trying to enter text on 'medit' box during the second iteration. First iteration it is success - medit.Text = searchItem; – SSNair Oct 04 '16 at 10:19
  • Possible duplicate of ["control.Exists" within a loop works for first time and not for second time in coded ui](http://stackoverflow.com/questions/27526163/control-exists-within-a-loop-works-for-first-time-and-not-for-second-time-in-c) – AdrianHHH Oct 05 '16 at 15:32

2 Answers2

0

If your browser window is opening and closing during the iteration, you'll need to include the initialization code in the loop or use the AlwaysSearch setting.

foreach(var thing in thingsToDo)
{
    HtmlEdit medit = new HtmlEdit(objSearchPage);     
    medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
    medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
    medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";

    // use medit now and it will work
}

or

HtmlEdit medit = new HtmlEdit(objSearchPage);     
medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";

// I'm not sure this will work because the browser window is different
medit.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
MPavlak
  • 2,133
  • 1
  • 23
  • 38
0

@MPavlak is close to the money. What I think we may be forgetting is you'll also have to reinitialize your parent control (objSearchPage). Make sure that it exists and is found before searching for medit.

foreach(var thing in thingsToDo)
{
    var objSearchPage = new UITestControl(); //you can also just reinitialize here if it's been previously declared.
    objSearchPage.SearchProperties.Add("yourPropertyHere");

    HtmlEdit medit = new HtmlEdit(objSearchPage);     
    medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
    medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
    medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";
}
Ryanman
  • 880
  • 1
  • 8
  • 20