This is a Sample code for :
Creating a txt file, write data into that file n read data from that file.
code:
Dim fso, MyFile,ts,op,MyFile1
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile("C:\text.txt", True)
MyFile.WriteLine("Last_Name" & " | " & "First_Name" & " | " & "Result") & " " & NewLine
MyFile.WriteLine("---------------------------------") & " " & NewLine
RowCnt=DataTable.GetSheet ("Action1").GetRowCount
For i=1 to RowCnt
DataTable.SetCurrentRow (i)
If DataTable.Value("Result",dtLocalSheet)="FAIL" Then
MyFile.WriteLine (DataTable.Value("Last_Name",dtLocalSheet) &" | " & DataTable.Value("First_Name",dtLocalSheet) &" | " & DataTable.Value("Result",dtLocalSheet)) & " "
MyFile.WriteLine()
end if
Next
MyFile.Close
Set f = fso.GetFile("C:\text.txt")
Set ts = f.OpenAsTextStream(1,-2)
Set MyFile1= fso.CreateTextFile("C:\text1.txt", True)
Do While Not ts.AtEndOfStream
op = op & ts.ReadLine
Loop
MyFile1.WriteLine(op)
ts.Close
MyFile1.close
QTP GUIDE
Monday, November 7, 2011
Alternate to Wait function
Function IOE(ObjectName,mtime)
a=ObjectName.Exist
b=1
while a=0
wait(1)
a=ObjectName.Exist
b = b + 1
if b=mtime then
a=1
end if
wend
End Function
a=ObjectName.Exist
b=1
while a=0
wait(1)
a=ObjectName.Exist
b = b + 1
if b=mtime then
a=1
end if
wend
End Function
Creating Objects
Creates a new, empty description object in which you can add collection of properties and values in order to specify the description object in place of a test object name in a step.
Syntax
set PropertiesColl = Description.Create
Example
1. set EditDesc = Description.Create()
EditDesc("Name").Value = "userName"
EditDesc("Index").Value = "0"
Browser("Welcome: Mercury").Page("Welcome: Mercury").WebEdit(EditDesc).Set "MyName"
2. 'Define User ID field object
Set oUserID = Description.Create()
oUserID("Class Name").Value = "WebEdit"
oUserID("name").Value = "j_username"
Syntax
set PropertiesColl = Description.Create
Example
1. set EditDesc = Description.Create()
EditDesc("Name").Value = "userName"
EditDesc("Index").Value = "0"
Browser("Welcome: Mercury").Page("Welcome: Mercury").WebEdit(EditDesc).Set "MyName"
2. 'Define User ID field object
Set oUserID = Description.Create()
oUserID("Class Name").Value = "WebEdit"
oUserID("name").Value = "j_username"
Clicking "webelement" within a cell of WebTable
This we can do more efficiently by using "Programmatic Descriptions" and substituting properties with variables(You can see in KB articles. But once case, I dont know why,"Programmatic Descriptions" did not work for me. Then I workaround by doing the below code :
numWTLen=Browser("Browser").Page("Page").Frame("Frame").WebTable("Table").RowCount
For i=1 to numWTLen
strConfigStatus=Trim(Browser("Browser").Page("Page").Frame("Frame").WebTable("Table").GetCellData(o,3))
If strConfigStatus="Configured" Then 'If this Condition satisfy, then only we are clicking "Web element" within Cell
'Putting Index value dynamically for every row.
DataTable.Value("Index",dtLocalSheet)=i 'Index starts from "0" but my Webtable first row contains Headings, hence i took index starts from "1"
Browser("Browser").Page("Page").Frame("Frame").WebElement("WebElement").Click
Exit For 'Exiting from "For" Loop
End If
Next
Note:Before executing the above code. we have to parameterize the " WebElement("WebElement")"
(right click on WebElement("WebElement") , choose object Properties and select "Parameter radio button").
property Name "Index" to "Local sheet" with Name "Index". And also obviously add "Index" property
to the " WebElement("WebElement")" property list and remove "innertext" property from property
list.
Example: WebElement("WebElement") contails two properties.
1) Name:html Tag Value=some tag
2) Name:Index Value=1
Thanks!
Raghubansh
numWTLen=Browser("Browser").Page("Page").Frame("Frame").WebTable("Table").RowCount
For i=1 to numWTLen
strConfigStatus=Trim(Browser("Browser").Page("Page").Frame("Frame").WebTable("Table").GetCellData(o,3))
If strConfigStatus="Configured" Then 'If this Condition satisfy, then only we are clicking "Web element" within Cell
'Putting Index value dynamically for every row.
DataTable.Value("Index",dtLocalSheet)=i 'Index starts from "0" but my Webtable first row contains Headings, hence i took index starts from "1"
Browser("Browser").Page("Page").Frame("Frame").WebElement("WebElement").Click
Exit For 'Exiting from "For" Loop
End If
Next
Note:Before executing the above code. we have to parameterize the " WebElement("WebElement")"
(right click on WebElement("WebElement") , choose object Properties and select "Parameter radio button").
property Name "Index" to "Local sheet" with Name "Index". And also obviously add "Index" property
to the " WebElement("WebElement")" property list and remove "innertext" property from property
list.
Example: WebElement("WebElement") contails two properties.
1) Name:html Tag Value=some tag
2) Name:Index Value=1
Thanks!
Raghubansh
Wednesday, October 12, 2011
Many a times you may need to interact with text files using QTP. Interaction can be(but not limited to) in the form of reading input from a file, writing output to a file. This post describe in detail "File handling using QTP".
We use FSO object to do this.
What is FSO?
FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll)
The FSO Object Model has a rich set of properties, methods and events to process folders and files.
How to create a file?
We first create a FSO object using CreateObject and then create a text file using CreateTextFile.
For Example: Suppose you want to create a file called "test.txt" located in C:
Dim fso, file, file_location
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists else false
We would use the same example for the rest of this post.
How to open a file?
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)
//2nd argument can be ForReading, ForWriting, ForAppending
//3rd argument is "True" if new file has to be created if the specified file doesn’t exist else false, blank signify false.
How to read content from a file?
Use ReadLine() method
For example:
Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a file
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop
How to write content to a file?
You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a file
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
How to delete content?
Use DeleteFile() method to delete a file from a particular location
Foe Example:
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)
We use FSO object to do this.
What is FSO?
FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll)
The FSO Object Model has a rich set of properties, methods and events to process folders and files.
How to create a file?
We first create a FSO object using CreateObject and then create a text file using CreateTextFile.
For Example: Suppose you want to create a file called "test.txt" located in C:
Dim fso, file, file_location
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists else false
We would use the same example for the rest of this post.
How to open a file?
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)
//2nd argument can be ForReading, ForWriting, ForAppending
//3rd argument is "True" if new file has to be created if the specified file doesn’t exist else false, blank signify false.
How to read content from a file?
Use ReadLine() method
For example:
Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a file
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop
How to write content to a file?
You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a file
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
How to delete content?
Use DeleteFile() method to delete a file from a particular location
Foe Example:
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)
How-QTP-identifies-objects ?
Understanding the Smart Identification Process
If QuickTest activates the Smart Identification mechanism during a run session (because it was unable to identify an object based on its learned description), it follows the following process to identify the object:
1. QuickTest "forgets" the learned test object description and creates a new object candidate list containing the objects (within the object's parent object) that match all of the properties defined in the Base Filter Properties list.
2. QuickTest filters out any object in the object candidate list that does not match the first property listed in the Optional Filter Properties list. The remaining objects become the new object candidate list.
3. QuickTest evaluates the new object candidate list:
o If the new object candidate list still has more than one object, QuickTest uses the new (smaller) object candidate list to repeat step 2 for the next optional filter property in the list.
o If the new object candidate list is empty, QuickTest ignores this optional filter property, returns to the previous object candidate list, and repeats step 2 for the next optional filter property in the list.
o If the object candidate list contains exactly one object, then QuickTest concludes that it has identified the object and performs the statement containing the object.
4. QuickTest continues the process described in steps 2 and 3 until it either identifies one object, or runs out of optional filter properties to use.
If, after completing the Smart Identification elimination process, QuickTest still cannot identify the object, then QuickTest uses the learned description plus the ordinal identifier to identify the object.
If the combined learned description and ordinal identifier are not sufficient to identify the object, then QuickTest stops the run session and displays a Run Error message.
Reviewing Smart Identification Information in the Test Results
If the learned description does not enable QuickTest to identify a specified object in a step, and a Smart Identification definition is defined (and enabled) for the object, then QuickTest tries to identify the object using the Smart Identification mechanism.
If QuickTest successfully uses Smart Identification to find an object after no object matches the learned description, the Test Results receive a warning status and indicate that the Smart Identification mechanism was used.
If the Smart Identification mechanism cannot successfully identify the object, QuickTest uses the learned description plus the ordinal identifier to identify the object. If the object is still not identified, the test or component fails and a normal failed step is displayed in the results.
Walking Through a Smart Identification Example
The following example walks you through the object identification process for an object.
Suppose you have the following statement in your test or component:
Browser("Mercury Tours").Page("Mercury Tours").Image("Login").Click 22,17
When you created your test or component, QuickTest learned the following object description for the Login image:
However, at some point after you created your test or component, a second login button (for logging into the VIP section of the Web site) was added to the page, so the Web designer changed the original Login button's alt tag to: basic login.
The default description for Web Image objects (alt, html tag, image type) works for most images in your site, but it no longer works for the Login image, because that image's alt property no longer matches the learned description. Therefore, when you run your test or component, QuickTest is unable to identify the Login button based on the learned description. However, QuickTest succeeds in identifying the Login button using its Smart Identification definition.
The explanation below describes the process that QuickTest uses to find the Login object using Smart Identification:
1. According to the Smart Identification definition for Web image objects, QuickTest learned the values of the following properties when you recorded the click on the Login image:

2. QuickTest begins the Smart Identification process by identifying the five objects on the Mercury Tours page that match the base filter properties definition (html tag = INPUT and image type = Image Button). QuickTest considers these to be the object candidates and begins checking the object candidates against the Optional Filter Properties list.
3. QuickTest checks the alt property of each of the object candidates, but none have the alt value: Login, so QuickTest ignores this property and moves on to the next one.
4. QuickTest checks the name property of each of the object candidates, and finds that two of the objects (both the basic and VIP Login buttons) have the name: login. QuickTest filters out the other three objects from the list, and these two login buttons become the new object candidates.
5. QuickTest checks the file name property of the two remaining object candidates. Only one of them has the file name login.gif, so QuickTest correctly concludes that it has found the Login button and clicks it.
Step-by-Step Instructions for Configuring a Smart Identification Definition
You use the Smart Identification Properties dialog box, accessible from the Object Identification dialog box, to configure the Smart Identification definition for a test object class.
To configure Smart Identification properties:
1. Choose Tools > Object Identification. The Object Identification dialog box opens.
2. Select the appropriate environment in the Environment list. The test object classes associated with the selected environment are displayed in the Test object classes list.
Note: The environments included in the Environment list are those that correspond to the loaded add-in environments.
3. Select the test object class you want to configure.
4. Click the Configure button next to the Enable Smart Identification check box. The Configure button is enabled only when the Enable Smart Identification option is selected. The Smart Identification Properties dialog box opens:

5. In the Base Filter Properties list, click Add/Remove. The Add/Remove Properties dialog box for base filter properties opens.

6. Select the properties you want to include in the Base Filter Properties list and/or clear the properties you want to remove from the list.
Note: You cannot include the same property in both the base and optional property lists.
You can specify a new property by clicking New and specifying a valid property name in the displayed dialog box.
Tip: You can also add property names to the set of available properties for Web objects using the attribute/ notation. To do this, click New. The New Property dialog box opens. Enter a valid property in the format attribute/ and click OK. The new property is added to the Base Filter Properties list. For example, to add a property called MyColor, enter attribute/MyColor.
7. Click OK to close the Add/Remove Properties dialog box. The updated set of base filter properties is displayed in the Base Filter Properties list.
8. In the Optional Filter Properties list, click Add/Remove. The Add/Remove Properties dialog box for optional filter properties opens.

9. Select the properties you want to include in the Optional Filter Properties list and/or clear the properties you want to remove from the list.
Note: You cannot include the same property in both the base and optional property lists.
You can specify a new property by clicking New and specifying a valid property name in the displayed dialog box.
Tip: You can also add property names to the set of available properties for Web objects using the attribute/ notation. To do this, click New. The New Property dialog box opens. Enter a valid property in the format attribute/ and click OK. The new property is added to the Optional Filter Properties list. For example, to add a property called MyColor, enter attribute/MyColor.
10. Click OK to close the Add/Remove Properties dialog box. The properties are displayed in the Optional Filter Properties list.
11. Use the up and down arrows to set your preferred order for the optional filter properties. When QuickTest uses the Smart Identification mechanism, it checks the remaining object candidates against the optional properties one-by-one according to the order you set in the Optional Filter Properties list until it filters the object candidates down to one object.
If QuickTest activates the Smart Identification mechanism during a run session (because it was unable to identify an object based on its learned description), it follows the following process to identify the object:
1. QuickTest "forgets" the learned test object description and creates a new object candidate list containing the objects (within the object's parent object) that match all of the properties defined in the Base Filter Properties list.
2. QuickTest filters out any object in the object candidate list that does not match the first property listed in the Optional Filter Properties list. The remaining objects become the new object candidate list.
3. QuickTest evaluates the new object candidate list:
o If the new object candidate list still has more than one object, QuickTest uses the new (smaller) object candidate list to repeat step 2 for the next optional filter property in the list.
o If the new object candidate list is empty, QuickTest ignores this optional filter property, returns to the previous object candidate list, and repeats step 2 for the next optional filter property in the list.
o If the object candidate list contains exactly one object, then QuickTest concludes that it has identified the object and performs the statement containing the object.
4. QuickTest continues the process described in steps 2 and 3 until it either identifies one object, or runs out of optional filter properties to use.
If, after completing the Smart Identification elimination process, QuickTest still cannot identify the object, then QuickTest uses the learned description plus the ordinal identifier to identify the object.
If the combined learned description and ordinal identifier are not sufficient to identify the object, then QuickTest stops the run session and displays a Run Error message.
Reviewing Smart Identification Information in the Test Results
If the learned description does not enable QuickTest to identify a specified object in a step, and a Smart Identification definition is defined (and enabled) for the object, then QuickTest tries to identify the object using the Smart Identification mechanism.
If QuickTest successfully uses Smart Identification to find an object after no object matches the learned description, the Test Results receive a warning status and indicate that the Smart Identification mechanism was used.
If the Smart Identification mechanism cannot successfully identify the object, QuickTest uses the learned description plus the ordinal identifier to identify the object. If the object is still not identified, the test or component fails and a normal failed step is displayed in the results.
Walking Through a Smart Identification Example
The following example walks you through the object identification process for an object.
Suppose you have the following statement in your test or component:
Browser("Mercury Tours").Page("Mercury Tours").Image("Login").Click 22,17
When you created your test or component, QuickTest learned the following object description for the Login image:

However, at some point after you created your test or component, a second login button (for logging into the VIP section of the Web site) was added to the page, so the Web designer changed the original Login button's alt tag to: basic login.
The default description for Web Image objects (alt, html tag, image type) works for most images in your site, but it no longer works for the Login image, because that image's alt property no longer matches the learned description. Therefore, when you run your test or component, QuickTest is unable to identify the Login button based on the learned description. However, QuickTest succeeds in identifying the Login button using its Smart Identification definition.
The explanation below describes the process that QuickTest uses to find the Login object using Smart Identification:
1. According to the Smart Identification definition for Web image objects, QuickTest learned the values of the following properties when you recorded the click on the Login image:

2. QuickTest begins the Smart Identification process by identifying the five objects on the Mercury Tours page that match the base filter properties definition (html tag = INPUT and image type = Image Button). QuickTest considers these to be the object candidates and begins checking the object candidates against the Optional Filter Properties list.
3. QuickTest checks the alt property of each of the object candidates, but none have the alt value: Login, so QuickTest ignores this property and moves on to the next one.
4. QuickTest checks the name property of each of the object candidates, and finds that two of the objects (both the basic and VIP Login buttons) have the name: login. QuickTest filters out the other three objects from the list, and these two login buttons become the new object candidates.
5. QuickTest checks the file name property of the two remaining object candidates. Only one of them has the file name login.gif, so QuickTest correctly concludes that it has found the Login button and clicks it.
Step-by-Step Instructions for Configuring a Smart Identification Definition
You use the Smart Identification Properties dialog box, accessible from the Object Identification dialog box, to configure the Smart Identification definition for a test object class.
To configure Smart Identification properties:
1. Choose Tools > Object Identification. The Object Identification dialog box opens.

Note: The environments included in the Environment list are those that correspond to the loaded add-in environments.
3. Select the test object class you want to configure.
4. Click the Configure button next to the Enable Smart Identification check box. The Configure button is enabled only when the Enable Smart Identification option is selected. The Smart Identification Properties dialog box opens:

5. In the Base Filter Properties list, click Add/Remove. The Add/Remove Properties dialog box for base filter properties opens.

6. Select the properties you want to include in the Base Filter Properties list and/or clear the properties you want to remove from the list.
Note: You cannot include the same property in both the base and optional property lists.
You can specify a new property by clicking New and specifying a valid property name in the displayed dialog box.
Tip: You can also add property names to the set of available properties for Web objects using the attribute/ notation. To do this, click New. The New Property dialog box opens. Enter a valid property in the format attribute/ and click OK. The new property is added to the Base Filter Properties list. For example, to add a property called MyColor, enter attribute/MyColor.
7. Click OK to close the Add/Remove Properties dialog box. The updated set of base filter properties is displayed in the Base Filter Properties list.
8. In the Optional Filter Properties list, click Add/Remove. The Add/Remove Properties dialog box for optional filter properties opens.

9. Select the properties you want to include in the Optional Filter Properties list and/or clear the properties you want to remove from the list.
Note: You cannot include the same property in both the base and optional property lists.
You can specify a new property by clicking New and specifying a valid property name in the displayed dialog box.
Tip: You can also add property names to the set of available properties for Web objects using the attribute/ notation. To do this, click New. The New Property dialog box opens. Enter a valid property in the format attribute/ and click OK. The new property is added to the Optional Filter Properties list. For example, to add a property called MyColor, enter attribute/MyColor.
10. Click OK to close the Add/Remove Properties dialog box. The properties are displayed in the Optional Filter Properties list.

regular expression in VB Script
A regular expression is a string that describes or matches a set of strings. It is often called a pattern as it describes set of strings.
Given underneath is one of the most widely used and ever confused BackLash character. The remaining expressions are serialized below that.
Using the Backslash Character
A backslash (\) instructs QuickTest to treat the next character as a literal character, if it is otherwise a special character. The backslash (\) can also instruct QuickTest to recognize certain ordinary characters as special characters. For example, QuickTest recognizes \n as the special newline character.
For example:
w matches the character w
\w is a special character that matches any word character including underscore
For example, in QTP, while entering the URL of a website,
http://mercurytours.mercuryinteractive.com
The period would be mistaken as an indication of a regular expression. To indicate that the period is not part of a regular expression, you would enter it as follows:
mercurytours\.mercuryinteractive\.com Note: If a backslash character is used before a character that has no special meaning, the backslash is ignored. For example, \z matches z.
Expressions & Explanation
Special characters and sequences are used in writing patterns for regular expressions. The following describes the characters and sequences that can be used.
\
Marks the next character as either a special character or a literal. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^
Matches the beginning of input.
$
Matches the end of input.
*
Matches the preceding character zero or more times. For example, "zo*" matches either "z" or "zoo".
+
Matches the preceding character one or more times. For example, "zo+" matches "zoo" but not "z".
?
Matches the preceding character zero or one time. For example, "a?ve?" matches the "ve" in "never".
.
Matches any single character except a newline character.
(pattern)
Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)".
xy
Matches either x or y. For example, "zwood" matches "z" or "wood". "(zw)oo" matches "zoo" or "wood".
{n}
n is a nonnegative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".
{n,}
n is a nonnegative integer. Matches at least n times. For example, "o{2,}" does not match the "o" in "Bob" and matches all the o's in "foooood." "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m}
m and n are nonnegative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".
[xyz]
A character set. Matches any one of the enclosed characters. For example, "[abc]" matches the "a" in "plain".
[^xyz]
A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain".
[a-z]
A range of characters. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".
[^m-z]
A negative range characters. Matches any character not in the specified range. For example, "[m-z]" matches any character not in the range "m" through "z".
\b
Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".
\B
Matches a non-word boundary. "ea*r\B" matches the "ear" in "never early".
\d -Matches a digit character. Equivalent to [0-9].
\D - Matches a non-digit character. Equivalent to [^0-9].
\f - Matches a form-feed character.
\n - Matches a newline character.
\r
Matches a carriage return character.
\s
Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]".
\S
Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]".
\t
Matches a tab character.
\v
Matches a vertical tab character.
\w
Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]".
\W
Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\num
Matches num, where num is a positive integer. A reference back to remembered matches. For example, "(.)\1" matches two consecutive identical characters.
\n
Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, "\11" and "\011" both match a tab character. "\0011" is the equivalent of "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions.
\xn
Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII codes to be used in regular expressions.
Reference: QTP Documentation-Pattern property
Given underneath is one of the most widely used and ever confused BackLash character. The remaining expressions are serialized below that.
Using the Backslash Character
A backslash (\) instructs QuickTest to treat the next character as a literal character, if it is otherwise a special character. The backslash (\) can also instruct QuickTest to recognize certain ordinary characters as special characters. For example, QuickTest recognizes \n as the special newline character.
For example:
w matches the character w
\w is a special character that matches any word character including underscore
For example, in QTP, while entering the URL of a website,
http://mercurytours.mercuryinteractive.com
The period would be mistaken as an indication of a regular expression. To indicate that the period is not part of a regular expression, you would enter it as follows:
mercurytours\.mercuryinteractive\.com Note: If a backslash character is used before a character that has no special meaning, the backslash is ignored. For example, \z matches z.
Expressions & Explanation
Special characters and sequences are used in writing patterns for regular expressions. The following describes the characters and sequences that can be used.
\
Marks the next character as either a special character or a literal. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^
Matches the beginning of input.
$
Matches the end of input.
*
Matches the preceding character zero or more times. For example, "zo*" matches either "z" or "zoo".
+
Matches the preceding character one or more times. For example, "zo+" matches "zoo" but not "z".
?
Matches the preceding character zero or one time. For example, "a?ve?" matches the "ve" in "never".
.
Matches any single character except a newline character.
(pattern)
Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)".
xy
Matches either x or y. For example, "zwood" matches "z" or "wood". "(zw)oo" matches "zoo" or "wood".
{n}
n is a nonnegative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".
{n,}
n is a nonnegative integer. Matches at least n times. For example, "o{2,}" does not match the "o" in "Bob" and matches all the o's in "foooood." "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m}
m and n are nonnegative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".
[xyz]
A character set. Matches any one of the enclosed characters. For example, "[abc]" matches the "a" in "plain".
[^xyz]
A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain".
[a-z]
A range of characters. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".
[^m-z]
A negative range characters. Matches any character not in the specified range. For example, "[m-z]" matches any character not in the range "m" through "z".
\b
Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".
\B
Matches a non-word boundary. "ea*r\B" matches the "ear" in "never early".
\d -Matches a digit character. Equivalent to [0-9].
\D - Matches a non-digit character. Equivalent to [^0-9].
\f - Matches a form-feed character.
\n - Matches a newline character.
\r
Matches a carriage return character.
\s
Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]".
\S
Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]".
\t
Matches a tab character.
\v
Matches a vertical tab character.
\w
Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]".
\W
Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\num
Matches num, where num is a positive integer. A reference back to remembered matches. For example, "(.)\1" matches two consecutive identical characters.
\n
Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, "\11" and "\011" both match a tab character. "\0011" is the equivalent of "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions.
\xn
Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII codes to be used in regular expressions.
Reference: QTP Documentation-Pattern property
Subscribe to:
Posts (Atom)