Forum Discussion

cindyso's avatar
cindyso
New Contributor
3 years ago

How to get substring from a text

//get feature

def feature = context.expand( '${Check Feature_JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row/NAME[text()="UVA "]//following-sibling::ENABLED}' ) 

The space after keyword 'UVA' might disappear sometimes. How could I update the script to avoid the space? Thanks.

 

Something like:

context.expand( '${Check Feature_JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row/NAME[text().trim()="UVA"]//following-sibling::ENABLED}' ) 

 

 

  • We inherit all of the Java methods of string, so I use substring

    def feature = context.expand( '${Check Feature_JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row/NAME[text()="UVA "]//following-sibling::ENABLED}' ) 

    def Slice =feature.substring(0,3)

  • cindyso's avatar
    cindyso
    New Contributor

    mattb Thanks for your solution. It really helps.

    What if the text is dynamic? Text is 'UVA ' for case1, while text is 'UV ' for case2, for example.

    • nmrao's avatar
      nmrao
      Champion Level 3

      You may use a list with all possible cases and see if the value is one of them in the list.

      • cindyso's avatar
        cindyso
        New Contributor

        nmrao thank you. But do you  have any other ideas?

        Example XML result:

        <Results>
            <ResultSet fetchSize="128">
                <Row rowNumber="1">
                    <NAME>UVA </NAME>
                    <SUBSCRIPTIONKEY>1858219</SUBSCRIPTIONKEY>
                    <ACCOUNTKEY>101</ACCOUNTKEY>
                    <ENABLED>true</ENABLED>
                    <STARTDATE>2020-06-29 20:38:00.0</STARTDATE>
                    <ENDDATE>2022-12-31 00:00:00.0</ENDDATE>
                </Row>
                <Row rowNumber="2">
                    <NAME>UV </NAME>
                    <SUBSCRIPTIONKEY>1858220</SUBSCRIPTIONKEY>
                    <SUBSCRIPTIONKEY>103</SUBSCRIPTIONKEY>
                    <ENABLED>false</ENABLED>
                    <STARTDATE>2019-06-29 20:38:00.0</STARTDATE>
                    <ENDDATE>2022-12-31 00:00:00.0</ENDDATE>
                </Row>
            </ResultSet>
        </Results>
         
        I expect to assert something like
        context.expand( '${Check Feature_JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row/NAME[text().trim()="UVA"]//following-sibling::ENABLED}' == true
  • nmrao's avatar
    nmrao
    Champion Level 3
    How it should be validated? Only valid names names where enabled is true?
  • cindyso's avatar
    cindyso
    New Contributor

    nmrao 

    I expect to do assertion.

    def feature = context.expand( '${Check Feature_JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row/NAME[text()="UVA "]//following-sibling::ENABLED}' ) 

    assert feature == true

     

    But I don't expect the tailing space after keyword 'UVA'. Because the tailing space might disappear every once.

    I wonder if we could use the similar statement? Otherwise, I have to break my script into several lines.