Welcome, Guest. Please login or register.
Login with username, password and session length

 
Advanced search

18120 Posts in 4741 Topics- by 31743 Members - Latest Member: suuraj
 

Pages: [1]   Go Down
Print
0 Members and 1 Guest are viewing this topic.
Author Topic: Regular Expresion Handling Question    (Read 4057 times)
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« on: February 24, 2010, 11:31:39 AM »

I got what I thought was incorrect Regular Expression handling in a script. I'm curious if anyone else thinks this is odd:

If I have a WebElement with innertext = "Text:" shouldn't a Regular Expression = "Text" find a match?

Code
GeSHi (qtp):


'Attached to the post is the sample html file with the following code:

'<html>

'<head></head>

'<body><p>Text:</p></body>

'</html>



Set regEx = New RegExp

regEx.Pattern = "Text"

MsgBox regEx.Test("Text:") 'True



MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=Text").Exist(0) 'False why?

MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=Text:").Exist(0) 'True

 
Created by GeSHI 1.0.7.20
« Last Edit: February 24, 2010, 11:36:39 AM by Christian Desserich » Logged
Asiq Ahamed
Moderator
Sr. User
*****
Offline Offline

Posts: 459



WWW
« Reply #1 on: February 24, 2010, 08:46:47 PM »

Hi Christian,
       QTP doesn't understand regular expression unless we use the ".*" regular expression symbol in descriptive programming.

Code
GeSHi (qtp):
WebElement("html tag:=P", "innertext:=Text.*")'Here you get True

 
Created by GeSHI 1.0.7.20

But the below code is only for regular expression, that's why your text matches with your pattern without using ".*" these symbols.
 
Code
GeSHi (qtp):
Set regEx = New RegExp



regEx.Pattern = "Text"



MsgBox regEx.Test("Text:") 'True

 
Created by GeSHI 1.0.7.20
Logged
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #2 on: February 24, 2010, 09:48:49 PM »

QTP doesn't understand regular expression unless we use the ".*"

Absolutely untrue. All DP properties are regular expressions by default. However if you don't believe that, you can explicitly set it with a Description object and you will get the same result.

Code
GeSHi (qtp):


Set oDesc = Description.Create

oDesc("micclass").Value = "WebElement"

oDesc("html tag").Value = "P"

oDesc("innertext").RegularExpression = True

oDesc("innertext").Value = "Text"

MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement(oDesc).Exist(0) 'False why?

 
Created by GeSHI 1.0.7.20


You are missing the point. I am fully aware that .* will match any following characters. It will also work with a "VBScript" regular expression.


Code
GeSHi (qtp):


Set regEx = New RegExp

regEx.Pattern = "Text.*"

MsgBox regEx.Test("Text:") 'Also True

 
Created by GeSHI 1.0.7.20


The point is: Given the fact that VBScript is the native language for QTP why does it handle regular expression differently?
Logged
Asiq Ahamed
Moderator
Sr. User
*****
Offline Offline

Posts: 459



WWW
« Reply #3 on: February 24, 2010, 10:21:57 PM »

Hi Christian,
          I knew you would come up with these point,

Code
GeSHi (qtp):
Set oDesc = Description.Create



oDesc("micclass").Value = "WebElement"



oDesc("html tag").Value = "P"



oDesc("innertext").RegularExpression = True



oDesc("innertext").Value = "Text.*"



MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement(oDesc).Exist(0)'Now you get True

 
Created by GeSHI 1.0.7.20

Quote
Given the fact that VBScript is the native language for QTP why does it handle regular expression differently?

Yes VBScript is the native language for QTP. But the below lines are not native for VBscript,

Code
GeSHi (qtp):
oDesc("innertext").RegularExpression = True

oDesc("innertext").Value = "Text.*"

Browser("micclass:=Browser").Page("micclass:=Page").WebElement(oDesc).Exist

 
Created by GeSHI 1.0.7.20
Logged
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #4 on: February 24, 2010, 10:28:20 PM »

Then the question stands. Why the heck would QTP handle this regular expression differently than any other regular expression engine I have ever used?

Here is a javascript tester that also behaves as I would expect:

http://www.regular-expressions.info/javascriptexample.html
« Last Edit: February 24, 2010, 10:34:18 PM by Christian Desserich » Logged
hari sreekanth
Full User
***
Offline Offline

Posts: 223


Trying to become an advanced user


WWW
« Reply #5 on: February 24, 2010, 11:18:11 PM »

