joelliusp
joelliusp

Reputation: 436

Alternate ways of checking control visibility?

<div id="mydiv" runat="server" class="mydivclass" style="display: none">
    <label class="medium" for="txtEntityName">Name of something:</label>
    <br />
    <asp:TextBox ID="txtEntityName" runat="server"></asp:TextBox><br />
</div>

I need to be able to check if my TextBox is visble. Checking txtEntityName.Visible does not appear to work nor does checking if txtEntityName.Style["display"] == "none" because in the html, "display: none" seems to only apply to the div itself. That is to say, the textbox inside of the div is invisible but it does not contain that style.

Are there any alternate ways to check if the textbox is visible? Or a way to tie txtEntityName's style explicitly to mydiv's? I know could directly set the TextBox's visibility but I wanted to see if there were any alternatives before I went down that route.

Upvotes: 2

Views: 1816

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Although directly checking for control's visiblity is not possible in Coded UI Test but there are few ways through which we can do this check.

  1. Using UITestControlNotVisibleException Exception

  2. Using custom method

1.Using UITestControlNotVisibleException Exception

Using this approach we can ensure whether or not a given control is visible or not.Through this approach, first of all we have to perform some action like setting control value so that this Exception could be fired by Coded UI Test. Once this exception is fired then push the Assert.Fail('message');

    try
    {

    }
    catch(UITestControlNotVisibleException exp)
    {
        Assert.Fail("textboxName
            control is invisible on web page.");
    }

But using this method is good as sometimes the same exception doesnot fire. the alternative approach is using custom method.

2.Using custom method

Coded UI Test provides a property called BoundingRectangle which gives the control's position on web page. If control is not visible on web page then control's X and Y co-ordinates are set to -1. We can create custom extended method and directly invoke on TestUiControls. For example -

     public static class CodedUITestControlsLibrary
    {
       public bool IsVisible(this HtmlControl HtmlControlToCheckForVisiblity)
       {
        var xCordinate =  HtmlControlToCheckForVisiblity.BoundingRectangle.X;
        var yCordinate = HtmlControlToCheckForVisiblity.BoundingRectangle.Y;

        return(xCordinate>-1 && yCordinate>-1);
       }
    }

    [TestMethod]
    public void MyTestMethod()
    {
        var HtmlDivControl = {find HtmlDiv Control};
        Assert.IsTrue(HtmlDivControl.IsVisible(),
        "Expected HtmlDiv Control is not visible.");
    }

In the above code, There is one extention method called IsVisible() which can be invoked any kind of CodedUI Control inherited from HtmlControl base control.

Upvotes: 3

afzalulh
afzalulh

Reputation: 7943

You can use jQuery :visible selector. Here's how you can test it:

Add a function to test visibility. Add an input to trigger the function. Your markup may look like this:

<head runat="server">
    <title></title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        function test() {
            if ($('#<%= txtEntityName.ClientID %>').is(':visible')) {
                alert("Visible");
            }
            else {
                alert("Not Visible");
            }

        };

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div id="mydiv" runat="server" class="mydivclass" style="display: none">
                <label class="medium" for="txtEntityName">Name of something:</label>
                <br />
                <asp:TextBox ID="txtEntityName" runat="server"></asp:TextBox><br />
            </div>
            <input type="button" onclick="test();" value="test" />

        </div>
    </form>
</body>

Disclaimer: Elements with visibility: hidden or opacity: 0 are considered visible. Source: :visible Selector.

Upvotes: 1

Related Questions