Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coded ui test project, obtain value of asp label

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?

like image 361
John Avatar asked Jul 21 '17 11:07

John


People also ask

How do I record coded UI?

In the Generate Code for Coded UI Test dialog box, choose Record actions > Edit UI map or add assertions. The Coded UI Test Builder appears. Record a sequence of actions. To start recording, choose the Record icon.

What is the difference between Coded UI and selenium?

Selenium IDE supports XML data sources. Coded UI Test supports any data source supported by.NET framework, which can come in the form of a. CSV file, XML file. It can also support any other data source like SQL Server table, Access table etc.

What is coded UI testing?

Coded UI tests are automated tests that drive your application through its user interface. Tests revolve around functional testing of the UI controls.

How do I record a UI Web test?

To do so, navigate to Window > Preferences > Test > Recording > Web UI Recording and select Record all actions on the page. Do not change any browser preferences, including JavaScript settings. Recording and playing back Web UI scripts in a browser requires that JavaScript be enabled.


1 Answers

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.

like image 90
K Scandrett Avatar answered Oct 01 '22 21:10

K Scandrett