owenG
home learn tableau about
divider
SDLC VS 2010 Coded UI Test Coded UI Test and Fitnesse MSBuild 4.0 MSBuild and IronPython MSBuild and IronPython - TFS checkins MSBuild and IronPython - Custom SQL Data








previous next

Coded UI Walkabout Part 4

Next, add a call to the new RemoveInnerLinkTextForOwengHyperlink() from the TestMethod in GoogleSearchOweng.cs, at some point before the assert is tested (see pic below).

Further manual investigation of the .uimap would help help in learning how effective the current hyperlink/control search pattern is but instead I'll get back there via a walkabout that involves changing the text being entered into the Google search box, from 'oweng' to 'owenx'. To do so programmatically, I can go back to GoogleSearchOweng.cs and add a new line just before the method responsible for entering the "oweng" characters and running the search (this.UIMap.SearchFor_oweng()). Constants in the Coded UI tests have all been automatically parameterized as part of the recording processs, where the values for a given method may be accessed by a call in the form of <Recorded method name>Params. Intellisense helps out as I enter the name of the recorded method whose values I want to change:

Params initial

and then provides help when I'm not sure which is the property I want to set:

Params choice 1

    - .UIGoogleSearchEditSendKeys refers to hitting Enter key from 'Google Search' Text Box

Params choice 2

    - .UIGoogleSearchEditText refers to 'oweng' into 'Google Search' Text Box, use this & set = "owenx"

In addition to setting the text to be entered to 'owenx', I need to change the assert that checks the href on the link it finds. Had been a Contains test, against the string '.oweng.net', update that also for the new text. The TestMethod now looks like:

C#
    [TestMethod]
    public void CodedUITestMethod1()
    {
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut...
        // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        this.UIMap.OpenIE();
        this.UIMap.OpenGoogleWebsite();
        this.UIMap.RemoveInnerLinkTextForOwengHyperlink();
        this.UIMap.SearchFor_owengParams.UIGoogleSearchEditText = "owenX";
        this.UIMap.SearchFor_oweng();
        this.UIMap.AssertFirstLinkContains_owengDOTnetExpectedValues.UIOwenGhomeowenGnetHyperlinkHref 
            = "owenx.net";
        this.UIMap.AssertFirstLinkContains_owengDOTnet();
        //add step for closing IE
        this.UIMap.CloseIE();
    }

Now when I run the test I expect maybe CUIT will try to confirm the first link contains 'owenx' and test will pass if so. Run the test and it does pass but the Google results indicate a failure was more appropriate:

Google Search Results - owenx

since I'm really interested in checking the first link and its href relates to wikipedia.org, not owenx.net. But link/result ordinality was actually never really an aspect of the recorded test i.e. CUIT never considered the physical placement of the Hyperlink control when it was initially added to the UI Map. In the current variant of the original test it will help to once again look at the UI Map definition of the hyperlink/control that is being sought:

C#
<UIObject ControlType="Hyperlink" Id="UIOwenGhomeowenGnetHyperlink"
         FriendlyName="owenG - home | owenG.net" SpecialControlType="None">
  <TechnologyName>Web</TechnologyName>
  <WindowTitles>
    <WindowTitle>oweng - Google Search</WindowTitle>
  </WindowTitles>
  <AndCondition Id="SearchCondition">
    <AndCondition Id="Primary">
      <PropertyCondition Name="ControlType">Hyperlink</PropertyCondition>
      <PropertyCondition Name="Id" />
      <PropertyCondition Name="Name" />
      <PropertyCondition Name="TagName">A</PropertyCondition>
      <PropertyCondition Name="Target" />
      <PropertyCondition Name="InnerText">owenG - home | owenG.net</PropertyCondition>
    </AndCondition>
    <FilterCondition Id="Secondary">
      <PropertyCondition Name="AbsolutePath">/</PropertyCondition>
      <PropertyCondition Name="Title" />
      <PropertyCondition Name="Href">http://www.oweng.net/</PropertyCondition>
      <PropertyCondition Name="Class">l</PropertyCondition>
      <PropertyCondition Name="ControlDefinition">class=l onmousedown="return rwt(this,'',</
            PropertyCondition>
      <PropertyCondition Name="TagInstance">1</PropertyCondition>
    </FilterCondition>
  </AndCondition>
  <SupportLevel>0</SupportLevel>
  <Descendants />
</UIObject>

Within the customization code the .InnerText SearchCondition has been removed, meaning that the primary search pattern for the hyperlink is:
any control with ControlType="Hyperlink" AND and a TagName="A"

That results in multiple candidates, probably every hyperlink in the results section (the particular TopWindow->Descendant->Descendant heirarchy defined in the XML must still hold true, so only a subset of controls on the entire page are candidates). CUIT then does does an 'ordered match', looking for an exact match by going through the FilterCondition properties one by one until it finds a single, exact match. This fails in above scenario because, at the least, the Href value it is looking for (http://www.oweng.net) will not be found. I had changed the Assert check on the found control to match "owenx.net" but didn't touch the original FilterProperties. My understanding is that with no controls having matched the initial sets of rules, CUIT is designed to then return the 'first match' i.e. the first control matching the first FilterProperty (if no matches, look for first control matching the second FilerProperty etc.). In many Google result sets this would indeed be the first link listed. In this instance though the first FilterProperty check is for AbsolutePath = "/" and the first hyperlink candidate is to a sub-page within its parent domain. The code equivalent of the pattern check, Uri.AbsolutePath, would return a string including the full path off of the root (firstSubfolder/secondSubfolder). The original "/" value was recorded by the Test Builder, when it processed the root "oweng.net" url. Likewise, the href found in this version of the test is a root address, "owenx.net". That link is therefore the first candidate control to match the first FilterProperty (AbsolutePath).

Now to try and make the test do what I really want it to do and assert on the href value of the first ordinal link of the results. To begin, get rid of all the existing customization, re owenx text and removal of the .InnerText SearchProperty. Now the general idea will be to allow the control search process to complete and only after the candidate is found, test to see if that control appears to actually be the first hyperlink appearing in the results section of Google search page.

Working in GoogleSearchOweng.cs, create a new method named IsReallyTheFirstLink as below and call from the main CodedUITestMethod():

C#
        [TestMethod]
        public void CodedUITestMethod1()
        {
            this.UIMap.OpenIE();
            this.UIMap.OpenGoogleWebsite();
            this.UIMap.SearchFor_oweng();
            string searchString = this.UIMap.SearchFor_owengParams.UIGoogleSearchEditText;
            bool isFirstLink = this.IsReallyTheFirstLink(this.UIMap.UIOwengGoogleSearchWinWindow.
                UIOwengGoogleSearchDocument.UIRsoCustom, searchString + ".net");
            if (isFirstLink)
            {
                this.UIMap.AssertFirstLinkContains_owengDOTnet();
            }
            else
            {
                Assert.Fail("Located control is not the first hyperlink in google result set");
            }
            //add step for closing IE
            this.UIMap.CloseIE();
        }

        public bool IsReallyTheFirstLink(UITestControl resultControl, string hrefText)
        {
            bool isFirst = false;
            UITestControlCollection children = resultControl.GetChildren();
            string hrefOfFirstChild = children[0].GetProperty("href") as string;
            if (hrefOfFirstChild != null && hrefOfFirstChild.Contains(hrefText))
            {
                isFirst = true;
            }
            return isFirst;
        }

This is starting to go way out on a limb but the idea is that if the first child control/hyperlink of the results section (.UIRsoCustom, as originally defined in the UI Map generated by the Test Builder) has a href value containing the string "oweng.net", we'll assume it is the same control found by the CUIT search pattern and pass the test. Give it a try and ...

    Test method TestProjectGoogleSearches.GoogleSearchOweng.CodedUITestMethod1 threw exception: 
    System.NotSupportedException: The property href is not supported for this control.

Failure. Investigate a little further, first by adding a line in the new method that will highlight the control in question at run-time by drawing a blue box around it:

C#
    children[0].DrawHighlight();

with the result:

Highlight first result

Which does not look right - should be a much smaller rectangle, matching the dimensions of the hyperlink as originally defined by the Test Builder. Long story short, this new IsReallyTheFirstLink() really did work correctly when I first wrote it but something changed google.com-wise in the time between the initial test and this current write-up. In my initial test the direct children of the .UIRsoCustom control were of type HtmlControls.HtmlHyperlink and there would have been at least 10 child controls. Now the children collection only has three members: the first three simple web results, before the links start getting into Image and Video results. These controls are coming in as HtmlControls.HtmlCustom, a type that lacks a Href property. Also the number of 'about' results for "oweng" is now coming up as 11,600, much lower than the 82,800 appearing in the initial screenshot when the Test Builder was used to add the first link to the UI Map. First idea is that somehow Google's Universal Search (aka Blended Search) was not in operation in the earlier searches, where there was not a sub-set of three text results appearing before Image results and Video results. Second thought, who really cares at this point, wasn't a good idea in the first place to try and automate something like this. But then walking-about is not the same thing as travelling and in the end learnin' is learnin'.

THE END.

previous next