1

chk this code snippet

Please refer the below code.

rv = “Are you 56' taller ?”

If I pass 20 fields ie, until [rv = “ Are you 56' taller ? "]. It’s not working because ‘ – apostrophe is used to comment in QTP

How to handle ' ( apostrophe ) in Xpath using QTP ?

Code Snippet:

 rv = Replace (rv,"'", "\'")
 rv = LEFT(rv,50)

 If SVAL = "Yes" Then

 Set oobj = Browser("xyz").Page("abc").WebElement("xpath:=//div[contains(text(),'"& rv &"')]/../..//label[starts-with(text(),'Yes')]")
 oobj.Click
 oobj.Click

 i = i+1             

End If

I really appreciate your reply.

Motti
  • 110,860
  • 49
  • 189
  • 262
user3411743
  • 11
  • 1
  • 2

4 Answers4

3

Try with the character code chr(39) for apostrophe as shown below:

 "Are you 56" & chr(39) & " taller ?"

Msgbox in QTP showing the "Are you 56' taller"

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
Amit
  • 19,780
  • 6
  • 46
  • 54
  • 1
    I don't know why this was downvoted, this solution actually fixes the problem (unlike the other answers that were here before). – Motti Mar 13 '14 at 10:33
1

As others mentioned this is not because ' is a comment in vbscript (not just QTP) but because you're ending the string too early.

You use single quotes for the string to compare to in the XPath and then the apostrophe closes the string too early. You should instead use regular quotes there too so that the apostrophe doesn't end the string too early.

In order to get a double quote in a string in VBScript write it twice "Like ""this"" for example".

So your XPath should look like this:

"//div[contains(text(),""Are you 56' taller ?"")]"

Rather than this:

"//div[contains(text(),'Are you 56' taller ?')]"

Or using your example:

Browser("xyz").Page("abc").WebElement("xpath:=//div[contains(text(),"""& rv &""")]/../..//label[starts-with(text(),'Yes')]")

(Note this has been tested and works)

Motti
  • 110,860
  • 49
  • 189
  • 262
  • Thanks Motti,Very good explanation.Another point is, If I am using this approach then how to handle the spaces in between the txt as well.If there a big text with multiple questions. Example: Read all the below questions and Answer Yes/No. If any of the answer is NO then choose NO <5 char spaces> 1) Are you taller than 56'2) Do you able to read,write & speak Spanish < 7 char spaces> 3) Are you able to commute to work by your own? < 6 char spaces> 4)Are you staying in town Now considering the above question,how to pass this text in the x -path since there are uneven spaces.Thanks a lot. – user3411743 Mar 15 '14 at 16:26
  • I'm not very familiar with XPath, AFAIK it doesn't support regular expressions however QTP's description does support regexs so perhaps you should use regular description (`\s+` matches one or more spaces or tabs). – Motti Mar 15 '14 at 21:45
0

Use &apos; rather than (') so that the string can be properly processed.

Supporting evidence -> click here.

Community
  • 1
  • 1
Rich
  • 4,134
  • 3
  • 26
  • 45
0

This has nothing to do with the ' being the comment character. This is normal working code:

Msgbox "'I love deadlines. I like the whooshing sound they make as they fly by.' Douglas Adams" 

Your code results into an error because some characters needs to be escaped like <, >, & and your infamous '. To enter the line above correctly into an XML tag you need to do this:

htmlEscaped = "&apos;I love deadlines. I like the whooshing sound they make as they fly by.&apos Douglas Adams"

Here you can find an overview to a set of the most common characters that needs escaping (while this is not totally true: if you are using Unicode/UTF-8 encoding, some characters will parse just fine).

Unfortunately VBScript does not have a native function that escapes HTML like the Escape function for urls. Only if you are on ASP Server, you can use Server.HtmlEncode but that is not the case with you

To generalize html escaping (treath everything as special except for the most commons) you can use a script like this:

Function HTMLEncode(ByVal sVal)

    sReturn = ""

    If ((TypeName(sVal)="String") And (Not IsNull(sVal)) And (sVal<>"")) Then

        For i = 1 To Len(sVal)

            ch = Mid(sVal, i, 1)

            Set oRE = New RegExp : oRE.Pattern = "[ a-zA-Z0-9]"

            If (Not oRE.Test(ch)) Then
                ch = "&#" & Asc(ch) & ";"
            End If

            sReturn = sReturn & ch

            Set oRE = Nothing
        Next
    End If

    HTMLEncode = sReturn
End Function

It could be improved a bit (you'll notice passing objects into this function will result into an error) and made more specific: the regular expression could be matching more characters. I do also not know the performance of it, regular expressions can be slow if used incorrectly, but it proves as an example.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • This solution doesn't solve the OP's question, XPath does not work with HTML encoding. – Motti Mar 13 '14 at 10:31
  • @Motti After reading the question again: You are right. I think TS searches for this: `Set oobj = Browser("xyz").Page("abc").WebElement("xpath:=//div[contains(text(),"""& rv &""")]/../..//label[starts-with(text(),'Yes')]")`. Notice the replaced `'` by `""`. EDIT: nevermind, you provided the same solution. Have an upvote. – AutomatedChaos Mar 13 '14 at 10:59