The playback failed to find the control with the g

2019-02-20 17:34发布

I'm new in Coded UI test, so here's a "simple" question:

Trying to navigate through the options of a menu, i've recorded actions nad tried to playback. I got the following message: The playback failed to find the control with the given search properties

Here's the code generated by recording tool:

public void NavegarSituacao()
        {
            #region Variable Declarations
            HtmlCustom uINotíciasCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UINotíciasCustom;
            HtmlCustom uIEntretenimentoCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIEntretenimentoCustom;
            HtmlCustom uIMulherCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIMulherCustom;
            HtmlCustom uIEsportesCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIEsportesCustom;
            HtmlCustom uIHomemCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIHomemCustom;
            HtmlCustom uITecnologiaCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UITecnologiaCustom;
            HtmlCustom uIVídeosCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIVídeosCustom;
            #endregion

            // Click 'Notícias' custom control
            Mouse.Click(uINotíciasCustom, new Point(89, 21));

            // Click 'Entretenimento' custom control
            Mouse.Click(uIEntretenimentoCustom, new Point(90, 15));

            // Click 'Mulher' custom control
            Mouse.Click(uIMulherCustom, new Point(90, 9));

            // Click 'Esportes' custom control
            Mouse.Click(uIEsportesCustom, new Point(84, 18));

            // Click 'Homem' custom control
            Mouse.Click(uIHomemCustom, new Point(82, 16));

            // Click 'Tecnologia' custom control
            Mouse.Click(uITecnologiaCustom, new Point(85, 8));

            // Click 'Vídeos' custom control
            Mouse.Click(uIVídeosCustom, new Point(70, 11));
        }

Is there a way to catch those elements by some kind of locators(these elements doesn't have id)? Something like this:

public HtmlCustom UIHomemCustom
        {
            get
            {
                if ((this.mUIHomemCustom == null))
                {
                    this.mUIHomemCustom = new HtmlCustom(this);
                    #region Search Criteria
                    this.mUIHomemCustom.SearchProperties["TagName"] = "LI";
                    this.mUIHomemCustom.SearchProperties["Id"] = null;
                    this.mUIHomemCustom.SearchProperties[UITestControl.PropertyNames.Name] = null;
                    this.mUIHomemCustom.FilterProperties["Class"] = null;
                    this.mUIHomemCustom.FilterProperties["ControlDefinition"] = "data-value=\"201405231131464799\"";
                    this.mUIHomemCustom.FilterProperties["InnerText"] = "Homem";
                    this.mUIHomemCustom.FilterProperties["TagInstance"] = "8";
                    this.mUIHomemCustom.FilterProperties["Xpath"] = "#default > div.wrapper > div.menu > div > ul > li:nth-child(5)";
                    this.mUIHomemCustom.WindowTitles.Add("http://cms.home.psafe.com/");
                    #endregion
                }
                return this.mUIHomemCustom;
            }
        }

Here's the menu:

Menu

3条回答
老娘就宠你
2楼-- · 2019-02-20 17:40

One of the interesting things about playback of CODEDUI is that it does not necessarily take into account the time factors of the User during recording. These are the things I've learned over the years about this type of error...

  1. The current window or control is not the Top Most window.
  2. The search criteria is too restrictive, ask yourself is there something in this search criteria that's not the same as the recording. In this case I can see a possible issue with this : "data-value=\"201405231131464799\"" Is that number the same every time?
  3. The control is searched for before it is ready to be done. This is fixed by calling (in your case) Custom.WaitForReady()
  4. I have also seen this (which I don't fully understand) because these recording don't have X,Y coordinates; except for the Point defined in the second parameter. If the recording screen size and the playback screen size are NOT the same, sometimes (but not always) the playback "can't find the control". You can try to omit the point parameter.
  5. The element is simply not visible.
查看更多
手持菜刀,她持情操
3楼-- · 2019-02-20 17:45

Go into the UI map and change the FilterProperty for InnerText to a SearchProperty. Search properties are applied first -- if it finds one exact match, it doesn't even look at Filter properties. In this case, the most important thing about the control (the text value) is a filter property.

It's trying to find a <LI> tag with no ID. It undoubtedly finds multiple matches. Then it applies the filter properties, which are probably things that vary from page load to page load.

You could also apply an ID property to the <LI> tags, then update the UI map search properties so that it searches for that specific ID, which would also solve the problem.

In general, when you're using Coded UI with web applications, it's a good idea to make sure everything on the page has a unique "ID" attribute. This makes it much easier for Coded UI to zero in on the page elements you're trying to interact with.

查看更多
叛逆
4楼-- · 2019-02-20 18:03

I had a similar problem and came across this. In my case, I was using older CodedUI tests that were recorded with a slightly older version (12.0.21005.1). Same error message ("The playback failed to find the control with the given search properties") and the error specifically mentioned a Text input that it couldn't find on the page.

The answer for me was that the BrowserWindow object was losing it's reference when a the page changed (after clicking on a link on a previous page). I don't know why.

The solution was to call BrowserWindow.TryFind() in a log statement and then everything worked find as previously recorded.

Jus thought I'd share in case somebody else has this issue.

查看更多
登录 后发表回答