Search code examples
c#watin

Having trouble clicking on an element in an automated test


I am currently building a test harness for the company I work at. I have experience both with C# and WatiN and have never encountered the issue I am now having.

Below, is a snippet of the markup for the page giving me the issue:

<div id="toggle1" class="NavLayout toggle">
     <span onClick="toggleMenu(1, false);">
          <span id="toggletext1">Quote Processing</span>
     </span>
</div>

As you can see, I have a div, 2 spans and an image. I am using WatiN to try and click the image, that will then expand the menu, exposing yet another layer that I will need to click something else on. The problem I am having is in getting the 'Click' to happen. From what I can see in the snippet, it seems to me I need to be able to click the event, but cannot 'find' it with the code.

Any help out there to be had?


Solution

  • I have also had issues with clicking on certain elements.

    I've run into issues where I could only click on an element if it was highlighted by mousing over the element.

    Since I cannot see your code snippet, I can't tell if there is any javascript that deals with mouseover associated with the image, but if there is, you can try the following:

    img.FireEvent("onmouseover");

    img.FireEvent("onmousedown");

    img.FireEvent("onMouseup");

    You might also might want to try img.FireEvent("onclick") as well.

    These are all guesses, since I can't see your code. It's also possible that rather than clicking on the image element itself, that you may want to try clicking on the parent object.

    EDIT: Ok, now that I can see your code, it appears that you should fire an onClick event against the span with the 'onclick' code in it.

    I don't see an image listed in your code snippet, but this code should call the parent of the lowest level span.

    Watin.Core.Span span = browserinstance
                           .Span(Find.By("innertext", "Quote Processing"));
    span.Parent.FireEvent("onclick");