Hi,
The difference(I think) is:
The aim of regexp in vbscript is to return as many matches it can find from a given string.
The aim of regular expressions(not regexp) in QTP is to find a unique match.

suppose i have to links named "Test" and "Test:" . if i want to refer to "Test" link how would i do it if qtp allows normal regexp logic?

please correct me if i am wrong. I just have a opinion.
regards,
Logged

http://BugHunterz.blogspot.com
Looking for a JOB. Please update me if you have an openning in your org.
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #6 on: February 24, 2010, 11:52:11 PM »

I would think in that instance you would just use constants. Maybe it's just a smart identification thing.
Logged
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #7 on: February 25, 2010, 12:00:47 AM »

I guess that's the point of the question. Why make a regular expression behave like a constant, if you can explicitly stipulate that you want the property to be a constant?

Code
GeSHi (qtp):


Set oDesc = Description.Create

oDesc("micclass").Value = "WebElement"

oDesc("html tag").Value = "P"

oDesc("innertext").RegularExpression = False

oDesc("innertext").Value = "Text"

MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement(oDesc).Exist(0) 'False just the same as if it were a RegEx

 
Created by GeSHI 1.0.7.20

I suppose this is more of an annoyance than a question, but I thought maybe there was a good answer.
Logged
hari sreekanth
Full User
***
Offline Offline

Posts: 223


Trying to become an advanced user


WWW
« Reply #8 on: February 25, 2010, 12:21:23 AM »

please correct your code. Actually if you want regular expressions in qtp to behave the way you want it to be then your code will look like:
Code:
Set oDesc = Description.Create
oDesc("micclass").RegularExpression = False
oDesc("micclass").Value = "WebElement"
oDesc("html tag").RegularExpression = False
oDesc("html tag").Value = "P"
oDesc("innertext").RegularExpression = False
oDesc("innertext").Value = "Text"
MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement(oDesc).Exist(0)

So what do you prefer? Smiley
Logged

http://BugHunterz.blogspot.com
Looking for a JOB. Please update me if you have an openning in your org.
Anshoo Arora
Advanced User
*****
Offline Offline

Posts: 835



WWW
« Reply #9 on: February 25, 2010, 06:18:46 AM »

Christian,

In your statement:

Quote
Code
GeSHi (qtp):
MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=Text").Exist(0) 'False why?

 
Created by GeSHI 1.0.7.20

innertext:=Text does not find Text: because, even though ReGex is true, you have not specified the pattern that QTP must use to find the remaining characters. Running a RegExp match is a different approach altogether. Its a good thing DP does not support such matches, otherwise it would become impossible to search correct strings in blocks of text.

Quote
QTP doesn't understand regular expression unless we use the ".*" regular expression symbol in descriptive programming.
QTP should understand any type of RegEx used in a DP statement. For example, all of the below are valid:

Code
GeSHi (qtp):
Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text.*", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text.", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text\D", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text\W", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text\S", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text.$", "html tag:=P").Highlight

Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=Text[;|:]", "html tag:=P").Highlight

 
Created by GeSHI 1.0.7.20

Quote
Given the fact that VBScript is the native language for QTP why does it handle regular expression differently?
I think you're confusing RegExp with the way a regular expression would work with a Description Object or a DP statement. With RegExp, you have to control over how you would like to run matches, and the RegExp will only match the first occcurence of the text by default. This will not hold true if Global = True though. With DP, you are not specifying a pattern, but a description for the target object that supports RegEx patterns, but for QTP to know the pattern, the user must provide it at all times.

In other words, with RegExp, you are providing a pattern. With DP/DescriptionObject, you are not.
Logged

Best Regards,

Anshoo Arora
AdvancedQTP Forums Moderator

http://relevantcodes.com - My Blog
Asiq Ahamed
Moderator
Sr. User
*****
Offline Offline

Posts: 459



WWW
« Reply #10 on: February 25, 2010, 06:40:49 AM »

Thanks Anshoo Smiley
Logged
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #11 on: February 25, 2010, 08:41:19 AM »


Its a good thing DP does not support such matches, otherwise it would become impossible to search correct strings in blocks of text.

How so? It is not impossible to find strings in blocks of text with the RegExp object, even with Global set to True.

Code
GeSHi (qtp):


'HTML Page structure

'<html>

'<head></head>

'<body><p>This is a bunch of text. It has bunches of words.</p></body>

'</html>



Set regEx = New RegExp

regEx.Pattern = "\bbunch\b"

