locked
How to Extract the value of a Value Attribute in Option Tag? RRS feed

  • Question

  •  Hi,

    I am new to VSTS2008,I want to extract the value of Value Attribute of Option tags.

    For example:

    On a web page there are five different Select tags and under that different options.

    For first select tag code is as follows:

    <Select name='CboOne'...>
     <Option value='12'>First</Option>
     <Option selected='selected' value='13'>Second</Option>
    </Select>
    For second tag
    <Select name='CboSecond'>
    <Option value='14'>Third</Option>
    <Option selected='selected' value='15'>Fourth</Option>

    And so on..

    Is there any way to extract the 13 and 15 from value attribute of selected options?

    Wednesday, October 15, 2008 7:42 AM

Answers

All replies

  • Hello,

    If the  default extraction rules are not good for this you need to write Custom Extraction Rule.

    Here is a screencast about creating custom extraction rule - Team System – How to Create Custom Extraction Rule [HE] - Screencast

    And here is an article from msdn - How to: Create a Custom Extraction Rule

    • Proposed as answer by Shai Raiten Thursday, October 16, 2008 12:13 AM
    Thursday, October 16, 2008 12:13 AM
  • Hi,

    I have tried that one also but it is working only for first Select Tag options.

    It is not working for second Select tag options.

    I mean to say i am able to identify the second Select tag in the code by it's name attribute, but there is no way to uniquely identify the Option tag under specific Select tag.

     For my given example:

    Custom extraction rule always returns me 13,even if i am checking the Select tag name before execution of extraction method.
    For Second Select Tag it is returning me options of First Select Tag.

    Thursday, October 16, 2008 9:27 AM
  • Hello,

    Can you please post your custom extraction rule code.

    There is no reason why you can't any value from the Selct tag!
    Thursday, October 16, 2008 9:49 AM
  • Public Class ExtractofCombo  
            Inherits ExtractionRule  
     
            Public Overrides ReadOnly Property RuleName() As String 
                Get 
                    Return "Custom Extract Dropdowbox value" 
                End Get 
            End Property 
     
            Public Overrides ReadOnly Property RuleDescription() As String 
                Get 
                    Return "Extracts the value from a specified Dropdownbox field" 
                End Get 
            End Property 
     
            Private NameValue1 As String 
            Private NameValue2 As String 
            Public Property Name() As String 
                Get 
                    Return NameValue1  
                End Get 
                Set(ByVal value As String)  
                    NameValue1 = value  
                End Set 
            End Property 
            Public Property OptionName() As String 
                Get 
                    Return NameValue2  
                End Get 
                Set(ByVal value As String)  
                    NameValue2 = value  
     
                End Set 
            End Property 
     
     
            Public Overrides Sub Extract(ByVal sender As ObjectByVal e As Microsoft.VisualStudio.TestTools.WebTesting.ExtractionEventArgs)  
     
     
                If Not e.Response.HtmlDocument Is Nothing Then 
     
                    For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"select"})  
                        If String.Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase) Then 
                            For Each tag2 As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"option"})  
                                If (String.Equals(tag2.Name, OptionName, StringComparison.InvariantCultureIgnoreCase) And String.Equals(tag2.GetAttributeValueAsString("selected"), "selected", StringComparison.InvariantCultureIgnoreCase)) Then 
     
                                    Dim formFieldValue As String = tag2.GetAttributeValueAsString("value")  
     
                                    If formFieldValue Is Nothing Then 
     
                                        formFieldValue = String.Empty  
                                    End If 
     
                                    ' add the extracted value to the Web test context  
                                    e.WebTest.Context.Add(Me.ContextParameterName, formFieldValue)  
                                    e.Success = True 
                                    Return 
     
                                End If 
                            Next 
                        End If 
                    Next 
                End If 
                ' If the extraction fails, set the error text that the user sees  
                e.Success = False 
                e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name)  
     
            End Sub 
        End Class 
     
     
    HI,
     
    This is the class.
    I am passing the name of the select tag in Name property.
    And Option in the OptionName property.

    Regards,
    Bhawana
    Thursday, October 16, 2008 1:05 PM
  • we have a select tag rule available on our code plex site: http://www.codeplex.com/TeamTestPlugins/SourceControl/ListDownloadableCommits.aspx  The rule name is ExtractionRuleSelectTag.cs
    Blog - http://blogs.msdn.com/slumley/default.aspx
    Friday, November 7, 2008 2:02 PM
    Moderator