-2

I'm running a Test Project in visual studios that is running test steps for a web page.

The web page itself is coded poorly. it has something like this:

<a id ="GenericButton" </a> 

<div id="UniqueID" class="ThisDiv">
    <a id ="GenericButton" </a> 
</div>

The issue that I'm having is that I need to click the link inside the div "UniqueID". I don't want to click the first web button.

Using the code:

HtmlHyperlink myLink = new HtmlHyperlink(this.IE);
myLink.SearchProperties[HtmlHyperlink.PropertyNames.Id] = "GenericButton";
Mouse.Click(myLink)

Will result in clicking the first button instead of the button inside of the div "UniqueID"

I can get the div using:

HtmlDiv myDiv = new HtmlDiv(this.IE);
myDiv.SearchProperties[HtmlCheckBox.PropertyNames.Class] = "ThisDiv";
myDiv.SearchProperties[HtmlCheckBox.PropertyNames.Id] = "UniqueID";
//The below line is later referenced
int myDivTag = myDiv.TagInstance;

But how can I then capture the a frame inside of this div?

Furthermore, when debugging, the object myDiv doesn't seem to properly set itself in Visual Studios until running the line "int myDivTag = myDiv.TagInstance;". Why doesn't the debugger know which object I am referring to until after that happens?

I am using Visual Studio libraries for these operations instead of something like WatiN.

user2589339
  • 91
  • 10

2 Answers2

2

If the web page is poorly code, then the tests for the web page should fail for the bits that have been implemented poorly - simple.

In HTML, two elements should not have the same ID. So write your tests expecting only the single element. Since the input markup to your test will provide two elements with the same ID, the test should fail and remain failed until the problem is actually fixed on the other end.

There's no point hacking your tests to work with bad implements as it defeats the point of testing.

Update: I have found this answer which may give a solution to finding the nested a element.

Community
  • 1
  • 1
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
  • I Agree, but I have no access to this code. What can I do? – user2589339 Oct 22 '14 at 13:43
  • Well that all depends on the goal of your testing which you will need to explain. Why does that particular button has to be clicked? – Adrian Sanguineti Oct 22 '14 at 13:48
  • The first button isn't actually visible. it still exists in the html code though. But both buttons have the same purpose. Under certain conditions, the first button will appear and become clickable. but under the testing conditions, it must remain invisible. The issue of the web page is independnet from my issue though. How can I capture a link that is supposed to be inside of a div? – user2589339 Oct 22 '14 at 13:51
  • I'm not at a computer which has a version of Visual Studio which supports coded UI tests so unfortunately that makes it difficult to try out ideas for you. But I believe that [this answer](http://stackoverflow.com/a/23202565/2641278) may be of some use to you. – Adrian Sanguineti Oct 22 '14 at 14:28
  • When possible, use id or className tags in the HTML rather than an index of the nodes as I did in that question. – Ryan Cox Oct 24 '14 at 18:54
1

Adrian gave the helpful link to the fix: How to handle testing an HTML control that lacks sufficiently unique attributes?

HtmlDiv myDiv = new HtmlDiv(this.IE);
myDiv.SearchProperties[HtmlDiv.PropertyNames.Class] = "ThisDiv";
myDiv.SearchProperties[HtmlDiv.PropertyNames.Id] = "UniqueID";
HtmlHyperlink myLink = new HtmlHyperlink(myDiv);
myLink.SearchProperties[HtmlHyperlink.PropertyNames.id] = "GenericButton";

Mouse.Click(myLink);
Community
  • 1
  • 1
user2589339
  • 91
  • 10