coded ui test project, obtain value of asp label
Asked Answered
A

1

13

Created a simple calculator app in webforms. User enters a number in a text field MainContent_numberTb and clicks on results button.

Added a new 'coded UI Test Project' to my solution. Have tested the UI by adding '5', This all works fine. Would now like to compare the actual result against the expected result.

BrowserWindow Browser = BrowserWindow.Launch("http://url");

UITestControl UiInputField = new UITestControl(Browser);
UiInputField.TechnologyName = "Web";
UiInputField.SearchProperties.Add("ControlType", "Edit");
UiInputField.SearchProperties.Add("Id", "MainContent_numberTb");

//Populate input field
Keyboard.SendKeys(UiInputField, "5");

//Results Button
UITestControl ResultsBtn = new UITestControl(Browser);
ResultsBtn.TechnologyName = "Web";
ResultsBtn.SearchProperties.Add("ControlType", "Button");
ResultsBtn.SearchProperties.Add("Id", "MainContent_calBtn");

Mouse.Click(ResultsBtn);

All above code works fine, problem occurs when trying to access the label

<asp:Label ID="AllNumLbl_Res" runat="server"></asp:Label>

What do I insert beside control type? It's not edit as edit is the text field. Then also, what stores the actual result so I can compare AllNumsTB?

string expectedAllNums = "1, 2, 3, 4, 5";
UITestControl AllNumsTB = new UITestControl(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add("ControlType", "?????");
AllNumsTB.SearchProperties.Add("Id", "MainContent_AllNumLbl_Res");

if(expectedAllNums != AllNumsTB.??????)
{
    Assert.Fail("Wrong Answer");
}

UPDATE OK so using the debugger console I was able to get the value of the label using ((Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlSpan)new System.Collections.ArrayList.ArrayListDebugView(((System.Collections.CollectionBase)(AllNumsTB.FindMatchingControls()).List).InnerList).Items[0]).DisplayText

but when I use this in the code & ArrayListDebugView are inaccessible due to protection??

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// UPDATE Thanks K Scandrett for the answer...If I may I was wondering could you also please help me with the validation...If the user enters a letter or a non positive number the error message will fire..

<asp:RegularExpressionValidator ID="regexpName"

 //VALIDATION MESSAGE
                UITestControl PositiveNumValMsg = new UITestControl(Browser);
                PositiveNumValMsg.TechnologyName = "Web";
                PositiveNumValMsg.SearchProperties.Add("Id", "MainContent_regexpName");

This all works fine, however I want to test if the label appears or not...so far I have tried

//bool visible = false;
            //System.Drawing.Point p;

            //// If the control is offscreen, bring it into the viewport
            //PositiveNumValMsg.EnsureClickable();

            //    // Now check the coordinates of the clickable point
            //    visible = PositiveNumValMsg.TryGetClickablePoint(out p)
            //        && (p.X > 0 || p.Y > 0);

            var isVisible = PositiveNumValMsg.WaitForControlPropertyNotEqual(UITestControl.PropertyNames.State, ControlStates.Invisible);

but they all return true even when the label is not shown, but it is still on the page just set to invisible. In that case I should check its style..something like

//string labelText3 = PositiveNumValMsg.GetProperty("style").ToString();

then check if the style contains visibility: visible?

Agglomeration answered 21/7, 2017 at 11:0 Comment(0)
D
3

You want to grab its InnerText property.

It's not mandatory to set ControlType, so some variation of the following should work:

UITestControl AllNumsTB = new UITestControl(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");

var result = AllNumsTB.GetProperty(HtmlLabel.InnerText).Trim();
// var result = AllNumsTB.GetProperty("InnerText").Trim();

OR from https://social.msdn.microsoft.com/Forums/en-US/69ea15e3-dcfa-4d51-bb6e-31e63deb0ace/how-to-read-dynamic-text-from-label-using-coded-ui-for-web-application?forum=vstest:

var AllNumsTB = new HtmlLabel(Browser);
AllNumsTB.TechnologyName = "Web";
AllNumsTB.SearchProperties.Add(HtmlControl.PropertyNames.Id, "MainContent_AllNumLbl_Res");
var result = AllNumsTB.InnerText;

string result2;

// you may need to include this section, or you may not
if (result.Length > 0)
{
    AllNumsTB.WaitForControlReady();
    result2 = AllNumsTB.InnerText;
}

EDIT: Regarding testing an ASP.Net Validator

I've been able to check whether the validator message is displayed with the following method:

1) Created a test asp.net page with a regex validator that requires exactly 2 digits:

<asp:TextBox ID="numberTb" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexpName" ControlToValidate="numberTb" ValidationExpression="\d{2}" runat="server" ErrorMessage="Please enter 2 digits"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />

2) Ran the Coded UI Test builder and started recording => Clicked Input box; typed s; hit tab (the validator error message is showing).

