Saturday, September 27, 2008

QTP Regular Expression Examples

QTP Regular Expression


Objects and text strings with varying (changeable) values can be identified by QuickTest using Regular expressions.

Regular expressions can be used:


to define property values of an object.

to parameterize a step.

to create a checkpoint with changeable values.





Important points regarding Regular expressions:

You can use regular expressions only for values of type string.

When any special character in a regular expression is preceded by a backslash (\), QuickTest searches for the literal character.

You can define a regular expression for a constant value, a Data Table parameter value, an Environment parameter value, or a property value in a programmatic description.

For more common options to create Regular Expressions, see QTP User Guide.


Instead of writing more about QTP regular expressions, lets quickly jump to examples.




Below you will find examples of :

QTP Regular Expression Example 1



[This is just an example using Yahoo mail inbox. Your inbox unread mails may differ from the one shown in this example]

1. Launch QTP and open a new test.

2. Open Internet Explorer.
[Now we have QTP with a blank test and Google open.]

3. Click on Record in order to start recording.

4. Copy and paste this URL (https://login.yahoo.com/config/login_verify2?&.src=ym) in the browser's address bar to open Yahoo mail login.

5. Type your user name and password to login to Yahoo mail.

6. When Yahoo mail is open, click on the Inbox link as shown in the screenshot below.



7. Click on Stop in order to stop recording.
My recorded code looks like this:

Browser("Browser").Page("Page").Sync
Browser("Browser").Navigate "https://login.yahoo.com/config/login_verify2?&.src=ym"
Browser("Browser").Page("Yahoo! Mail: The best").WebEdit("login").Set "sach2n"
Browser("Browser").Page("Yahoo! Mail: The best").WebEdit("passwd").SetSecure "4801a2cbf793b46aad67194b5cbc961c071f"
Browser("Browser").Page("Yahoo! Mail: The best").WebButton("Sign In").Click
Browser("Browser").Page("Yahoo! Mail - sach2n@yahoo.com").Link("Inbox (6)").Click

Now if you don't check any mail in your inbox and log out and then again run this code it will work fine.
But if you check any mail like if I check one mail in my inbox then it will be Inbox(5) in the above screen shot, then if I run this code it will fail and show the below error.



Now we will change the above code with the help of regular expression so that it will work even if there is only one unread mail.

In QTP go to Resources (menu) ->Object Repository.

Object Repository window will open. Now follow the screen shots below.





When you click on the button as in above screen shot it will open 'Value Configuration Options' window. On this window click on Regular Expression check box. When you click on checkbox it will show warning as in the screen shot below. Just click on Yes.






Now in the Constant text box (above screenshot) enter what I have entered "Inbox \([5-6]\)" and click Ok and close Object Repository window.

This Regular Expression setting which we have done works for inbox unread mails between 5 and 6 e.g. if your inbox says inbox(5) or inbox(6).

Run the test. It passes for me because I had 5 unread mails in my inbox (inbox(5)).

You can do this setting according to your convenience e.g. [1-5] for unread mails between 1 and 5 and so on.



QTP Regular Expression Example 2



1. Open QTP and a new test.

2. Open Internet Explorer and open this URL http://www.worldtimeserver.com/current_time_in_IN.aspx

3. Click on Record in order to start recording.
4. Go to Insert-> Checkpoint->Text Checkpoint.

5. QTP will be minimized and mouse pointer will change into hand shape.

6. Click on time as shown in below screenshot.



7. Text Checkpoint Properties window will open with the text on which we clicked on, in red color.

8. In this window, on the right hand side of 'Constant' radio button click on pencil and paper button to open "Constant Value Options" button.

9. Check Regular Expression checkbox and in the value field type:(1[012]|[1-9]):[0-5][0-9] (am|pm) . Click OK. Again click Ok to come out of Text Checkpoint Properties window.

10. Click on Stop in order to stop recording.

It will record only one line in the expert view:

Browser("India current local time").Page("India current local time").Check CheckPoint("India current local time from WorldTimeServer.com")

Now you can refresh that website so as to see the current time and run this test again. It should pass.
QTP Regular Expression Example 3



1. Open QTP and a new test.

2. Open Internet Explorer and open this URL http://www.worldtimeserver.com/current_time_in_IN.aspx

3. Click on Record in order to start recording.

4. Go to Insert-> Checkpoint->Text Checkpoint.

5. QTP will be minimized and mouse pointer will change into hand shape.

6. Click on time as shown in below screenshot.



7. Text Checkpoint Properties window will open with the text on which we clicked on, in red color.

8. On the right hand side of 'Parameters' radio button, click on that pen and paper icon, to open Parameter Options dialog box.

9. Make sure Parameters Type is Data Table. In the Advanced Configuration area click on Regular Expression checkbox. Click Ok. Again click ok to come out of Text Checkpoint Properties window.

10. Click Stop in order to stop recording.

In the expert view it will write this code:

Browser("India current local time").Page("India current local time").Check CheckPoint("India current local time from WorldTimeServer.com")

In the global data sheet it will add a new column heading "India_current_local_timeChecked_Text" and will add in the first row- the current time. Now in place of this current time enter this (1[012]|[1-9]):[0-5][0-9] (am|pm) and save it.

Run the test. It should work fine.

No comments: