Tim Almond
Tim Almond

Reputation: 12686

Identifying an element in Visual Studio UI coded test

I'm trying to set up a coded UI test to allow me to check for an error message on a login. The test runs, but I'm struggling to get the assert to work.

The response that comes back is nested as follows:-

<div class='ui-errors'>
   <ul>
         <li>Your password is invalid</li>
   </ul>
</div>

What do I need to set up to check the first li in the div of that class in an assert?

Upvotes: 1

Views: 3307

Answers (4)

SarkarG
SarkarG

Reputation: 687

yonitdm answer will solve your problem, but as per your words, "first li in the div of that class" try below.

// Find Error Div
var errorDiv = new HtmlDiv(new Parent(RootParentWindow));
errorDiv.SearchProperties.Add("Class", "ui-errors");
errorDiv.Find();

// Get UL - First item in div
var errorUL = errorDiv.GetChildren().First(); // or GetChildren()[0]

//  Get all LIs and take first item
var firstLI = errorDiv.GetChildren().First(); // or GetChildren()[0]

Assert.IsTrue(firstLI.GetProperty("InnerText").ToString().Contains("Your password is invalid"));

Upvotes: 0

yonitdm
yonitdm

Reputation: 441

Coded UI can capture DIV. In the following code I've created a custom DIV object from your provided example. AdrianHHH's answer will definitely get you information you need to insert in to my example.

var error = new HtmlDiv(new Parent(RootParentWindow));
        error.SearchProperties.Add("Class", "ui-errors");
        var errors = error.FindMatchingControls();
        foreach (var item in errors)
        {
            Assert.IsTrue(item.GetProperty("InnerText").ToString().Contains("Your password is invalid"));
        }

Upvotes: 2

matthiasgh
matthiasgh

Reputation: 321

If you can get a sample of the page where the assertion is needed I could create it for you, otherwise do what AdrianHHH said.

In case you don't know when you use the assertion tool, all the options you get are different ways to assert that particular control, eg you could assert if it exists or if the inner text is equal etc.

Upvotes: 0

AdrianHHH
AdrianHHH

Reputation: 14038

Coded UI does not really look at DIVs or ULs etc. Coded UI looks at what is drawn on the display. I suggest you use the Coded UI cross-hair tool to examine the error message then add an assertion to check for the message. You might also examine the same area of the screen for a test which passes to see how they differ.

If you are hand coding your test rather than letting Coded UI generate the code for you, I recommend creating a sandbox project and recording the assertion into that. Then copy the useful ideas from the generated code into your own test code.

Upvotes: 0

Related Questions