3) Paused the Recorder.

4) Clicked "Generate Code" icon and give it a method name; clicked "Add and Generate" button.

5) Now I dragged and dropped the Crosshair icon onto the validator message. Scrolling down the list of options is the ControlDefinition. Right-clicked it and selected "Add Assertion...".

6) Changed the Comparator to "Contains"; the Comparison Value to " visible;"; and gave it an Assertion Failure message.

7) Clicked the "Generate Code" icon, gave it a method name, etc.

Now we have code that will test the validator by running two methods - first to enter the input and trigger (or not) the validator message; the second to test the validator's message visibility. I copied and pasted the generated code and used it to write another opposing test using " hidden;" when given correct input. Ran both tests, and they both passed.

You will end up with something like (have substituted values):

public void DigitValidatorMsgShownWithIncorrectStringInput()
{
    #region Variable Declarations
    HtmlSpan uIAtleast2digitsPane = this.UIHomePageMyASPNETApplWindow.UIHomePageMyASPNETApplDocument.UIAtleast2digitsPane;
    #endregion

    // Verify that the 'ControlDefinition' property of 'At least 2 digits' pane contains ' visible;'
    StringAssert.Contains(uIAtleast2digitsPane.ControlDefinition, " visible;", "The validator was not shown");
}

Of course all this can be coded manually once you know what you're looking for.

Daph answered 24/7, 2017 at 1:33 Comment(6)
Thank you for your help, the second example worked for me. If I may I have updated the Q to include a validation query. I know this wasnt part of the orignal Q, I will still award your answer as correct but you seem to know what you are talking about and I cant find what i'm looking for so I thought I would ask. If not, thanks for the original answer anyway!...Agglomeration
Validation messages are rendered as a <span> so you should be able to use var PositiveNumValMsg = new HtmlSpan(Browser); if you want to be more explicit. And typically variables start with a lowercase letter, so positiveNumValMsg would be a more conventional name.Daph
cant use PositiveNumValMsg.GetProperty("style") as this returns NULL....Tried var PositiveNumValMsg1 = new HtmlSpan(Browser); PositiveNumValMsg1.GetProperty("Style"); this says internal error in the expression validatorAgglomeration
Thanks for the reply, the problem is not finding the label. I can find it with UITestControl PositiveNumValMsg = new UITestControl(Browser); PositiveNumValMsg.TechnologyName = "Web"; PositiveNumValMsg.SearchProperties.Add("Id", "MainContent_regexpName"); the problem is finding if the style="visibility: is VISIBLE or HIDDEN" so I can test if displays VISIBLE when I enter a letter. thanksAgglomeration
I understand, but I think you can use the same technique they used to search the ControlDefinition for an occurrence of "visible" and if it doesn't find the control it is hidden.Daph
I've tested the " visible;" search of the ControlDefinition and it works well (tested using " hidden;" also). I've edited my answer to explain the steps in case you're still having problems checking your validatorDaph

© 2022 - 2024 — McMap. All rights reserved.