regEx.Global = True

MsgBox regEx.Test("This is a bunch of text. It has bunches of words.") 'True

Set matches = regEx.Execute("This is a bunch of text. It has bunches of words.")

For Each match In matches

        MsgBox match.Value 'Only matches "bunch"

Next



MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=\bbunch\b").Exist(0) 'False

 
Created by GeSHI 1.0.7.20

I realize that QTP is looking for a Regular Expression that represents the entire  string you are trying to match, but at this point I just don't understand why. In what scenario can you do something with a "QTP handling" of a Regular Expression that you cannot do with a "VBScript handling" a Regular Expression? What's the advantage?
Logged
Anshoo Arora
Advanced User
*****
Offline Offline

Posts: 835



WWW
« Reply #12 on: February 25, 2010, 07:12:11 PM »

Quote
How so? It is not impossible to find strings in blocks of text with the RegExp object, even with Global set to True.

If you run a RegExp match in a block of text, you can either find the first match (Global=False) or all matches (Global=True). DP works in the same manner, but to identify the same string within the block of text, you will have to use Index - which would equate to the Matches found through RegExp. If DP would support the matches, then each time in the DP statement, we would have to set a collection object, which would contain all the matches returned from the description. Wouldn't it be a lot more work? Instead, to access an element, we can create a description with its Index and identify it without the extra work.

Quote
Code
GeSHi (qtp):
MsgBox Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=\bbunch\b").Exist(0) 'False
Created by GeSHI 1.0.7.20

It returns False because the entire element is:

This is a bunch of text. It has bunches of words.

You would have to do this to identify it:

Code
GeSHi (qtp):
Browser("micclass:=Browser").Page("micclass:=Page").WebElement("html tag:=P", "innertext:=.*bunch.*").Exist(0)

 
Created by GeSHI 1.0.7.20

I'm not sure which part is confusing you here. Are you comparing QTP's Regex handling for DP with VBScript?  In the OP:

Quote
If I have a WebElement with innertext = "Text:" shouldn't a Regular Expression = "Text" find a match?

"Text" is not a regular expression in a DP statement. Its a description. "Text." would be a regular expression.
Logged

Best Regards,

Anshoo Arora
AdvancedQTP Forums Moderator

http://relevantcodes.com - My Blog
Christian Desserich
Full User
***
Offline Offline

Posts: 121



WWW
« Reply #13 on: February 26, 2010, 08:57:20 AM »

I'm just going to drop it after this. I'm not confused in the least about how to get the behavior I want. I have no problem coding to whatever standard the tool I'm working with uses Wink. My confusion lies in the reason that Mercury decided to do it the way they did instead of doing it the same way Microsoft did. I don't see the advantage. If I were able to learn the advantage, that would make me a better programmer, which is what I'm always striving to become.

I'm not sure that my question is fully understood. I'll try to clarify one more time, but again, I know how to get the behavior I want, so if there really isn't an answer, I'll just drop it, I also just thought it was an interesting discussion topic kind of like discussing the advantages and disadvantages of OO principles. Maybe the answer is that: Mercury implemented their own Regular Expression engine before VBScript added the RegExp object http://msdn.microsoft.com/en-us/library/ms974570.aspx (1999), and there really is no particular reason or advantage to doing it one way or the other. If that is the answer, then of course I have no problem with that. Again, just trying to be a more informed programmer by attempting to investigate the reason and possible advantages for the difference.

Clarifications:
 - DP "bunch" is the same behavior as RegExp "^bunch$" - Both will not find a match
 - DP "bunch" is NOT the same behavior as RegExp "bunch" - DP will not find a match, RegExp will find two matches
 - DP ".*bunch.*" is the same behavior as RegExp "bunch" - Both will find two matches
 - DP ".*bunch.*" is the same behavior as ReGExp ".*bunch.*" - Both will find two matches
 - DP "\bbunch\b" is NOT the same behavior as RegExp "\bbunch\b" - DP will not find a match, RegExp will find one match
 - DP ".*\bbunch\b.*" is the same behavior as RegExp "\bbunch\b" - Both will find one match
 - DP ".*\bbunch\b.*" is the same behavior as ReGExp ".*\bbunch\b.*" - Both will find one match

I don't see the advantage of having to type the ".*" before and after a DP Regular Expression when it would have the same behavior without the extra characters with a RegExp. If I wanted a constant, I could code it that way. Any particular advantages to making DP behave this way?
Logged
Pages: [1]   Go Up
Print
Jump to: