In watir, is there a way to capture the text matched by .present?

73 views Asked by At

It is possible to only get the text that was matched by a call to .present?

The specific issue was that the page contains a string of the form "TIME: ##:##" where # is [0-9]. I'm interested in the value of ##:##

I can solve the specific problem with something like (hopefully no typos)

myString = "TIME: [0-9][0-9]:[0-9][0-9]"
myRegExp = /#{myString.gsub(/\s/, '\s')}/
if ( @browser.div(:text => myRegExp).present? )
    myResult = @browser.div(:text => myRegExp).text
end

In my test, myResult is a string containing all of the text elements on the page; which is more information than I want. I want "TIME: ##:##"

Of course, I can use Ruby to get what I want

myResult [ myRegExp ]

The specific issue is solved. The more general question is more of a best practices question. Is there a better way? Is there a way to get Watir to return just the part of the string that matched or is using the Ruby string class the best way to solve this sort of problem?

1

There are 1 answers

0
Carldmitch On BEST ANSWER

I tend to use Ruby to do any/all of the 'extra' stuff I want out of my tests. In terms of 'Best Practices' I would say just use 'Watir' for 'driving' the browser test. so in you particular case. I would save the full string then parse it later into the format you want.