Is it possible to get HiddenField value in a test?

11 posts, 0 answers
  1. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 01 Sep 2010 Link to this post

    Hi,
    I need to get the value of a HiddenField, to use it in a coded_step test (for a request to the DB) Is it possible?

    Thanks
    Sam
  2. Nikolai
    Admin
    Nikolai avatar
    111 posts

    Posted 02 Sep 2010 Link to this post

    Hi Sammy78,

    This scenario is possible. Here is an example:
    If we assume that this is the HTML representation of field:

    <input type="hidden" id="myHiddenField" value="MyValue" />

    The easiest way to get the value is to find the HtmlHiddenElement in the page by its id and extract its value. To do this follow this steps:
    1. Convert a click step to code
    2. Change the code from this:
    // Click 'HtmlTag'
    Pages.HttpLocalhost9000HtmlHid.HtmlTag.Click(false);

    to this:

    [CodedStep(@"GetValue 'myHiddenField'")]
    public void HiddenField_CodedStep()
    {
        //Get Hidden field by id
        var hiddenFieldControl =               Pages.HttpLocalhost9000HtmlHid.Get<HtmlInputHidden>("id=myHiddenField");           
        var fieldValue = hiddenFieldControl.Value;
    }

    Please note that "HttpLocalhost9000HtmlHid" is the page that is currently in use in my test yours will be different. If that page does not contain the hidden element the test step will fail.

     

    Best wishes,
    Nikolai
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 02 Sep 2010 Link to this post

    Hi Nikolai,
    Thanks for the reply. I will try it out and hopefully get back to you with positive feedback.

    Sincerely,
    Sam
  4. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 21 Sep 2010 Link to this post

    Hi Nikolai,
    I was finally able to try out your solution, but my test can't seem to find the hiddenField, but it might have to do with the page definition. Here is the code for the aspx:

    <asp:Content ID="Content2" ContentPlaceHolderID="cphContenuPage" runat="server">
         <div class="grid_24">
            <div class="clear"></div>
            <div class="grid_24 H1TitrePage">
                <asp:Label ID="lblTitreH1" runat="server" Text="lblTitreH1"></asp:Label>
                <asp:Image ID="imgBandeau" runat="server" CssClass="ccImage" />
                <asp:Image ID="imgDestination" runat="server" CssClass="ccImage" />
            </div>
            <div class="clear"></div>
        <telerik:radscriptmanager ID="RadScriptManager1" runat="server">
        </telerik:radscriptmanager>   
        <h2 id="h2Titre" runat="server">Partez l'esprit tranquille !</h2>
        <br />  
        <h5 id="lblPolice" runat="server">NO de Police</h5>
        <asp:Label ID="lblNoPoliceCache" runat="server" Text="Label" Visible="False"></asp:Label>

       
    <asp:HiddenField ID="hfNoPolice" runat="server" />
        <uc1:ProtectionsAssures ID="ProtectionsAssures1" runat="server" />
        <uc2:GrilleRecommandation ID="GrilleRecommandation1" runat="server" />
        <br />
        <table style="width: 900px;">
            <tr>
                <td width="80%">
                </td>
                <td align="right" valign="top" width="15%">
                    <asp:Button ID="btnRetournerQuoter" runat="server" Width="150px" />                               
                </td>                        
            </tr>
        </table
         
        </div
    </asp:Content>

    And here's the code the I put in the coded_step:
    <CodedStep("Click 'Div'")> _
        Public Sub HiddenField_CodedStep()
            'Get Hidden field by id
            Dim hiddenFieldControl As HtmlInputHidden = Pages.Page_confirmation.Get(Of HtmlInputHidden)("id=hfNoPolice")
            Dim fieldValue As String = hiddenFieldControl.Value
            Log.WriteLine("Value: " + fieldValue)
        End Sub

    And my log doesn't write.
    Thanks for the help

    Sam
  5. Nikolai
    Admin
    Nikolai avatar
    111 posts

    Posted 22 Sep 2010 Link to this post

    Hello Sammy78,

    From the page you've sent I don't see a Value attribute for you "hfNoPolice" field. This hidden field has only two attributes - id and name:
    Server:

    <asp:HiddenField ID="hfNoPolice" runat="server" />
    Rendered Client Html:
    <input type="hidden" name="hfNoPolice" id="hfNoPolice" />

    so it is normal the value to be 'null'. May be there is a server or client method for setting the value that is not part of the code you sent?
    Anyway I added a 'Value' attribute to your code:
    <asp:HiddenField ID="hfNoPolice" runat="server" Value="myCustomValue" />

    and the test returned the value as it should.(see attachment)


    All the best,
    Nikolai
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 22 Sep 2010 Link to this post

    The value is set on the load method of the page (and I have checked the source code of the page at runtime and the value is present in the hidden field.
  7. Nikolai
    Admin
    Nikolai avatar
    111 posts

    Posted 22 Sep 2010 Link to this post

    Hello Sammy78,

    Is this page on live site?
    If it is possible we would like to debug this problem locally because we cannot reproduce it in our test environment. 

    Best wishes,
    Nikolai
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Morten Røgenes
    Morten Røgenes avatar
    24 posts
    Member since:
    Apr 2010

    Posted 22 Sep 2010 Link to this post

    .NET will probably change the ID of the control to something like 'Content2_hfNoPolice' because the hiddenfield is inside a NamingContainer.

    In the browser, try to 'View source' and see what the ID really is
  9. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 22 Sep 2010 Link to this post

    Sadly, the website isn't online yet. It is on our unit and fonc servers, but outside access is blocked. And I'm not allowed to release too much info at this time.

    I was wondering if the problem occured because the hiddenfield is inside a div and I had the wrong path or something. Also, I translated the code to VB, cause well, that's what we use, but maybe I didn't do it correctly.
  10. Sammy78
    Sammy78 avatar
    27 posts
    Member since:
    May 2010

    Posted 22 Sep 2010 Link to this post

    Morten Røgenes: I already checked and I have set the correct ID (and it isn't changed in the generated page source code)
  11. Konstantin Petkov
    Admin
    Konstantin Petkov avatar
    1911 posts

    Posted 27 Sep 2010 Link to this post

    Hi Sammy78,

    We will really need a sample app reproducing the problem to help you further. Once we have that we will investigate the issue and post back our findings. We can continue communicating in private if you prefer to not share anything publicly.

    Best wishes,
    Konstantin Petkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top