This is a migrated thread and some comments may be shown as answers.

Is it possible to get HiddenField value in a test?

10 Answers 952 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sammy78
Top achievements
Rank 2
Sammy78 asked on 01 Sep 2010, 07:47 PM
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

10 Answers, 1 is accepted

Sort by
0
Nikolai
Telerik team
answered on 02 Sep 2010, 05:43 AM
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
0
Sammy78
Top achievements
Rank 2
answered on 02 Sep 2010, 01:08 PM
Hi Nikolai,
Thanks for the reply. I will try it out and hopefully get back to you with positive feedback.

Sincerely,
Sam
0
Sammy78
Top achievements
Rank 2
answered on 21 Sep 2010, 03:49 PM
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
0
Nikolai
Telerik team
answered on 22 Sep 2010, 05:47 AM
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
0
Sammy78
Top achievements
Rank 2
answered on 22 Sep 2010, 01:29 PM
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.
0
Nikolai
Telerik team
answered on 22 Sep 2010, 01:49 PM
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
0
Morten Røgenes
Top achievements
Rank 1
answered on 22 Sep 2010, 01:52 PM
.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
0
Sammy78
Top achievements
Rank 2
answered on 22 Sep 2010, 01:55 PM
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.
0
Sammy78
Top achievements
Rank 2
answered on 22 Sep 2010, 01:57 PM
Morten Røgenes: I already checked and I have set the correct ID (and it isn't changed in the generated page source code)
0
Konstantin Petkov
Telerik team
answered on 27 Sep 2010, 08:23 AM
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
Tags
General Discussions
Asked by
Sammy78
Top achievements
Rank 2
Answers by
Nikolai
Telerik team
Sammy78
Top achievements
Rank 2
Morten Røgenes
Top achievements
Rank 1
Konstantin Petkov
Telerik team
Share this question
or