Data driving Radio buttons RRS feed

  • Question

  • Apolgies for the terrible layout. I'll tidy it up later...

    Imagine a simple page with three radio buttons and a submit button.
    These radio buttons represent a user selection.

    All three radio buttons belong to the same group. But the page is unusual in that on initial display none of the radio buttons is selected.
    Lets start recording:
    - click on radio button1
    - click on radio button2
    - click on radio button3
    - click submit

    Stop the recorder and generate code.
    Now why did I click on all three buttons instead of selecting the option I want? Its because my aim is to customize the test and I've found
    from earlier recording that the recorded method and more importantly corresponding method params class will only contains
    controls that are touched during the recording.

    Examining the recorced method and params class we find:

    public Rec_TouchAllThreeRadioButtons(){
    #region Variable Declarations
    HtmlRadioButton mainContent_RadioButRadioButton = this.HomePageMicrosoftIntWindow.Httplocalhost10170DeClient.HomePageDocument.MainContent_RadioButRadioButton;
    HtmlRadioButton mainContent_RadioButRadioButton1 = this.HomePageMicrosoftIntWindow.Httplocalhost10170DeClient.HomePageDocument.MainContent_RadioButRadioButton1;HtmlRadioButton mainContent_RadioButRadioButton2 = this.HomePageMicrosoftIntWindow.Httplocalhost10170DeClient.HomePageDocument.MainContent_RadioButRadioButton2;

    // Select 'MainContent_RadioButton1' radio button
    mainContent_RadioButRadioButton.Selected = this.Rec_TouchAllThreeRadioButtonsParams.MainContent_RadioButRadioButtonSelected;  

    // Select 'MainContent_RadioButton2' radio button
    mainContent_RadioButRadioButton1.Selected = this.Rec_TouchAllThreeRadioButtonsParams.MainContent_RadioButRadioButton1Selected; 

    // Select 'MainContent_RadioButton3' radio button
    mainContent_RadioButRadioButton2.Selected = this.Rec_TouchAllThreeRadioButtonsParams.MainContent_RadioButRadioButton2Selected;}

    And the corresponding params class: 

    public class Rec_TouchAllThreeRadioButtonsParams{
    #region Fields
    /// <summary>
    /// Select 'MainContent_RadioButton1' radio button
    /// </summary>
    public bool MainContent_RadioButRadioButtonSelected = true  

    /// <summary>
    /// Select 'MainContent_RadioButton2' radio button
    /// </summary>
    public bool MainContent_RadioButRadioButton1Selected = true 

    /// <summary>
    /// Select 'MainContent_RadioButton3' radio button
    /// </summary>
    public bool MainContent_RadioButRadioButton2Selected = true;

    Note how each parameter has the value true.

    ok so far, but I want to drive this page from values in a spreadsheet.
    I have no problem setting up the databinding and pulling the desired values out.
    Lets say one row in the spreadsheet defines:
    -radiobutton1 - false
    -radiobutton2 - true
    -radiobutton3 - false

    My first approach was to simply follow the guidelines by setting up the parameter values then calling the recorded method.
    This fails because apparently I can't set the selected property to false.
    We find in the log:
    Playback Error: System.ArgumentException: SetState of "False" is not supported on control type: RadioButton

    My second approach if to modify the record method from:

    // Select 'MainContent_RadioButton1' radio button

    To: [note thegreen coloring is wrong, its not a comment]
    // Select 'MainContent_RadioButton1' radio button
    if (this.Rec_TouchAllThreeRadioButtonsParams.MainContent_RadioButRadioButtonSelected)
       mainContent_RadioButRadioButton.Selected =

    So I'm sating if we want to selected the radio button go ahead and set selected  =true.
    But if the parameter value if false leave the button alone. Which given its default state of "unselected" will leave it unselected.

    This works but everytime a page has radio buttons I'm forced to modify the recorded code.
    Why  can't I just set the Selected propoerty to False?
    Is there an easier way of dealing with radio buttons?


    mainContent_RadioButRadioButton.Selected = this.Rec_TouchAllThreeRadioButtonsParams.MainContent_RadioButRadioButtonSelected;


    • Edited by sovande Wednesday, November 25, 2009 5:15 PM layout
    Wednesday, November 25, 2009 5:05 PM


  • Hi,

    It throws ArgumentException when passed false because it is not possible to unselect already selected radio button. You can only do that by selecting some other radio button. Hence this is by design.

    Other thing you can try is to have id/name of the radio button which you want to select. Then in the code read this data and then search for this radio button and then select it.

    Code will look something like

    HtmlRadioButton radioButton =
    new HtmlRadioButton(

    radioButton.SearchProperties.Add("Id", "//Id you read from DataSource");
    radioButton.Selected = true;

    Wednesday, November 25, 2009 5:27 PM